• swing自定义border


    public class MyBorder extends AbstractBorder {
    
        private static final long serialVersionUID = 1L;
    
        private int xOff;
        private int yOff;
        private Insets insets;
    
        public MyBorder(int x, int y) {
            this.xOff = x;
            this.yOff = y;
            this.insets = new Insets(0, 0, this.xOff, this.yOff);
        }
    
        @Override
        public Insets getBorderInsets(Component c) {
            return this.insets;
        }
    
        @Override
        public void paintBorder(Component c, Graphics g, int x, int y, int width,
                int height) {
            g.translate(x, y);
            BufferedImage rightImage = createBufferedImage(this.xOff, height
                    - this.yOff, Color.red, 0.5f);
            BufferedImage bottomImage = createBufferedImage(width - 2 * this.xOff,
                    height, Color.green, 0.5f);
            g.drawImage(rightImage, width - this.xOff, this.yOff, null);
            g.drawImage(bottomImage, this.xOff, height - this.yOff, null);
            g.translate(-x, -y);
    
        }
    
        private BufferedImage createBufferedImage(int width, int height,
                Color color, float alpha) {
            BufferedImage bufferedImage = new BufferedImage(width, height,
                    BufferedImage.TYPE_INT_BGR);
            Graphics2D g2d = bufferedImage.createGraphics();
            bufferedImage = g2d.getDeviceConfiguration().createCompatibleImage(
                    width, height, Transparency.TRANSLUCENT);
            g2d.dispose();
            g2d = bufferedImage.createGraphics();
            g2d.setColor(color);
            g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
                    alpha));
            g2d.fillRect(0, 0, width, height);
            g2d.dispose();
            return bufferedImage;
    
        }
    
        public static void main(String[] args) {
            JFrame frame = new JFrame("border");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JTextField text = new JTextField("helloKitty");
            text.setPreferredSize(new Dimension(100, 30));
            JButton button = new JButton("donald duck");
            // text.setBorder(new MyBorder(10, 10));
            // text.setForeground(Color.yellow);
            frame.getContentPane().setLayout(new BorderLayout());
            button.setBorder(new MyBorder(5, 5));
            frame.getContentPane().add(text, BorderLayout.CENTER);
            frame.getContentPane().add(button, BorderLayout.SOUTH);
            frame.setSize(300, 300);
            frame.setVisible(true);
    
        }
    }
  • 相关阅读:
    怀才就像怀孕,时间久了才能让人看出来
    程序开发基础学习五(json配置、解析文件,c++篇)
    PHP 代码 加密
    2010年8月22日周日_StylingAndSkinningOverview_6.10
    2010年8月21日周六_GeoprocessingTasks_6.7
    2010年8月19日周四_insideTheAPI_Using Extent_6.4
    2010年8月22日周日_StylingAndSkinningNavigation_6.11
    2010年8月20日周五_UsingGraphic_6.5
    2010年8月18日周三_insideTheAPI_usingMap_6.2
    2010年8月21日周六_RoutingTasks_6.8
  • 原文地址:https://www.cnblogs.com/happyPawpaw/p/3183511.html
Copyright © 2020-2023  润新知