所以这个问题问了很多,但是我搜索了其中的几个,似乎每个人都忘记了将面板添加到框架中。我已将面板添加到框架中,但是仍然看不到JPanel。
public class AddSomethingFrame extends JFrame{
private JFrame application;
JPanel viewPanel = new JPanel();
public AddSomethingFrame(JFrame application) {
super("Add");
this.application = application;
setDefaultCloseOperation(EXIT_ON_CLOSE);
setPreferredSize(new Dimension(300, 200));
placeComponents(viewPanel);
viewPanel.setBorder(new EmptyBorder(13, 25, 13, 25));
setLayout(new FlowLayout());
setLocationRelativeTo(null);
setResizable(true);
pack();
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
application.setVisible(true);
setVisible(false);
dispose();
}
});
setVisible(true);
}
private void placeComponents (JPanel panel) {
panel.setLayout(null);
JLabel nameLabel = new JLabel("Name");
nameLabel.setBounds(10, 10, 80, 25);
panel.add(nameLabel);
JTextField nameText = new JTextField(20);
nameText.setBounds(100, 10, 160, 25);
panel.add(nameText);
JLabel brandLabel = new JLabel("Brand");
brandLabel.setBounds(10, 40, 80, 25);
panel.add(brandLabel);
JTextField brandText = new JTextField(20);
brandText.setBounds(100, 40, 160, 25);
panel.add(brandText);
JLabel costLabel = new JLabel("Cost");
costLabel.setBounds(10, 10, 80, 25);
panel.add(costLabel);
JTextField costText = new JTextField(20);
costText.setBounds(100, 10, 160, 25);
panel.add(costText);
JButton storeGearButton = new JButton("Store");
storeGearButton.setBounds(10, 80, 80, 25);
panel.add(storeGearButton);
this.add(viewPanel, BorderLayout.NORTH);
}
}
我已经尝试过为JFrame和JPanel设置setVisible。我尝试更改每个面板的大小,并尝试更改面板的BorderLayout,但是没有任何效果。请帮忙。
答案 0 :(得分:0)
我建议您避免使用“ placeComponents(viewPanel)”构造,而应将“ JPanle panel”元素直接添加到JFrame中,这将使您的代码更容易。像这样的东西:
public class AddSomethingFrame extends JFrame{
static JFrame application;
//JPanel viewPanel = new JPanel();
public AddSomethingFrame(JFrame application) {
super("Add");
// this.application = application;
setBounds(300, 200, 1000, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
// placeComponents(viewPanel);
// viewPanel.setBorder(new EmptyBorder(10, 25, 2, 25));
// setLayout(new FlowLayout());
// setLocationRelativeTo(null);
// setResizable(true);
// pack();
// setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
// this.addWindowListener(new WindowAdapter() {
// public void windowClosing(WindowEvent e) {
// application.setVisible(true);
// setVisible(false);
// dispose();
// }
// });
// setVisible(true);
//}
//
// private void placeComponents (JPanel panel) {
JPanel panel = new JPanel();
// panel.setLayout(null);
JLabel nameLabel = new JLabel("Name");
nameLabel.setBounds(10, 10, 80, 25);
panel.add(nameLabel);
JTextField nameText = new JTextField(20);
nameText.setBounds(100, 10, 160, 25);
panel.add(nameText);
JLabel brandLabel = new JLabel("Brand");
brandLabel.setBounds(10, 40, 80, 25);
panel.add(brandLabel);
JTextField brandText = new JTextField(20);
brandText.setBounds(100, 40, 160, 25);
panel.add(brandText);
JLabel costLabel = new JLabel("Cost");
costLabel.setBounds(10, 10, 80, 25);
panel.add(costLabel);
JTextField costText = new JTextField(20);
costText.setBounds(100, 10, 160, 25);
panel.add(costText);
JButton storeGearButton = new JButton("Store");
storeGearButton.setBounds(10, 80, 80, 25);
panel.add(storeGearButton);
this.add(panel, BorderLayout.NORTH);
}
public static void main(String[] args) {
AddSomethingFrame app= new AddSomethingFrame(application);
app.setVisible(true);
}
}