Java 从另一个类从Gui类调用printText方法
我试图在我的GUI类中实现一个singleton,但一旦我返回实例,它就会给我一个nullpointer异常,我已经查看了多个源代码,但它就是不能正常工作Java 从另一个类从Gui类调用printText方法,java,swing,singleton,stack-overflow,Java,Swing,Singleton,Stack Overflow,我试图在我的GUI类中实现一个singleton,但一旦我返回实例,它就会给我一个nullpointer异常,我已经查看了多个源代码,但它就是不能正常工作 import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.*; import java.awt.*; import java.awt.event.*; import javax.swin
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时,它会给我一个stackoverflow,因为instance总是空的,我知道我的GUI类是创建的,因为我可以看到GUI
谢谢
p、 对不起,我不是这方面的专家。我想到了几件事:
getInstance()
方法实例化GUI
:
public void main(字符串[]args){
SwingUtilities.invokeLater(新的Runnable(){
@凌驾
公开募捐{
new GUI()//在我开始讨论您的实际问题之前,我突然想到了许多事情。GUI
从JFrame
扩展而来,但是您在它的构造函数中创建了第二个JFrame
——我现在不知道哪个框架被用于什么。一般来说,您应该避免从JFrame
扩展(如果没有其他原因,因为此类问题)。您应该避免使用KeyListener
和文本组件,在您的情况下,ActionListener
将完成相同的工作,更简单地说。您还缺少printAppend
的实现,这可能是一个问题,也可能不是一个问题。您的单例实现有问题,可能会泄漏多个实例。因此,我获取了您的代码修改后,它将运行并且没有问题。您可以考虑提供一个可运行的示例来演示您的问题,因为代码> GUI 构造函数是私有的;<代码>私有Guy()
,他们将无法通过new
@MadProgrammer让类不满足。你是对的,但他是在他的代码中完成的。这就是为什么我告诉他不要这么做。他们在哪里完成他们的代码?我看不到他们提供的代码,因为这会产生编译器错误,我不知道这是怎么可能的,也不知道这是怎么可能的relevant@MadProgrammer在他的main
方法中,它在他的GUI
类中,使私有构造函数可以访问它