我有一个 Swing JFrame,它有一堆菜单项。下面的代码尝试但未能在选择“添加地址”菜单项时设置 JFrame 的内容。
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setVisible(true);
JMenuBar menuBar = new JMenuBar();
JMenu clientMenu = new JMenu("Client");
JMenuItem address = new JMenuItem("Add Address");
clientMenu.add(address);
menuBar.add(clientMenu);
address.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
setupAddressForm();
}
});
private void setupAddressForm() {
frame.getContentPane().removeAll();
JPanel infoPanel = new JPanel();
JLabel infoLabel = new JLabel();
infoLabel.setText("<html><B>Please enter the address below</B></html>");
infoPanel.add(infoLabel, SwingConstants.CENTER);
frame.getContentPane().add(infoPanel, BorderLayout.NORTH);
JPanel buttonPanel = new JPanel();
JButton submitButton = new JButton("Submit Address");
JButton resetButton = new JButton("Reset");
submitButton.setToolTipText("Submit");
resetButton.setToolTipText("Reset");
buttonPanel.add(submitButton);
buttonPanel.add(resetButton);
frame.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
JPanel fieldPanel = new JPanel();
fieldPanel.setLayout(new GridLayout(10, 10));
JLabel houseNumberLabel = new JLabel("House Number",
SwingConstants.LEFT);
final JTextField houseNumberField = new JTextField(3);
JLabel streetNameLabel = new JLabel("Street Name", SwingConstants.LEFT);
final JTextField streetNameField = new JTextField(10);
JLabel localityLabel = new JLabel("Locality", SwingConstants.LEFT);
final JTextField localityField = new JTextField(10);
JLabel cityLabel = new JLabel("City", SwingConstants.LEFT);
final JTextField cityField = new JTextField(10);
JLabel stateLabel = new JLabel("State", SwingConstants.LEFT);
final JTextField stateField = new JTextField(10);
JLabel zipCodeLabel = new JLabel("Zip Code", SwingConstants.LEFT);
final JTextField zipCodeField = new JTextField(10);
fieldPanel.add(houseNumberLabel);
fieldPanel.add(houseNumberField);
fieldPanel.add(streetNameLabel);
fieldPanel.add(streetNameField);
fieldPanel.add(localityLabel);
fieldPanel.add(localityField);
fieldPanel.add(cityLabel);
fieldPanel.add(cityField);
fieldPanel.add(stateLabel);
fieldPanel.add(stateField);
fieldPanel.add(zipCodeLabel);
fieldPanel.add(zipCodeField);
frame.getContentPane().add(fieldPanel, BorderLayout.CENTER);
submitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String houseNumber = houseNumberField.getText();
String street = streetNameField.getText();
String locality = localityField.getText();
String city = cityField.getText();
String state = stateField.getText();
String zipCode = zipCodeField.getText();
Address address = new Address();
if (!"".equals(houseNumber) && !"".equals(street)
&& !"".equals(locality) && !"".equals(city)
&& !"".equals(state) && !"".equals(zipCode)) {
address.setHouseNumber(houseNumber);
address.setStreetName(street);
address.setLocality(locality);
address.setState(state);
address.setZipCode(zipCode);
}
}
});
frame.getContentPane().doLayout();
frame.getContentPane().update(frame.getGraphics());
}
当我选择“添加地址”菜单项时,框架就变成了空白。不是我所期望的。我使用断点进行了调试,发现正在调用设置地址的方法。我不知道为什么它不起作用。如果我在这里做错了什么,有人可以指出吗?