Java I';我们已经将JPanel添加到JFrame,但它仍然不是';t显示
所以这个问题被问了很多,但我已经搜索了其中的几个,似乎每个人都忘记了将面板添加到框架中。我已经将面板添加到我的框架中,但是我仍然没有看到我的JPanelJava I';我们已经将JPanel添加到JFrame,但它仍然不是';t显示,java,swing,jframe,jpanel,Java,Swing,Jframe,Jpanel,所以这个问题被问了很多,但我已经搜索了其中的几个,似乎每个人都忘记了将面板添加到框架中。我已经将面板添加到我的框架中,但是我仍然没有看到我的JPanel public class AddSomethingFrame extends JFrame{ private JFrame application; JPanel viewPanel = new JPanel(); public AddSomethingFrame(JFrame application) { super
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中移动。我尝试过改变每个面板的大小,也尝试过改变面板的边框布局,但是没有任何效果。请帮忙 我建议您避免使用“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);
}
}
我们经常告诉这里的人们,
panel.setLayout(null)
是危险的代码,但不幸的是没有足够的听力。请成为例外,倾听并相信。还要了解这对您的JPanel的首选大小有什么影响。那么,您在哪里要求将面板连接到gui??你的主要问题似乎是这个,有点粗心的错误。您正在添加viewpanel,但没有添加panel。AddSomethingFrame
是一个JFrame。无需将其引用传递给一个。删除应用程序
。