简体   繁体   English

JComponent不会重绘

[英]JComponent Won't Repaint

I have a JTabbedPane that looks like this: 我有一个看起来像这样的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);

my AccountOverview method looks like this: 我的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;
}

I want the JLabel that is displayed on the JComponent on the tab to update the information when "Edit Name", "Edit Gender", etc. is clicked. 我希望单击“编辑名称”,“编辑性别”等时,选项卡JComponent上显示的JLabel更新信息。 I can't get the JPanel to repaint itself. 我无法让JPanel重新绘制自身。 What would the edited code look like so the displayed information would update? 编辑后的代码会是什么样子,以便更新显示的信息?

It's really simple. 真的很简单。 I'll demonstrate with the fisrt button. 我将使用fisrt按钮进行演示。

final JLabel filler = new JLabel(text);

Variable has to be declared final in order to use it inside an anonymous ActionListener. 必须将变量声明为final才能在匿名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())
        }
    });

If you call setText inside the listener the information in your JLabel will update automatically. 如果在侦听器内部调用setText,则JLabel中的信息将自动更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM