Swing 中的 JTextArea 问题
我在更新文本区域时遇到问题。
我在 gui.java
中声明 textArea
:
JTextArea textArea;
我启动 GUI..
public void startGUI() {
// These are all essential GUI pieces
JLabel jLabInstruction, jLaberror;
JLabel copyright = new JLabel("");
JTextField uI = new JTextField("");
JTextArea textArea = new JTextArea("");
JButton jbtnSubmit;
final JFrame jfrm = new JFrame("app name!");
jfrm.setLayout(new FlowLayout());
jfrm.setSize(300, 300);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
textArea = new JTextArea(5, 20);
textArea.setEditable(false);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
jLabInstruction = new JLabel("SYSTEM: Please type in a command: ");
jbtnSubmit = new JButton("Submit");
jLaberror = new JLabel("");
textArea.setMargin(new Insets(10,10,10,10));
jfrm.add(jLaberror);
jfrm.add(textArea);
jfrm.add(jLabInstruction);
jfrm.add(uI);
jfrm.add(jbtnSubmit);
jfrm.add(new JSeparator(SwingConstants.HORIZONTAL));
jfrm.add(copyright);
jfrm.setVisible(true);
}
并且我有一个方法可以写入上面的 textArea
:
public void writeToTextArea(String userInputText) {
textArea.append("\nSYSTEM: "
+ userInputText);
}
另外,在tasks.java中,我可以调用最后一个方法:
gui.writeToTextArea("PROGRAM STARTED!");
我的问题是文本区域字段没有更新。没有输入任何内容。我认为这是因为它找不到 textArea
是什么。我得到一个:
Exception in thread "main" java.lang.NullPointerException
I am having trouble updating a text area.
I declare textArea
in gui.java
:
JTextArea textArea;
I start up the GUI..
public void startGUI() {
// These are all essential GUI pieces
JLabel jLabInstruction, jLaberror;
JLabel copyright = new JLabel("");
JTextField uI = new JTextField("");
JTextArea textArea = new JTextArea("");
JButton jbtnSubmit;
final JFrame jfrm = new JFrame("app name!");
jfrm.setLayout(new FlowLayout());
jfrm.setSize(300, 300);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
textArea = new JTextArea(5, 20);
textArea.setEditable(false);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
jLabInstruction = new JLabel("SYSTEM: Please type in a command: ");
jbtnSubmit = new JButton("Submit");
jLaberror = new JLabel("");
textArea.setMargin(new Insets(10,10,10,10));
jfrm.add(jLaberror);
jfrm.add(textArea);
jfrm.add(jLabInstruction);
jfrm.add(uI);
jfrm.add(jbtnSubmit);
jfrm.add(new JSeparator(SwingConstants.HORIZONTAL));
jfrm.add(copyright);
jfrm.setVisible(true);
}
And I have a method that writes to the textArea
above:
public void writeToTextArea(String userInputText) {
textArea.append("\nSYSTEM: "
+ userInputText);
}
Also, in tasks.java
, I am able to call on this last method:
gui.writeToTextArea("PROGRAM STARTED!");
My problem is that the text area field is not updating. nothing is being inputted. I am thinking it is because it can't find what textArea
is. I am getting an:
Exception in thread "main" java.lang.NullPointerException
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在
startGUI
函数中声明了另一个名为textArea
的变量,该变量隐藏了类级别textArea
。这就是当您稍后尝试在程序中写入文本区域时收到 NPE 的原因。You are declaring another variable called
textArea
in yourstartGUI
function, which is hiding the class leveltextArea
. Which is why you get an NPE when you try to write to the text area later on in your program.