更新一個JComponent我有一個JTabbedPane中,看起來像這樣:在JTabbedPane的
overview= new JTabbedPane();
JComponent accountinfo= AccountOverview(guest.toString());
overview.addTab ("Account Overview", accountinfo);
overview.setMnemonicAt(0, KeyEvent.VK_1);
JFrame tabbed= new JFrame("AIR Reservation");
tabbed.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
tabbed.add(overview);
tabbed.setSize(650,500);
tabbed.setLocationRelativeTo(null);
tabbed.setVisible(true);
}
guest.toString打印出客戶的信息。 我AccountOverview(guest.toString)方法是這樣的:
JPanel panel = new JPanel(false);
JLabel filler = new JLabel(text);
JButton editName= new JButton ("Edit Name");
editName.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
JFrame nameframe= new JFrame("Name Edit");
String name = JOptionPane.showInputDialog(nameframe, "Change name to: ");
guest.setName(name);
}
});
JButton editGender= new JButton ("Edit Gender");
editGender.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
JFrame genderframe= new JFrame("Gender Edit");
String gender = JOptionPane.showInputDialog(genderframe, "Change gender to: ");
guest.setGender(gender);
}
});
JButton editBirthday= new JButton ("Edit Birthday");
editBirthday.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
JFrame birthdayframe= new JFrame("Birthdate Edit");
String birthday = JOptionPane.showInputDialog(birthdayframe, "Change birthdate to: ");
guest.setBirthDate(birthday);
}
});
JButton editPassportNumber= new JButton ("Edit Passport Number");
editPassportNumber.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
JFrame passportframe= new JFrame("Passport Number Edit");
int passnum= Integer.parseInt(JOptionPane.showInputDialog(passportframe, "Change passportnumber to: "));
guest.setPassportNumber(passnum);
}
});
panel.add(editName);
panel.add(editGender);
panel.add(editBirthday);
panel.add(editPassportNumber);
panel.add(destination);
filler.setHorizontalAlignment(JLabel.CENTER);
panel.add(filler);
return panel;
}
我想編輯的JComponent所以選項卡窗格反映新更新的變化。 例如,如果原始顯示是「名稱:管理員」,只需單擊「編輯名稱」並輸入「約翰」即可將原始顯示更改爲「姓名:約翰」。
編輯:我的toString()方法(來自另一個類,客戶)
//Returns the Customer object as a String.
public String toString()
{
customerstring= "<html>Name: " + getName();
customerstring+= "<br>Gender: " + getGender();
customerstring+= "<br>Date of Birth: " + getBirthDate();
customerstring+= "<br>Passport number: " + getPassportNumber();
customerstring+= "<br>Balance: " + money.format(getMoney());
return customerstring;
}
我如何修改我的密碼?
在此先感謝!
@HovercraftFullOfEels對不起,toString方法是另一個類的一部分。我編輯了我的帖子以包含此方法。我最初發布的代碼來自我的GUI類。 – nichi