JFrame 上的 Java 圆角?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18935558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 12:39:20  来源:igfitidea点击:

Java rounded corners on JFrame?

javaswingjframerounded-corners

提问by Alosyius

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

我有一个用于我的应用程序的登录 JFrame,我想让框架的角由几个像素四舍五入。

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

我想这样做而不在 JFrame 上使用透明度并且必须在 JPanel 内使用背景图像 - 这可能吗?

回答by Jk1

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:

此代码适用于 Java 7。对于 Java 6(自更新 10 起),您可以使用以下命令执行相同操作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);

回答by Harsha Sampath

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));
                }
            });