我已经将 JPanel 添加到 JFrame 中,但它仍然没有显示



所以这个问题被问了很多,但我已经搜索了其中的几个,似乎每个人都忘记了将面板添加到框架中。我已经把面板添加到我的框架中,但我仍然没有看到我的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,但都不起作用。请帮忙。

我建议您避免使用"placeComponents(viewPanel("构造,直接将"JPanlepanel"元素添加到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);
}
}

最新更新