Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.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 如何计算按钮的按下次数_Java_User Interface - Fatal编程技术网

Java 如何计算按钮的按下次数

Java 如何计算按钮的按下次数,java,user-interface,Java,User Interface,我在计算程序中按下按钮的次数。 我希望每次按下按钮时它都会改变,以显示用户所做的按下次数。 这是我的密码: import javax.swing.*; import java.awt.*; import java.awt.event.*; // needed for listeners public class PushCounter3 { public static void main(String[] arg) { PushGUI myGui = new Pu

我在计算程序中按下按钮的次数。 我希望每次按下按钮时它都会改变,以显示用户所做的按下次数。 这是我的密码:

import javax.swing.*; 
import java.awt.*;  
import java.awt.event.*; // needed for listeners

public class PushCounter3 {
    public static void main(String[] arg) {
        PushGUI myGui = new PushGUI();
    }
}

 class PushGUI extends JPanel{
    private JFrame theWindow;
    private int nbPushes;
    private JButton myButton;
    private JLabel myLabel;
    private JPanel myPanel;

    PushGUI(){
        theWindow = new JFrame("Push Counter that counts!");
        theWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        nbPushes = 0;
        myButton = new JButton("Push Me!");
        myLabel = new JLabel("Pushes: " + Integer.toString(nbPushes));

        // let's register the event listener     
        myButton.addActionListener(new ButtonListener()); 

        myPanel = new JPanel();

        theWindow.add(myPanel);
        myPanel.add(myButton);

        myPanel.add(myLabel);

        theWindow.pack();
        theWindow.setVisible(true);
    }


   private class ButtonListener implements ActionListener {
      public void actionPerformed (ActionEvent event) {

         nbPushes++;
         myLabel.setText("Pushes: " + Integer.toString(nbPushes));
      }
}
}

所有内容都显示在屏幕上,但不是每次单击按钮都会更新。

将变量设置为静态,然后查看它是否正确更新me@MadProgrammer今天早上打开了我的程序,它成功了。不知道如何或为什么。我要了