我正在尝试在我的 GUI 类中实现一个单例,但是一旦我返回实例,它就会给我一个空指针异常,我一直在查看多个源,但它无法正常工作。
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.SwingUtilities;
import java.util.concurrent.TimeUnit;
/**
* Write a description of class GUI here.
*/
public class GUI extends JFrame
{
JFrame frame = new JFrame("VoidWorld");
JTextArea textarea = new JTextArea(10, 45);
JScrollPane scrollPane = new JScrollPane(textarea);
JTextArea textAreaSide = new JTextArea(10, 15);
JScrollPane scrollPaneSide = new JScrollPane(textAreaSide);
JTextField textfield = new JTextField("", 5);
Color bgcolor = new Color(255, 255, 255);
private static GUI instance;
Parser command;
/**
* Constructor for objects of class GUI
*/
private GUI()
{
command = new Parser();
textarea.setEditable(false);
frame.setSize(900,400);
frame.add(scrollPaneSide, BorderLayout.CENTER);
textAreaSide.setFont(new Font("Helvetica", Font.PLAIN, 20));
textAreaSide.setForeground(Color.red);
textAreaSide.setBorder(BorderFactory.createLineBorder(Color.white));
textAreaSide.setBackground(bgcolor);
textAreaSide.setLineWrap(true);
frame.add(scrollPane, BorderLayout.WEST);
textarea.setFont(new Font("Helvetica", Font.PLAIN, 20));
textarea.setForeground(Color.black);
textarea.setBorder(BorderFactory.createLineBorder(Color.white));
textarea.setBackground(bgcolor);
textarea.setLineWrap(true);
frame.add(textfield, BorderLayout.PAGE_END);
textfield.setFont(new Font("Helvetica", Font.PLAIN, 30));
textfield.setForeground(Color.black);
textfield.setBorder(BorderFactory.createLineBorder(Color.white));
textfield.setBackground(bgcolor);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//frame.pack();
frame.setVisible(true);
frame.setResizable(false);
textfield.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
printIfTextFieldIsActivated(evt);
}
});
}
public void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new GUI();
}
});
}
private void printIfTextFieldIsActivated(java.awt.event.KeyEvent evt) {
if(evt.getKeyCode() == KeyEvent.VK_ENTER) {
String commandString = textfield.getText().toString();
String output = command.parseCommand(commandString);
printAppend(output);
textfield.setText("");
}
}
public static GUI getInstance() {
if(instance == null){
instance = new GUI();
}
return instance;
}
}
当我从另一个类调用 getInstance 时,它会给我一个堆栈溢出,因为实例始终为空,我知道我的 GUI 类已创建,因为我可以看到 Gui。
谢谢
附:抱歉,代码很乱,我不是这方面的专家..
最佳答案
我想到了几件事:
您需要实例化
GUI
通过getInstance()
方法:public void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new GUI() // <-- NO GUI.getInstance(); // <-- YES } }); }
您可能正在访问
instance
来自不同线程的字段。如果是,则将其标记为volatile
可能有帮助。像这样:private static volatile GUI instance;
您可能正在
GUI
中触发监听器尝试通过GUI.getInstance()
获取实例的构造函数在构造函数完成之前。
关于java - 从另一个类的 Gui 类调用 print Text 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47401603/