Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java GUI应用程序中的变量未更新_Java_Swing_User Interface_Awt - Fatal编程技术网

Java GUI应用程序中的变量未更新

Java GUI应用程序中的变量未更新,java,swing,user-interface,awt,Java,Swing,User Interface,Awt,我目前正在制作一个非常简单的JavaGUI应用程序,但遇到了变量无法更新的问题。该应用程序是一个简单的篮球记分员,分数整数不会更新,显示它们的标签文本也不会更新。没有错误,所以我不确定为什么没有更新。守则: ScoreWindow.java import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.SpringLayout; import javax.swing.JLabel; import java.aw

我目前正在制作一个非常简单的JavaGUI应用程序,但遇到了变量无法更新的问题。该应用程序是一个简单的篮球记分员,分数整数不会更新,显示它们的标签文本也不会更新。没有错误,所以我不确定为什么没有更新。守则:

ScoreWindow.java

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.SpringLayout;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.FlowLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;

public class ScoreWindow implements ScoreListener {

    private JFrame frmScorewindow;

    public volatile JLabel homeScoreLabel;

    public JLabel awayScoreLabel;

    public volatile int homeScore, awayScore;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ScoreWindow window = new ScoreWindow();
                    window.frmScorewindow.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public ScoreWindow() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {

        // Init Scores
        homeScore = 0;
        awayScore = 0;

        frmScorewindow = new JFrame();
        frmScorewindow.setResizable(false);
        frmScorewindow.setTitle("Score Keeper");
        frmScorewindow.setBounds(100, 100, 551, 348);
        frmScorewindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmScorewindow.getContentPane().setLayout(null);

        JButton homeScore2 = new JButton("+2");
        homeScore2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ScoreListener listener = new ScoreWindow();
                listener.homeScore(2);
            }
        });
        homeScore2.setBounds(110, 129, 117, 29);
        frmScorewindow.getContentPane().add(homeScore2);

        JButton homeScore3 = new JButton("+3");
        homeScore3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ScoreListener listener = new ScoreWindow();
                listener.homeScore(3);
            }
        });
        homeScore3.setBounds(110, 156, 117, 29);
        frmScorewindow.getContentPane().add(homeScore3);

        JButton awayScore2 = new JButton("+2");
        awayScore2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ScoreListener listener = new ScoreWindow();
                listener.awayScore(2);
            }
        });
        awayScore2.setBounds(332, 129, 117, 29);
        frmScorewindow.getContentPane().add(awayScore2);

        JButton awayScore3 = new JButton("+3");
        awayScore3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ScoreListener listener = new ScoreWindow();
                listener.awayScore(3);
            }
        });
        awayScore3.setBounds(332, 156, 117, 29);
        frmScorewindow.getContentPane().add(awayScore3);

        JButton resetButton = new JButton("Reset");
        resetButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ScoreListener listener = new ScoreWindow();
                listener.reset();
            }
        });
        resetButton.setBounds(225, 220, 117, 29);
        frmScorewindow.getContentPane().add(resetButton);

        homeScoreLabel = new JLabel("000");
        homeScoreLabel.setFont(new Font("Lucida Grande", Font.PLAIN, 24));
        homeScoreLabel.setHorizontalAlignment(SwingConstants.CENTER);
        homeScoreLabel.setBounds(138, 88, 61, 29);
        frmScorewindow.getContentPane().add(homeScoreLabel);

        awayScoreLabel = new JLabel("000");
        awayScoreLabel.setHorizontalAlignment(SwingConstants.CENTER);
        awayScoreLabel.setFont(new Font("Lucida Grande", Font.PLAIN, 24));
        awayScoreLabel.setBounds(361, 88, 61, 29);
        frmScorewindow.getContentPane().add(awayScoreLabel);

        JLabel lblHome = new JLabel("Home");
        lblHome.setHorizontalAlignment(SwingConstants.CENTER);
        lblHome.setBounds(138, 60, 61, 16);
        frmScorewindow.getContentPane().add(lblHome);

        JLabel lblAway = new JLabel("Away");
        lblAway.setHorizontalAlignment(SwingConstants.CENTER);
        lblAway.setBounds(361, 60, 61, 16);
        frmScorewindow.getContentPane().add(lblAway);

        JLabel title = new JLabel("Score Keeper App");
        title.setHorizontalAlignment(SwingConstants.CENTER);
        title.setBounds(180, 33, 200, 16);
        frmScorewindow.getContentPane().add(title);
    }

    @Override
    public void reset() {
        print("reset();");
        homeScore = 0;
        awayScore = 0;
        awayScoreLabel.setText("" + awayScore);
        homeScoreLabel.setText("" + homeScore);
    }

    @Override
    public void awayScore(int n) {
        print("awayScore();");
        awayScore+=n;
        awayScoreLabel.setText("" + awayScore);
    }

    @Override
    public void homeScore(int n) {
            print("homeScore();");
            print(homeScoreLabel.getText());
            homeScore = homeScore + n;
            homeScoreLabel.setText("" + homeScore);
            homeScoreLabel.repaint();
            homeScoreLabel.revalidate();
    }

    static void print(Object o) {
        System.out.println(o);
    }
}
ScoreListener.java

public interface ScoreListener {
        public void reset();
        public void awayScore(int n);
        public void homeScore(int n);
}
谢谢

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                ScoreWindow window = new ScoreWindow();
                window.frmScorewindow.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
您可以使用上述代码创建窗口

    JButton homeScore2 = new JButton("+2");
    homeScore2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            ScoreListener listener = new ScoreWindow();
            listener.homeScore(2);
        }
    });
但是,然后创建窗口的第二个实例

不要这样做。您只需要创建一个类实例。引用此实例的所有其他代码


ActionListner类是在ScoreWindow类中定义的,您可以直接引用“homeScore()”方法。

您在错误的ScoreWindow实例上调用了方法。您正在侦听器中创建新的ScoreWindow实例,但这些实例不是显示的实例,因此显示永远不会更改。请注意您使用的引用,因为这是这里的关键。为了确保您没有这样做,请确保您在整个程序中只看到一次
new ScoreWindow()
,而不是像这样看到15次。