在使用Java Swing时收到错误消息?

huangapple go评论71阅读模式
英文:

Getting an error message in Java while using Java Swing?

问题

这是我使用GUI和Java Swing的一个非常初级的小项目之一。一切都正常运行,没有错误,直到我调用setText()方法。以下是我的代码:

public class FirstGui implements ActionListener {
    
    private int count = 0;
    private JLabel label;
    private JFrame frame;
    private JButton button;
    
    public FirstGui() {
        // 创建窗口
        frame = new JFrame();
        JPanel panel = new JPanel();
        panel.setBorder(BorderFactory.createEmptyBorder(30, 30, 10, 30));
        panel.setLayout(new GridLayout(0, 1));
        
        // 创建面板
        frame.add(panel, BorderLayout.CENTER);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("这是我们的第一个GUI!");
        frame.pack();
        frame.setVisible(true);
        
        // 创建一个点击按钮
        button = new JButton("点击这里!:)");
        panel.add(button); 
        // 当点击按钮时添加动作
        
        button.addActionListener(this);
        
        // 创建一个标签
        label = new JLabel("点击次数: ");
        panel.add(label);
    }

    public static void main(String[] args) {
        FirstGui user1 = new FirstGui();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        count++;
        label.setText("点击次数: " + count); // 这似乎是问题发生的地方。
    }
}

我不确定为什么它会给我一个错误消息:"无法调用"javax.swing.JLabel.setText(String)",因为"this.label"为空"。

英文:

This is one of my very first mini-projects using GUI and Java swing. Everything runs fine with no errors until I invoke the setText() method. Here is my code:

public class firstGui implements ActionListener {
	
	private int count = 0;
	private JLabel label;
	private JFrame frame;
	private JButton b;
	
	public firstGui() {
		// Creating the window
		JFrame frame = new JFrame();
		JPanel panel1 = new JPanel();
		panel1.setBorder(BorderFactory.createEmptyBorder(30, 30, 10, 30));
		panel1.setLayout(new GridLayout(0, 1));
	
		
		// Creating the panel
		frame.add(panel1, BorderLayout.CENTER);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setTitle("Here is our first GUI!");
		frame.pack();
		frame.setVisible(true);
		
		// Creating a button to click on
		JButton b = new JButton("Click here! :) ");
		panel1.add(b); 
		// Adding actions to the button when clicked
		
		b.addActionListener(this);
		
		// Creating a label
		JLabel label = new JLabel("Number of times clicked: ");
		panel1.add(label);
		
	}

	public static void main(String[] args) {
		
		firstGui user1 = new firstGui();
		
	
		
		

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		
		count++;
		label.setText("Number of times clicked: " + count); // here is where the problem seems to occur.
		
		
	}

}

I'm not sure as to why it gives me a:

"Cannot invoke "javax.swing.JLabel.setText(String)" because "this.label" is null" error message.

答案1

得分: 1

发生这种情况的原因是因为您对标签使用了一个新的_object_,而不是使用已声明的_label_字段。

在这里,您的以下字段可以在_firstGui_类中的任何非静态方法中访问。

private int count = 0;
private JLabel label;
private JFrame frame;
private JButton b;

所以,不要使用一个新的_JLabel_对象,在这里

JLabel label = new JLabel("点击次数:");

使用来自类字段的相同对象-在声明中删除对象类型。

label = new JLabel("点击次数:");
英文:

The reason this is happening is because you have used a new object for your label.
Rather than using the declared label field.

Your following fields here, accessible to any non-static method within the firstGui class.

private int count = 0;
private JLabel label;
private JFrame frame;
private JButton b;

So, instead of using a new JLabel object, here

JLabel label = new JLabel("Number of times clicked: ");

Use the same one from the class fields—remove the object type, in the declaration.

label = new JLabel("Number of times clicked: ");

huangapple
  • 本文由 发表于 2023年6月8日 02:01:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76425958.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定