我想知道是否有任何以编程方式创建 JFrame 我的意思是哪个是正确的
JFrame frame = new JFrame("title");
frame.pack();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
frame.setSize(250,200);
or
JFrame frame = new JFrame("title");
frame.setSize(250,200);
frame.pack();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
or
JFrame frame = new JFrame("title");
frame.setSize(250,200);
frame.pack();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
现在这段代码可以在简单的程序中正常工作,但是如果我在更大的程序中使用线程怎么办
最佳答案
如果您使用正确的布局管理器并调用
pack()
,则无需调用setSize()
方法。如果您确实需要绝对大小,请覆盖 JComponent#getPreferredSize()JPanel
的方法。了解更多 Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?
添加完所有组件后最后调用
setVisible()
方法。不要直接在顶级容器中添加组件。使用其他容器,例如JPanel
,最后将其添加到JFrame
中。- 调用
setDefaultCloseOperation()
方法的顺序并不重要。
关于java - 正确的打字顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26480850/