0

我有一个看起来像这样的 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 以便选项卡窗格反映新的更新更改。例如,如果原始显示为“姓名:管理员”,则只需单击“编辑姓名”并输入“John”即可将原始显示更改为“姓名:John”。

编辑:我的 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;
}

如何编辑我的代码?

提前致谢!

4

1 回答 1

0

在:JComponent accountinfo = AccountOverview(guest.toString()); 您处理信息以创建您的组件。

这会生成您的客人信息的副本(通过调用 toString()),以后无法更改。尝试将来宾对象传递给您的组件,并在每次重绘自身时调用 toString()。

在进行编辑后,可以通过调用 accountinfo.repaint() 来让组件自行重绘。

于 2013-04-12T22:23:03.330 回答