我正在创建一个小程序来重新创建游戏。到目前为止,我已经创建了一个小的GUI。我的问题是,当我创建一个动作侦听器来更新变量counterNum时,该变量随后更新了cookieCountLabel,屏幕只更新了一个而未更新它。我以为,如果单击该按钮,counterNum应该等于counterNum +1。然后它应该已经更新了cookieCountLabel,但是这似乎没有用。

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JComponent;
import java.awt.Graphics;
import java.awt.Canvas;
import java.awt.FlowLayout;
import javax.swing.SwingUtilities;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
 *Author:Graham
 *Lang:Java
 *Program: Cookie Clicker
 *Date Created: 6/22/2019
 *CITATION:
 *Some GUI concepts from(the pdf you provided):https://lms.dist113.org/attachment/907907595/docviewer
 *Some GUI concepts from https://www.guru99.com/java-swing-gui.html
 */
public class Clicker extends JComponent
{
    public static void main(String[] args){
        //variables



        //frame
        JFrame frame = new JFrame("Cookie Clicker");
        frame.setLocationRelativeTo(null);

        frame.setSize(300,300);

        frame.setResizable(true);
        frame.setLayout(null);

        //close on click x
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //buttons
        JButton cookie = new JButton("Cookie");
        cookie.setBounds(80,0, 140, 20);
        //to set visible
        frame.setVisible(true);
        frame.add(cookie);



        //listen for click
        cookie.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
            Integer counterNum = 0;
            counterNum += 1;
            String convert = counterNum.toString();
            JLabel cookieCountLabel = new JLabel();
            cookieCountLabel.setBounds(140,120,50,20);
            cookieCountLabel.setText(convert);
            frame.add(cookieCountLabel);



            }



        });


    }
}

最佳答案

首先,您每次单击按钮时都将counterNum设置为0。因此,您的counterNum值将始终显示为1:

cookie.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
            Integer counterNum = 0; !!! -> error here
            counterNum += 1;


要解决此问题,请从main方法中取出counterNum变量,并将其设为字段。

public class Clicker extends JComponent
{
    private static int counterNum; !!!

    public static void main(String[] args){
     ...


接下来,每按一次按钮,您将创建一个新的Label,其中包含counterNum的值。如注释中所建议,您可能不想每次按下按钮都创建一个新的Label对象,而是想更改写在Label上的值。因此,从ActionListener中取出Label创建代码,并将其放入main方法中。现在,您只有1个标签,其中包含counterNum的值。

public static void main(String[] args){

        JLabel cookieCountLabel = new JLabel(); !!!
        cookieCountLabel.setBounds(140,120,50,20); !!!

        //frame
        JFrame frame = new JFrame("Cookie Clicker");
        frame.setLocationRelativeTo(null);


现在,每当您按下按钮时,都不会创建新的Label,但是counterNum的值会更改并增加1。这就是您的ActionListener现在的样子:

cookie.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
        counterNum += 1; //increment counterNum by 1
        String convert = counterNum + ""; //convert to String
        cookieCountLabel.setText(convert);
        frame.add(cookieCountLabel);
    }


java -  Action 监听器不更新Java中的变量-LMLPHP

09-30 23:57