• 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);
    
        }
    }
  • 相关阅读:
    winform 窗体移动API、窗体阴影API
    winform 对话框、打印框
    winform 容器控件
    winform listview控件
    winform 计算器
    Winform 主窗体更换 构造函数传值
    Winform 菜单和工具栏控件
    0103 luffy项目配置
    0102 三大视图配置
    1230 视图家族类
  • 原文地址:https://www.cnblogs.com/happyPawpaw/p/3183511.html
Copyright © 2020-2023  润新知