如何让我的按钮 actionListener 计算某人点击的次数?
How do I get my button actionListener to count the number of times someone clicks?
public class Button {
public static void main(String[] args) {
int numClicks = 0;
JButton button1 = new JButton ();
button1.setText("1 click = 1 dollar for animals you love");
JFrame god = new JFrame ();
JPanel panel = new JPanel();
god.getContentPane().add(button1);
god.add(button1);
god.setSize(new Dimension(400, 400));
god.setVisible(true);
}
public void actionPerformed(ActionEvent event) {
}
}
谁能帮我弄清楚如何让actionlistener 计算按钮被点击的次数?我是一个新手编码员(3 个月),我真的坚持这个
您需要为您的按钮添加 ActionListener。
button1.addActionListener(new MyActionList());
//MyActionList is an object which implements ActionListener
你的情况是同一个对象。
//---implements ActionListener---
public class Button implements ActionListener{
private int clickCount;
public Button() {
clickCount = 0;
JFrame god = new JFrame ();
god.setSize(new Dimension(400, 400));
//if you don't do this, your program still running, even if you click on X.
god.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
god.add(panel);
JButton button1 = new JButton("1 click = 1 dollar for animals you love");
//here we add ActionListener
button1.addActionListener(this);
//'this' refer to the current object. In our case 'new Button()' from 'main'...
//...which has implemented ActionListener.
panel.add(button1);
god.setVisible(true);
}
public static void main(String[] args){
new Button();
}
//Override from Interface ActionListener and...
//...here describe what happend when button was pressed.
@Override
public void actionPerformed(ActionEvent e) {
clickCount++;
System.out.println(clickCount);
}
}
public class Button {
public static void main(String[] args) {
int numClicks = 0;
JButton button1 = new JButton ();
button1.setText("1 click = 1 dollar for animals you love");
JFrame god = new JFrame ();
JPanel panel = new JPanel();
god.getContentPane().add(button1);
god.add(button1);
god.setSize(new Dimension(400, 400));
god.setVisible(true);
}
public void actionPerformed(ActionEvent event) {
}
}
谁能帮我弄清楚如何让actionlistener 计算按钮被点击的次数?我是一个新手编码员(3 个月),我真的坚持这个
您需要为您的按钮添加 ActionListener。
button1.addActionListener(new MyActionList());
//MyActionList is an object which implements ActionListener
你的情况是同一个对象。
//---implements ActionListener---
public class Button implements ActionListener{
private int clickCount;
public Button() {
clickCount = 0;
JFrame god = new JFrame ();
god.setSize(new Dimension(400, 400));
//if you don't do this, your program still running, even if you click on X.
god.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
god.add(panel);
JButton button1 = new JButton("1 click = 1 dollar for animals you love");
//here we add ActionListener
button1.addActionListener(this);
//'this' refer to the current object. In our case 'new Button()' from 'main'...
//...which has implemented ActionListener.
panel.add(button1);
god.setVisible(true);
}
public static void main(String[] args){
new Button();
}
//Override from Interface ActionListener and...
//...here describe what happend when button was pressed.
@Override
public void actionPerformed(ActionEvent e) {
clickCount++;
System.out.println(clickCount);
}
}