2017-10-14 146 views
2

我想創建圖形控制檯,它將向用戶提供關於程序當前狀態的信息。我已經計劃爲它使用JTextArea,但是我對append()方法有問題。即使在主類中使用它,我仍然有空的JTextArea。我究竟做錯了什麼?Append()在JTextArea元素上不起作用

這裏是控制檯的GUI的代碼:

package com.meh; 

import javax.swing.*; 

public class Controller extends JFrame { 
    public JPanel ControlPanel; 
    public JTextArea Log; 

    static void setView() { 
     JFrame frame = new JFrame("Controller"); 
     frame.setContentPane(new Controller().ControlPanel); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setVisible(true); 
    } 
} 

這是主類的代碼:

package com.meh; 

public class Main { 
    public static void main(String[] args) { 
    Controller controller = new Controller(); 
     controller.setView(); 
     controller.Log.append("Hello"); 
    } 
} 

回答

2

如果你查查jTextArea append方法,你會看到:

Appends the given text to the end of the document 

但是,如果String是emp,它就不會執行任何操作ty或null

你可以使用setText()你的情況。

+0

嗯...我已經改變append()setText(),但我仍然有同樣的問題。 – Halep

+0

我在我的手機上,但它看起來像你可能沒有將文本區域添加到jframe – notyou

+0

或可能初始化jTextArea – notyou

1

如果您致電getText()是否返回新的字符串值?如果是這樣,您可能需要在更改文本後在controller和/或controller.Log上撥打repaint()和/或revalidate()

1

正如我所看到的,您從不初始化'ControlPanel',它將始終爲空,因此您無法對其執行任何操作。