[英]Append String to textArea doesn't do anything
I am trying to add a String to the textArea which is passed as a parameter using a custom showMessage method. 我试图将一个字符串添加到textArea中,该字符串使用自定义showMessage方法作为参数传递。 The code runs but I don't see any text in the textArea.
该代码运行,但在textArea中看不到任何文本。 What is wrong with it?
怎么了
This is my implementation: 这是我的实现:
import java.awt.*;
import javax.swing.*;
public class FrameDemo extends JPanel {
JTextArea textArea;
private final static String newline = "\n";
public FrameDemo() {
super(new GridBagLayout());
textArea = new JTextArea(5, 20);
textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea);
GridBagConstraints c = new GridBagConstraints();
c.gridwidth = GridBagConstraints.REMAINDER;
c.fill = GridBagConstraints.HORIZONTAL;
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 1.0;
add(scrollPane, c);
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("TextDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new FrameDemo());
frame.pack();
frame.setVisible(true);
}
public static void showMessage(String message){
FrameDemo text = new FrameDemo();
text.textArea = new JTextArea(10,10);
text.textArea.append(message + newline);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
showMessage("Hello!");
showMessage("Hello!");
showMessage("Hello!");
showMessage("Hello!");
}
});
}
}
Every time you do FrameDemo text = new FrameDemo();
每次执行
FrameDemo text = new FrameDemo();
you create a new instance (so a new JTextArea
). 您创建一个新实例(因此是一个新的
JTextArea
)。 Same goes for text.textArea = new JTextArea(10,10);
text.textArea = new JTextArea(10,10);
A possible solution would be: 可能的解决方案是:
// ....
public void showMessage(String message){ // no longer static
this.textArea.append(message + newline);
}
// ....
public static void main(String[] args) {
final FrameDemo text = new FrameDemo();
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
text.showMessage("Hello!");
text.showMessage("Hello!");
text.showMessage("Hello!");
text.showMessage("Hello!");
}
});
}
NB: untested 注意:未经测试
Also note that it's common convention to use uppercase in constants name (so newline
should be NEWLINE
) 另请注意,在常量名称中使用大写字母是常见的约定(因此
newline
应该为NEWLINE
)
Make things simpler, if you don't understand the basics: 如果您不了解基本知识,则可以简化工作:
package framedemo;
import javax.swing.*;
public class FrameDemo {
JTextArea textArea;
private final static String newline = "\n";
public FrameDemo() {
textArea = new JTextArea(5, 20);
textArea.setEditable(false);
}
public void createAndShowGUI() {
JFrame frame = new JFrame("TextDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(textArea);
frame.pack();
frame.setVisible(true);
}
public void showMessage(String message){
textArea.append(message + newline);
}
public static void main(String[] args) {
FrameDemo frameDemo = new FrameDemo();
frameDemo.createAndShowGUI();
frameDemo.showMessage("Hello!");
frameDemo.showMessage("World!");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.