如何删除JFrame中的标题栏

发布于 2024-12-23 13:20:38 字数 857 浏览 0 评论 0原文

我使用以下代码进行练习,

http:// docs.oracle.com/javase/tutorial/uiswing/examples/layout/BorderLayoutDemoProject/src/layout/BorderLayoutDemo.java

我还添加了

frame.setSize(frame.getMaximumSize());

createAndShowGUI()方法,

而且我希望这个窗口没有标题栏、关闭和最小化按钮。

我尝试了以下代码,

frame.setUndecorated(true);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

如果我在 pack() 之前添加此代码,它将进入无限循环并出现此异常 线程“AWT-EventQueue-0”java.lang.NegativeArraySizeException 中的异常

如果我添加createAndShowGUI() 方法的最后一行它抛出 线程“AWT-EventQueue-0”中的异常 java.awt.IllegalComponentStateException: The Frame is displayedable.

我应该做什么?

谢谢。

I'm using the following code for practising,

http://docs.oracle.com/javase/tutorial/uiswing/examples/layout/BorderLayoutDemoProject/src/layout/BorderLayoutDemo.java

I also add

frame.setSize(frame.getMaximumSize());

in createAndShowGUI() method,

What is more I want this window not to have the title bar, close and minimize buttons.

I tried the following code,

frame.setUndecorated(true);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

If I added this code before the pack(), it goes into infine loop with this exception Exception in thread "AWT-EventQueue-0" java.lang.NegativeArraySizeException

If I added the last line of createAndShowGUI() method it throws Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: The frame is displayable.

What should I do ?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

比忠 2024-12-30 13:20:38

可以通过调用 来删除标题栏FrameJFrame 实例上的 setUndecorated(true),即通过将“undecorated”属性设置为 true< /代码>。这将删除标题栏周围的框架。

以下是该问题所需的代码:

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Already there
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setUndecorated(true); // <-- the title bar is removed here

在打包的 JFrame 上,

必须在 pack()setUndecorated(true) > 被直接或间接调用,例如通过setVisible(true)

显然,在执行打包后不可能删除标题栏,这是 Frame#setDecolated(boolean)

框架可以使用setUndecorated关闭其本机装饰(即框架和标题栏)。这只能在框架不可显示时完成。如果框架可显示,该方法将抛出运行时异常:IllegalComponentStateException

但是,您可以在FrameWindow#dispose() > 或 JFrame ,然后再次打包,这会导致 JFrame 内组件的布局被刷新。

The title bar can be removed by calling setUndecorated(true) on the Frame or JFrame instance, i.e. by setting the "undecorated" property to true. This removes both the title bar and the surrounding frame.

Here's the required code for the question:

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Already there
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setUndecorated(true); // <-- the title bar is removed here

On a packed JFrame

setUndecorated(true) must be called before pack() is being called, either directly or indirectly, for instance through setVisible(true).

Apparently it is not possible to remove the title bar after the packing is performed, this is the documentation of Frame#setDecorated(boolean):

A frame may have its native decorations (i.e. Frame and Titlebar) turned off with setUndecorated. This can only be done while the frame is not displayable. The method will throw a runtime exception: IllegalComponentStateException if the frame is displayable.

However, you can call Window#dispose() on the Frame or JFrame and then pack it again, which causes the layout of the components within the JFrame to be refreshed.

独﹏钓一江月 2024-12-30 13:20:38

好吧, createAndShowGUI() 中的以下代码片段对我有用:

JFrame frame = new JFrame("BorderLayoutDemo");
frame.setUndecorated(true); // Remove title bar
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);

请注意,我不确定您想要通过手动将未实现的框架的大小设置为其最大大小来实现什么目的,这最初将为 (0, 0)

Well, the following code snippet in createAndShowGUI() worked for me:

JFrame frame = new JFrame("BorderLayoutDemo");
frame.setUndecorated(true); // Remove title bar
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);

Note that I'm not sure what you're trying to achieve by manually setting the size of an unrealized frame to it's maximum size, which will be (0, 0) initially.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文