zoukankan      html  css  js  c++  java
  • 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);
    
        }
    }
  • 相关阅读:
    vue 生产包 背景图片-background图片不显示
    数组的方法
    前端常用Utils工具函数库合集
    vue路由
    问题
    Promise与async/await -- 处理异步
    vue中axios使用
    移动端-调试工具
    微信公众平台开发(8) 自定义菜单功能开发
    微信公众平台开发(6) 翻译功能开发
  • 原文地址:https://www.cnblogs.com/happyPawpaw/p/3183511.html
Copyright © 2011-2022 走看看