2
我有一個Swing JFrame,它有一堆菜單項。下面的代碼嘗試但未能在選擇「添加地址」菜單項時設置JFrame的內容。擺動菜單項選擇不設置contentpane中的內容
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());
}
當我選擇「添加地址」菜單項時,框架變爲空白。不是我所期望的。我使用斷點進行調試,並看到設置地址的方法正在被調用。 我想不通它爲什麼不起作用。難道有人指出我在這裏做錯了什麼嗎?
「這是因爲您在行(A)上聲明瞭JFrame,因此它只在該方法或構造函數中可見。」我沒有發佈上面的完整代碼。但CardLayout解決了我的問題。感謝您的解決方案。 – sachinrahulsourav 2012-04-14 16:10:55
@sachinrahulsourav:歡迎您和CardLayout使用我認爲最好的工具。 – 2012-04-14 16:13:38