我正在创建一个应用程序,框架中有两个面板,顶部有一个菜单栏。菜单栏显示得很好,到目前为止设置的任何操作都有效,但其他两个面板从未出现。
我尝试追溯所有将它们添加到框架上的面板和线条,但找不到任何错误。
两个不显示的窗格中的第一个,drawForm() 方法中的表单,在我添加一些组件之前确实显示了,但是即使我再次删除组件也没有显示。
这是框架类:
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
主要方法在另一个类中,但是 Frame 类创建了 GUI。框架和菜单栏完美地工作,但之后的一切都不起作用。
感谢您的帮助,谢谢
乔什