2014-10-27 86 views
0

我有兩個JFrames A和B. B有一個textArea。我想 添加一些文字在B中的TextArea點擊這裏A.一個按鈕是我的代碼:如何通過另一個JFrame修改JFrame?

JButton btn = new JButton("Text"); 
btnButton_1.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 
     b.textArea.setText("Button clicked"); 
    } 
}); 
btnButton_1.setBounds(10, 45, 89, 23); 
frmA.getContentPane().add(btnButton_1); 
  • b爲B類的對象
  • textarea裏的JTextArea變量名B級
+0

嘗試調用'重繪()''中添加B'文本到JTextArea中 – 2014-10-27 11:50:54

+1

不重繪anythign,爲更好地幫助越早張貼SSCCE後/ MCVE短,可運行,編譯 – mKorbel 2014-10-27 11:51:32

+0

也可以使用b。 textArea.append(「你的文字」); – AJJ 2014-10-27 12:00:43

回答

0

使用repaint()方法組件。

JButton btn = new JButton("Text"); 
     btnButton_1.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       b.textArea.setText("Button clicked"); 
       b.frameB.repaint(); 
      } 
     }); 
     btnButton_1.setBounds(10, 45, 89, 23); 
     frmA.getContentPane().add(btnButton_1); 
+0

什麼是repaint()方法。當我按(。)時不顯示? – user3417593 2014-10-27 12:02:45

+0

@ user3417593 - 請參閱我更新的帖子。你應該重新畫框本身。 – shepard23 2014-10-27 12:34:36

+0

我重畫框架,但同樣的事情不起作用。我也嘗試重新繪製textArea。再次不起作用。你試過這個嗎? – user3417593 2014-10-27 19:50:23