简体   繁体   English

挣扎与JTextArea的append方法

[英]Struggling with the append method for JTextArea

This is the code I am struggling with. 这是我在努力的代码。 It is refusing to amend the JTextArea with the new text. 它拒绝用新文本修改JTextArea。 I create the window and set it to visible in the main function of the project. 我创建窗口并将其设置为在项目的主要功能中可见。 Thanks ahead. 谢谢你

EDIT: By refusing, I mean the JTextArea will simply not display the text. 编辑:通过拒绝,我的意思是JTextArea将根本不显示文本。 It just stays empty. 它只是空着。 I'm not getting and error or exception. 我没有得到错误和异常。 It is all logical. 这都是合乎逻辑的。

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);
}

} }

The column width of 50 is greater than the width of the frame so the added text appears offscreen. 列宽50大于框架的宽度,因此添加的文本显示在屏幕外。 Reduce its value to fit the parent window 降低其值以适合父窗口

textArea = new JTextArea(10, 35);

Don't use setPrerredSize . 不要使用setPrerredSize Let the layout manager do its job and call pack after all components have been added. 添加所有组件后,让布局管理器执行其工作并调用pack

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM