Java rounded corners on JFrame?

╄→гoц情女王★ 提交于 2019-11-27 18:41:25

问题


I have a login JFrame for my application and i would like to make the corners of the frame rounded by a few pixles.

Id like to do this without using transparency on the JFrame and having to use a background image inside a JPanel - is this possible?


回答1:


It's possible with undecorated frame, consider the following example:

JFrame frame = new JFrame();
frame.setUndecorated(true);
frame.setShape(new RoundRectangle2D.Double(10, 10, 100, 100, 50, 50));
frame.setSize(300, 200);
frame.setVisible(true);

This code works on Java 7. For Java 6 (since update 10) you can do the same with AWTUtilities.setWindowShape:

JFrame frame = new JFrame();
frame.setUndecorated(true);
AWTUtilities.setWindowShape(frame, new RoundRectangle2D.Double(10, 10, 100, 100, 50, 50));
frame.setSize(300, 200);
frame.setVisible(true);



回答2:


Try this. It works :)

yourframe.setUndecorated(true);
yourframe.setBackground(new Color(0, 0, 0, 180));
yourframe.addComponentListener(new ComponentAdapter() {
               @Override
                public void componentResized(ComponentEvent e) {
                    setShape(new RoundRectangle2D.Double(0, 0, getWidth(), getHeight(), 80, 80));
                }
            });


来源:https://stackoverflow.com/questions/18935558/java-rounded-corners-on-jframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!