Java 将面板添加到框架,但在应用程序运行时不显示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18950065/
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
Adding panels to frame, but not showing when app is run
提问by Joshua Martin
I am crating an app with two panels in the frame along with a menu bar along the top. The menu bar shows up just fine, and any actions set so far work, but the other two panels never appear.
我正在创建一个应用程序,框架中有两个面板,顶部有一个菜单栏。菜单栏显示得很好,到目前为止设置的任何操作都可以工作,但其他两个面板从未出现。
I have tried retracing all the panels and lines that add them on to the frame and cannot find any errors.
我试过追溯所有将它们添加到框架上的面板和线条,但找不到任何错误。
The first of the two panes that do not show, form in the drawForm() method, did show before I added some components, but since have not shown even when I remove the components again.
没有显示的两个窗格中的第一个,即 drawForm() 方法中的表单,在我添加一些组件之前确实显示了,但是因为即使我再次删除组件也没有显示。
Here is the class Frame:
这是 Frame 类:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Frame {
public static void drawFrame(){
// Create frame
JFrame frame = new JFrame("Frame");
// Set default close operation
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Set frame attributes
frame.setVisible(true);
frame.setSize(400, 300);
frame.setResizable(false);
// Set Layout
frame.setLayout(new BorderLayout());
// Add Components
frame.add(drawMenuBar(), BorderLayout.NORTH);
JPanel twinPane = new JPanel();
frame.add(twinPane, BorderLayout.CENTER);
twinPane.setLayout(new GridLayout(1, 2));
twinPane.add(drawForm());
twinPane.add(drawInfo());
} // Ends method drawFrame
public static JMenuBar drawMenuBar(){
//Create menu structure
JMenuBar menu = new JMenuBar();
JMenu file = new JMenu("File");
JMenuItem clear = new JMenuItem("Clear");
JMenuItem calculate = new JMenuItem("calculate");
JMenuItem exit = new JMenuItem("Exit");
JMenu help = new JMenu("Help");
JMenuItem about = new JMenuItem("About");
JMenuItem instructions = new JMenuItem("Instructions");
//Add menu items to repective area of menu tree
menu.add(file);
file.add(clear);
file.add(calculate);
file.add(exit);
menu.add(help);
help.add(about);
help.add(instructions);
//Add ActionListener
exit.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
MainApp app = new MainApp();
app.exit();
}
});
//Set Return
return menu;
} // Ends method drawMenuBar
public static JPanel drawForm(){
//Set panel with layout and border
JPanel form = new JPanel();
form.setBorder(BorderFactory.createTitledBorder("Form"));
form.setLayout(new BoxLayout(form, BoxLayout.PAGE_AXIS));
//Create field and labels for form panel and add to form panel
JLabel label1 = new JLabel("text1");
form.add(label1);
JTextField textfield1 = new JTextField(5);
form.add(textfield1);
JLabel label2 = new JLabel("text2");
form.add(label2);
JTextField textfield2 = new JTextField(5);
form.add(textfield2);
JButton calc = new JButton("calculate");
form.add(calc);
JTextField result = new JTextField(5);
form.add(result);
//Set return
return form;
} // Ends method drawForm
public static JPanel drawInfo(){
//Set Panel with layout and border
JPanel info = new JPanel();
info.setBorder(BorderFactory.createTitledBorder("Information"));
//Set Return
return info;
} // Ends method drawInfo
} // Ends class Frame
The main method is in another class, but the class Frame creates the GUI. The frame along with the menu bar work perfectly, but everything after that does nothing.
main 方法在另一个类中,但 Frame 类创建了 GUI。框架与菜单栏一起工作得很好,但之后的一切都不起作用。
I appreciate any help, thank you
我感谢任何帮助,谢谢
Josh
乔希
采纳答案by MadProgrammer
When you add components to a container you may need to invalidate the container hierarchy in order to get them to become visible...
当您将组件添加到容器时,您可能需要使容器层次结构无效以使它们可见...
The problems is as highlight when you set the frame visible BEFORE you've added anything to it...
在向其添加任何内容之前将框架设置为可见时,问题就像突出显示...
public static void drawFrame(){
// Create frame
JFrame frame = new JFrame("Frame");
// Set default close operation
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Set frame attributes
// !! Don't do this here...
//frame.setVisible(true);
// ... IMHO, better to use pack...
//frame.setSize(400, 300);
frame.setResizable(false);
// Set Layout
frame.setLayout(new BorderLayout());
// Add Components
frame.add(drawMenuBar(), BorderLayout.NORTH);
JPanel twinPane = new JPanel();
frame.add(twinPane, BorderLayout.CENTER);
twinPane.setLayout(new GridLayout(1, 2));
twinPane.add(drawForm());
twinPane.add(drawInfo());
// !! Do it here instead...
frame.pack();
frame.setVisible(true);
} // Ends method drawFrame
回答by Hovercraft Full Of Eels
You're setting the JFrame visible beforeyou've added the components. Don't do that. Add the components, pack()
your JFrame, and only then set it visible.
在添加组件之前,您将 JFrame 设置为可见。不要那样做。添加组件,pack()
您的 JFrame,然后才将其设置为可见。
But regardless of any answers you might receive, your question has been asked manytimes before, including: Why shouldn't I call setVisible(true) before adding components?. Voting to close as a duplicate since the question is unlikely to help future visitors.
但是不管您可能会收到什么答案,您的问题之前已经被问过很多次,包括:为什么我不应该在添加组件之前调用 setVisible(true)?. 由于该问题不太可能对未来的访问者有帮助,因此投票以重复结束。
回答by meng hang
Agree with @MadProgrammer. You should put the codes--frame.pack(); frame.setVisible(true)
-- on bottom.
同意@MadProgrammer。你应该把代码frame.pack(); frame.setVisible(true)
————放在底部。