我已经在 GUI Designer 的 JPanel 中初始化了我自己的 JPanel,但是当单击按钮时,我似乎仍然无法将此 JTextField 添加到我新创建的 JPanel 中。我没有收到任何错误,我尝试过重新验证、验证、重新绘制等等。我什至按照其他用户的建议将面板的布局设置为 BoxLayout,但这仍然不起作用。
fieldsPanel
是使用 GUI 设计器创建的,但我尝试覆盖它。
panel
是我自己想要添加的代码fieldsPanel
。
public class Form extends JFrame {
private JPanel rootPanel;
private JPanel fieldsPanel;
private JPanel panel;
public Form() {
fieldsPanel = new JPanel();
panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
setContentPane(rootPanel);
pack();
addFieldButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JTextField skuField = new JTextField();
panel.add(skuField);
fieldsPanel.add(panel);
pack();
repaint();
}
});
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}