How to remove title bar in JFrame

泪湿孤枕 提交于 2019-12-28 02:06:10

问题


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.


回答1:


    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Already there
    frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    frame.setUndecorated(true);



回答2:


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.



来源:https://stackoverflow.com/questions/8701716/how-to-remove-title-bar-in-jframe

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