JFrame改变边界为圆角矩形

来源:互联网 发布:淘宝买药怎么付款 编辑:程序博客网 时间:2024/05/22 04:24

效果如图:

import com.sun.awt.AWTUtilities;import javax.swing.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;public class Demo {    public static void main(String[] args) {        int arc = 100;        int jfW = 400,jfH = 400;        JFrame jf = new JFrame();        roundRectPanel jp = new roundRectPanel(arc,jfW,jfH);        jp.setLayout(null);        jp.setOpaque(false);//Panel设置为透明        jp.setBorder(new roundRectBorder(Color.BLUE,arc));        JButton bt = new JButton("退出");        bt.setBounds(jfW/2-30,jfH/2-15,60,30);        bt.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent e) {                System.exit(0);            }        });        jp.add(bt);        jf.add(jp);        jf.setUndecorated(true);//去掉标题栏        jf.setBounds(300,200,jfW,jfH);        AWTUtilities.setWindowOpaque(jf, false);//JFrame设置为透明        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        jf.setVisible(true);    }}

import javax.swing.*;import java.awt.*;public class roundRectPanel extends JPanel {        public int arc,jfW,jfH;        public roundRectPanel(){            super();        }        public roundRectPanel(int arc,int jfW,int jfH){            this.arc = arc;            this.jfW = jfW;            this.jfH = jfH;        }        @Override        protected void paintComponent(Graphics g) {            ((Graphics2D)g).setPaint(new GradientPaint(0,0,Color.WHITE,0,jfH,Color.BLUE));            g.fillRoundRect(0,0,jfW,jfH,arc,arc);            super.paintComponent(g);        }}


import javax.swing.border.Border;import java.awt.*;public class roundRectBorder implements Border {    Color color;    int arc;    public roundRectBorder(){        super();        this.color = Color.RED;    }    public roundRectBorder(Color color,int arc){        this.color = color;        this.arc = arc;    }    @Override    public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {        ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);        g.setColor(color);        ((Graphics2D) g).setStroke(new BasicStroke(3.0f));        g.drawRoundRect(0,0,c.getWidth()-1,c.getHeight()-1,arc,arc);    }    @Override    public Insets getBorderInsets(Component c) {        return new Insets(0,0,0,0);    }    @Override    public boolean isBorderOpaque() {        return false;    }}







原创粉丝点击