2012-02-08 59 views
-1

我的refresh和refresh2方法導致出現一個新的窗口jpanel。我希望我的textareas能夠在同一個窗口中更新。我不認爲我在調用正確的jpanel。我該如何解決?另外爲什麼它會創建一個新窗口?java gui textarea沒有正確更新

public static void main(String[] args) { 
       MPUComp frame = new MPUComp(); 
       frame.setVisible(true); 

} 
public MPUComp() { 
    setTitle("Mpu Finder"); 
    ImageIcon LoadIco = new ImageIcon(getClass().getResource("load.png")); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setBounds(100, 100, 799, 680); 

    contentPane = new JPanel(); 
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
    setContentPane(contentPane); 
    contentPane.setLayout(null); 
    btnFind1.setBounds(250, 27, 68, 23); 

    contentPane.add(btnFind1); 
    btnLoadMpu.setBounds(328, 27, 36, 22); 
    btnLoadMpu.setIcon(LoadIco); 
    contentPane.add(btnLoadMpu); 
    btnFind2.setBounds(642, 27, 68, 23); 
    contentPane.add(btnFind2); 
    btnLoadMpu2.setBounds(720, 28, 36, 22); 
    btnLoadMpu2.setIcon(LoadIco); 
    contentPane.add(btnLoadMpu2); 
    menu(); 

} 
public void refresh(String pane1) { 
    textArea_1.append(pane1 + "\n"); 
    contentPane.revalidate(); 
    contentPane.repaint(); 
    setVisible(true); 
} 
public void refresh2(String pane1) { 
     textArea_2.append(pane1 + "\n"); 
     contentPane.revalidate(); 
     contentPane.repaint(); 
     setVisible(true); 

} 
+0

請發表您的完整源代碼。你提供的東西根本不調用'refresh'方法 – mbatchkarov 2012-02-08 18:02:53

回答

5
  1. Swing組件必須在事件派發線程中更新
  2. 沒有必要無效容器或發出重畫請求
  3. 不要使用空佈局管理器
  4. 當問這樣的問題時,最好包含一個sscce

這樣的問題幾乎要求50次一天。下一次,請搜索已經得到充分回答的相關項目。

欲瞭解更多信息,請參閱Concurrency in Swing

+0

我試過只用textarea.append使用invokelater,但它不起作用 – 2012-02-08 18:07:32

+1

@CharlieMorrison,爲了更好的幫助,請看第4項。 – mre 2012-02-08 18:08:53

+0

我注意到瞭如果我在main中調用方法,但不能從另一個類中調用該方法,它會起作用 – 2012-02-08 18:26:11