简体   繁体   English

jTextArea追加无法正常工作

[英]jTextArea append is not working

I have a scrollpane in a panel and a jtextarea under the scrollpane. 我在面板中有一个滚动窗格,在滚动窗格下有一个jtextarea。 jtextarea append is not working jtextarea追加无法正常工作

I am using this for logging purpose. 我将此用于记录目的。

JPanel panel_1 = new JPanel();
tabbedPane.addTab("Logs", null, panel_1, null);
panel_1.setLayout(null);

JScrollPane scrollPane_1 = new JScrollPane();
scrollPane_1.setBounds(0, 0, 672, 303);
panel_1.add(scrollPane_1);

JTextArea jTextArea = new JTextArea(100,200);
jTextArea.setEditable(false);
jTextArea.setVisible(true);
scrollPane_1.add(jTextArea);
jTextArea.append("Hello");
scrollPane_1.add(jTextArea);

Don't add components to a scrollpane. 不要将组件添加到滚动窗格中。 The component needs to be added to the viewport of the scrollpane. 该组件需要添加到滚动窗格的视口。

The easiest way to do this is to use: 最简单的方法是使用:

JTextArea jTextArea = new JTextArea(100,200);
jTextArea.setEditable(false);
jTextArea.setVisible(true);
//scrollPane_1.add(jTextArea);
jTextArea.append("Hello");

JScrollPane scrollPane_1 = new JScrollPane(jTextArea);
scrollPane_1.setBounds(0, 0, 672, 303);
panel_1.add(scrollPane_1);

The other way to do this is to use: 另一种方法是使用:

JScrollPane scrollPane_1 = new JScrollPane();
scrollPane_1.setViewportView( jTextArea );

Also you should note when you create a text area the parameters are row/columns, not pixels so your values are too large. 还应注意,在创建文本区域时,参数是行/列,而不是像素,因此您的值太大。 I would suggest something like: 我建议类似的东西:

//JTextArea jTextArea = new JTextArea(100,200);
JTextArea jTextArea = new JTextArea(30,10);

Finally you should not be setting the bounds of the scrollpane. 最后,您不应该设置滚动窗格的边界。 Swing was designed to be used with layout managers. Swing旨在与布局管理器一起使用。 The layout manager will determine the size of the scrollpane based on the size of the text area: 布局管理器将基于文本区域的大小确定滚动窗格的大小:

//scrollPane_1.setBounds(0, 0, 672, 303);

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

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