2014-12-03 132 views
1

这是我正在努力的代码。它拒绝用新文本修改JTextArea。我创建窗口并将其设置为在项目的主要功能中可见。 提前致谢。用JTextArea的append方法挣扎

编辑: 通过拒绝,我的意思是JTextArea将不会显示文本。它只是空着。我没有得到和错误或例外。这是合乎逻辑的。

class Window extends JFrame{ 

protected JTextArea text; 

public Window() { 

    setTitle("Create a list of names"); 
    setSize(500,400); 
    Container containerPane = getContentPane(); 
    JPanel jp = new JPanel(); 

    text = new JTextArea(10,50); 
    text.setPreferredSize(new Dimension(256,256)); 
    text.setEditable(false); 

    JScrollPane scrollText = new JScrollPane(text); 
    scrollText.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); 
    jp.add(scrollText); 

    containerPane.add(jp, BorderLayout.CENTER); 

    text.append("Test"); 

} 

public static void main(String[] args) { 
     Window w = new Window(); 
     w.setVisible(true); 
} 

}

+0

你是什么意思拒绝?它是否指向任何错误或异常? – 2014-12-03 18:41:36

+1

发布代码对我来说看起来很合理。发布你的[SSCCE](http://sscce.org/)来证明问题。由于main()方法中存在您的当前代码,因此它不可编译或可执行。另外,不要使用setPrefferedSize()。每个组件都将确定自己的首选大小,布局管理器将使用此信息来正确定位/调整组件的大小。 – camickr 2014-12-03 18:43:39

+0

对不起。通过拒绝,我的意思是JTextArea不会显示文本。它只是空着。我没有得到和错误或豁免。这是合乎逻辑的。 – 2014-12-03 18:54:23

回答

4

50列的宽度是比帧的宽度大,以便所添加的文本显示屏幕外。降低其值以适应父窗口

textArea = new JTextArea(10, 35); 

请勿使用setPrerredSize。让布局管理器完成其工作,并在添加完所有组件后调用pack

+0

谢谢,这完全解决了它 – 2014-12-03 19:21:44