gpt4 book ai didi

java - JComponent 不会重绘

转载 作者:太空宇宙 更新时间:2023-11-04 07:41:37 24 4
gpt4 key购买 nike

我有一个如下所示的 JTabbedPane:

overview= new JTabbedPane();

JComponent accountinfo= AccountOverview(guest.toString());
overview.addTab ("Account Overview", accountinfo);
overview.setMnemonicAt(0, KeyEvent.VK_1);

JComponent flightoption= FlightOptions();
overview.addTab ("Book a Flight",flightoption);
overview.setMnemonicAt(1, KeyEvent.VK_2);

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);

我的 AccountOverview 方法如下所示:

protected JComponent AccountOverview (String text)
{
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");
name2 = JOptionPane.showInputDialog(nameframe, "Change name to: ");
guest.setName(name2);
}
});
JButton editGender= new JButton ("Edit Gender");
editGender.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
JFrame genderframe= new JFrame("Gender Edit");
gen2 = JOptionPane.showInputDialog(genderframe, "Change gender to: ");
guest.setGender(gen2);
}
});
JButton editBirthday= new JButton ("Edit Birthday");
editBirthday.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
JFrame birthdayframe= new JFrame("Birthdate Edit");
birthday2 = JOptionPane.showInputDialog(birthdayframe, "Change birthdate to: ");
guest.setBirthDate(birthday2);
}
});
JButton editPassportNumber= new JButton ("Edit Passport Number");
editPassportNumber.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
JFrame passportframe= new JFrame("Passport Number Edit");
passnum2= Integer.parseInt(JOptionPane.showInputDialog(passportframe, "Change passport number to: "));
guest.setPassportNumber(passnum2);
}
});

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 上显示的 JLabel 在单击“编辑姓名”、“编辑性别”等时更新信息。我无法让 JPanel 重新绘制自身。编辑后的代码会是什么样子,以便显示的信息会更新?

最佳答案

这真的很简单。我将使用第一个按钮进行演示。

final JLabel filler = new JLabel(text);

变量必须声明为最终变量才能在匿名 ActionListener 中使用它。

JButton editName= new JButton ("Edit Name");
editName.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
JFrame nameframe= new JFrame("Name Edit");
name2 = JOptionPane.showInputDialog(nameframe, "Change name to: ");
guest.setName(name2);
filler.setText(guest.toString())
}
});

如果您在监听器内调用 setText,JLabel 中的信息将自动更新。

关于java - JComponent 不会重绘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15992415/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com