2013-04-13 67 views
1

我有一個JTabbedPane中,看起來像這樣:的JComponent不會重新繪製

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重新繪製自己。被編輯的代碼會是什麼樣子,以便顯示的信息會更新?

回答

1

這很簡單。我將用fisrt按鈕進行演示。

final JLabel filler = new JLabel(text); 

爲了在匿名ActionListener中使用它,變量必須聲明爲final。

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中的信息將自動更新。

+0

非常感謝!我一直試圖找出這個問題很長一段時間,我只能找到複雜的答案。你的回答很簡單,但很快解決了我的問題:) – nichi