按下按钮时将文本附加到 JTextArea?

发布于 2024-12-10 11:52:54 字数 3156 浏览 0 评论 0原文

我有一个简单的 Swing GUI,我想在按下按钮后向 JTextArea 添加一行新文本,很简单吧?

Button 及其 ActionListener 功能正确(将内容打印到控制台工作正常),但是当我使用 .append() 或 .setText() 将文本添加到文本区域时,出现空指针异常。

一如既往,在下面编写代码。任何意见将不胜感激,谢谢!

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.LineBorder;

public class GUI extends JFrame implements ActionListener {

private JFrame frame;
private JLabel paneHeader;
public JTextArea ipArea, portArea, outputLog, orderLog, cookLog;
private JButton newServer;

public String ipAddress, portNumber, cashierName, cookName;

public GUI() {
    initGUI();
}

public void initGUI() {

    frame  = new JFrame("Restaurant Overview");
    Container contentPane = frame.getContentPane(); 

    JLabel paneHeader = new JLabel("Restaurant Monitoring System");
    paneHeader.setBounds(200, 0, 200, 25);
    paneHeader.setFont(new Font("Calibri", Font.BOLD, 14));

    JLabel ipLabel = new JLabel("IP Address: ");
    ipLabel.setBounds(25, 30, 75, 20);
    ipLabel.setFont(new Font("Calibri", Font.PLAIN, 12));

    final JTextArea ipArea = new JTextArea();
    ipArea.setBorder(new LineBorder(Color.GRAY));
    ipArea.setBounds(105, 30, 100, 20);

    JLabel portLabel = new JLabel("Port Number: ");
    portLabel.setBounds(25, 55, 75, 20);
    portLabel.setFont(new Font("Calibri", Font.PLAIN, 12));

    final JTextArea portArea = new JTextArea();
    portArea.setBorder(new LineBorder(Color.GRAY));
    portArea.setBounds(105, 55, 100, 20);

    JButton newServer = new JButton("Create new Server");
    newServer.setBorder(new LineBorder(Color.GRAY));
    newServer.setBounds(250, 30, 150, 40);
    newServer.setActionCommand("createserver");
    newServer.addActionListener(this);

    JTextArea outputLog = new JTextArea(" ");
    outputLog.setBorder(new LineBorder(Color.GRAY));
    outputLog.setBounds(25, 90, 150, 150);
    //outputLog.setEditable(false);  

    JTextArea cashierLog = new JTextArea();
    cashierLog.setBorder(new LineBorder(Color.GRAY));
    cashierLog.setBounds(185, 90, 150, 150);
    //cashierLog.setEditable(false);  

    JTextArea cookLog = new JTextArea();
    cookLog.setBorder(new LineBorder(Color.GRAY));
    cookLog.setBounds(345, 90, 150, 150);
    //cookLog.setEditable(false);  

    contentPane.add(paneHeader);
    contentPane.add(ipLabel);
    contentPane.add(ipArea);
    contentPane.add(portLabel);
    contentPane.add(portArea);
    contentPane.add(outputLog);
    contentPane.add(cashierLog);
    contentPane.add(cookLog);

    contentPane.add(newServer);

    frame.setLayout(null);
    frame.pack();
    frame.setSize(600,400);
    frame.setVisible(true); 
}

public void test() {
    //ipAddress = ipArea.getText() + "\n";
    //portNumber = portArea.getText() + "\n";
    String text = "lemons";
    //System.out.println(text);
    outputLog.append(text);
    //outputLog.append(portNumber);
}


public void actionPerformed(ActionEvent e) {
    if ("createserver".equals(e.getActionCommand())) {
        //test();
        outputLog.append("lemons");
    } else {
        //Do Nothing
    }
}
}

I've got a simple Swing GUI and I want to add a new line of text to a JTextArea after a button is pressed, simple right?

The Button and it's ActionListener function correctly (printing stuff to the console works fine), but when I use .append()or .setText() to add text to the textarea, I get a nullpointer exception.

As always, code it below. Any input would be greatly appreciated, thanks!!!

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.LineBorder;

public class GUI extends JFrame implements ActionListener {

private JFrame frame;
private JLabel paneHeader;
public JTextArea ipArea, portArea, outputLog, orderLog, cookLog;
private JButton newServer;

public String ipAddress, portNumber, cashierName, cookName;

public GUI() {
    initGUI();
}

public void initGUI() {

    frame  = new JFrame("Restaurant Overview");
    Container contentPane = frame.getContentPane(); 

    JLabel paneHeader = new JLabel("Restaurant Monitoring System");
    paneHeader.setBounds(200, 0, 200, 25);
    paneHeader.setFont(new Font("Calibri", Font.BOLD, 14));

    JLabel ipLabel = new JLabel("IP Address: ");
    ipLabel.setBounds(25, 30, 75, 20);
    ipLabel.setFont(new Font("Calibri", Font.PLAIN, 12));

    final JTextArea ipArea = new JTextArea();
    ipArea.setBorder(new LineBorder(Color.GRAY));
    ipArea.setBounds(105, 30, 100, 20);

    JLabel portLabel = new JLabel("Port Number: ");
    portLabel.setBounds(25, 55, 75, 20);
    portLabel.setFont(new Font("Calibri", Font.PLAIN, 12));

    final JTextArea portArea = new JTextArea();
    portArea.setBorder(new LineBorder(Color.GRAY));
    portArea.setBounds(105, 55, 100, 20);

    JButton newServer = new JButton("Create new Server");
    newServer.setBorder(new LineBorder(Color.GRAY));
    newServer.setBounds(250, 30, 150, 40);
    newServer.setActionCommand("createserver");
    newServer.addActionListener(this);

    JTextArea outputLog = new JTextArea(" ");
    outputLog.setBorder(new LineBorder(Color.GRAY));
    outputLog.setBounds(25, 90, 150, 150);
    //outputLog.setEditable(false);  

    JTextArea cashierLog = new JTextArea();
    cashierLog.setBorder(new LineBorder(Color.GRAY));
    cashierLog.setBounds(185, 90, 150, 150);
    //cashierLog.setEditable(false);  

    JTextArea cookLog = new JTextArea();
    cookLog.setBorder(new LineBorder(Color.GRAY));
    cookLog.setBounds(345, 90, 150, 150);
    //cookLog.setEditable(false);  

    contentPane.add(paneHeader);
    contentPane.add(ipLabel);
    contentPane.add(ipArea);
    contentPane.add(portLabel);
    contentPane.add(portArea);
    contentPane.add(outputLog);
    contentPane.add(cashierLog);
    contentPane.add(cookLog);

    contentPane.add(newServer);

    frame.setLayout(null);
    frame.pack();
    frame.setSize(600,400);
    frame.setVisible(true); 
}

public void test() {
    //ipAddress = ipArea.getText() + "\n";
    //portNumber = portArea.getText() + "\n";
    String text = "lemons";
    //System.out.println(text);
    outputLog.append(text);
    //outputLog.append(portNumber);
}


public void actionPerformed(ActionEvent e) {
    if ("createserver".equals(e.getActionCommand())) {
        //test();
        outputLog.append("lemons");
    } else {
        //Do Nothing
    }
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

随波逐流 2024-12-17 11:52:54

您可能会隐藏一个变量——多次声明它,但初始化局部变量而不是类字段,因此类字段仍然为空。

编辑:
是的,你当然知道。在构造函数中,

JTextArea outputLog = new JTextArea(" ");

This 重新声明了 outputLog 变量,因此您仅初始化构造函数的本地变量。解决方案不是重新声明变量,而是初始化类字段。因此,将上面的内容更改为

outputLog = new JTextArea(" ");

您将需要对需要在类范围内访问的每个变量执行此操作。对于那些可以在本地声明的,可以这样做,但为了安全起见,请删除其相应的类声明,以免将来出现导致相同错误的风险。

You are likely shadowing a variable -- declaring it more than once, but initializing a local variable not the class field, and so the class field remains null.

Edit:
Yep, sure enough you do. In your constructor you have

JTextArea outputLog = new JTextArea(" ");

This re-declares the outputLog variable, and so you are initializing only the variable local to the constructor. The solution is not to redeclare the variable but instead initialize the class field. So change the above to

outputLog = new JTextArea(" ");

You will need to do this for every variable that needs to be accessed in the class scope. For those that are OK to declare locally, do so, but in the sake of safety, get rid of their corresponding class declaration so as not to risk causing the same error in the future.

半葬歌 2024-12-17 11:52:54

您的错误是实例变量outputLog未初始化。

Your error is that the instance variable outputLog is not initialized.

戏蝶舞 2024-12-17 11:52:54

您的代码未在 EDT 上完成,您必须将其包装到稍后调用()

EventQueue.invokeLater(new Runnable() {

    @Override
    public void run() {
       outputLog.setText(outputLog.getText() 
          + System.getProperty("line.separator") + text);
    }
});

your code not done on EDT, you have to wrap tht into invokeLater()

EventQueue.invokeLater(new Runnable() {

    @Override
    public void run() {
       outputLog.setText(outputLog.getText() 
          + System.getProperty("line.separator") + text);
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文