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);
    
        }
    }
  • 相关阅读:
    查询类里面对象个数
    动手动脑2
    关于随机数的产生-动手动脑1
    单词频率代码测试
    反码补码报告
    动手动脑课上总结
    java开学第一周测试代码
    【P1825】表达式整除
    工程代码の初體驗
    差分:IncDec Sequence 差分数组
  • 原文地址:https://www.cnblogs.com/happyPawpaw/p/3183511.html
Copyright © 2011-2022 走看看