计数按钮点击

时间:2013-02-21 10:04:22

标签: java swing jbutton

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

我做了这段代码:

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

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

我希望每次点击按钮向我显示计数。

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

8 个答案:

答案 0 :(得分:7)

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

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);
}

答案 1 :(得分:4)

您已将clicked声明为局部变量,初始化为0,除了1

之外,它永远不会是其他任何内容

clicked成为一个班级变量......

private int clicked = 0;

//...

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

答案 2 :(得分:4)

尝试以下代码

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

  clicked++;
  System.out.println(clicked);
 } 

答案 3 :(得分:2)

更改

int clicked = 0;

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

答案 4 :(得分:2)

您已在count内声明了ActionListener变量。在块外声明它。

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

  clicked++;
  System.out.println(clicked);
}    

答案 5 :(得分: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);
 } 

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

答案 6 :(得分: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);
 }

答案 7 :(得分: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);
    }

}