2

我想计算使用 GUI 单击按钮的次数。

我做了这段代码:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) 
 {                                         
  int clicked = 0;
  clicked++;
  System.out.println(clicked);
 }    

但每次我单击按钮时,它都会显示输出“1”。

我希望每次单击按钮时都显示计数。

例如:如果我单击按钮两次,它应该给我“2”的输出。

4

8 回答 8

7

每次单击时都会重置计数器,因为您已经在 action 方法中定义了变量。尽量不要那样做。

int clicked = 0; // move this outside
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) 
{                                         
    // int clicked = 0; -- this resets it to 0 each time
    clicked++;
    System.out.println(clicked);
}
于 2013-02-21T10:05:01.077 回答
4

您已声明clicked为局部变量,初始化为0,它不能是其他任何东西1

改为clicked创建一个类级别的变量...

private int clicked = 0;

//...

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) 
{                                         
    clicked++;
    System.out.println(clicked);
} 
于 2013-02-21T10:06:03.023 回答
4

试试下面的代码

int clicked = 0;
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) 
 {                                         

  clicked++;
  System.out.println(clicked);
 } 
于 2013-02-21T10:06:07.537 回答
2

您已经countActionListener. 在块外声明它。

int clicked = 0;  //make it as your class member.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) 
{                                         

  clicked++;
  System.out.println(clicked);
}    
于 2013-02-21T10:06:23.637 回答
2

根据您实例化此类的方式,您需要clicked在字段级别或类变量级别声明变量。目前,clicked变量的范围是方法的本地范围。

选项1

int clicked = 0;

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) 
 {                                         
  clicked++;
  System.out.println(clicked);
 }  

选项 2

static int clicked = 0;

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) 
 {                                         
  clicked++;
  System.out.println(clicked);
 } 

您使用的选项将取决于实例化。如果可能,应避免使用第二种选择。

于 2013-02-21T10:06:33.103 回答
2

每次jButton1ActionPerformed触发时,单击的变量都会实例化回 0,这就是为什么它总是给你一个值 1。你应该将clicked变量移到该方法之外

//Somewhere in your class
private intClicked = 0;
//More methods here.

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) 
 {                                         
  clicked++;
  System.out.println(clicked);
 }
于 2013-02-21T10:38:58.837 回答
2

改变

int clicked = 0;

成为你班级的一员。这样每次按下按钮时它都不会设置为零。

于 2013-02-21T10:05:26.317 回答
0
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class Threading extends JFrame {

    private JPanel contentPane;
    private JTextField DisplayOne;
    private JTextField DisplayTwo;

    int count;
    int count1;
    int count2;


    /**
     * Launch the application.
     */

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() 
            {
                  try {
                    Threading frame = new Threading();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */


    public Threading() 
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        JPanel panel = new JPanel();
        contentPane.add(panel, BorderLayout.CENTER);

        DisplayOne = new JTextField();
        panel.add(DisplayOne);
        DisplayOne.setColumns(10);

        DisplayTwo = new JTextField();
        panel.add(DisplayTwo);
        DisplayTwo.setColumns(10);

        JButton btnCountOne = new JButton("Count 1");
        count1=0;
        btnCountOne.addActionListener(new ActionListener() {                
                public void actionPerformed(ActionEvent arg0) 
                {   
                    DisplayOne.setText(Integer.toString(count1++));
                }
        });
        panel.add(btnCountOne);

        JButton btnCountTwo = new JButton("Count 2");
        count2=0;
        btnCountTwo.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) 
            {
                DisplayTwo.setText(Integer.toString(count2++));
            }
        });
        panel.add(btnCountTwo);

        JButton btnCountBoth = new JButton("Count Both");
        count=0;
        btnCountBoth.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) 
            {
                while (count < 10)
                {
                DisplayOne.setText(Integer.toString(++count));
                DisplayTwo.setText(Integer.toString(count));
                }
            }
        });
        panel.add(btnCountBoth);
    }

}
于 2013-03-16T07:33:41.283 回答