Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/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 GUI计数器的重置按钮_Java_Swing - Fatal编程技术网

Java GUI计数器的重置按钮

Java GUI计数器的重置按钮,java,swing,Java,Swing,我必须在button2.addActionListener中输入什么才能让代码将计数重置为零。我对这件事很感兴趣。我想调用ActionReset,这是下面定义的一个方法,但是我不知道如何在这个实例中执行它。为了弄明白这一点,我的大脑受到了严重的伤害 import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.*; import javax.swing.*; public cla

我必须在button2.addActionListener中输入什么才能让代码将计数重置为零。我对这件事很感兴趣。我想调用ActionReset,这是下面定义的一个方法,但是我不知道如何在这个实例中执行它。为了弄明白这一点,我的大脑受到了严重的伤害

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.*;
import javax.swing.*;

public class GUI implements ActionListener
{
    private int count = 0;
    private JLabel label;
    private JFrame frame;
    private JPanel panel;
    public GUI()
    {
        frame = new JFrame();

        JButton button = new JButton("Click Me");
        button.addActionListener(this);
        JButton button2 = new JButton("Reset");
        button2.addActionListener(actionReset);

        label = new JLabel("Number of Clicks: 0");
        panel = new JPanel();
        panel.setBorder(BorderFactory.createEmptyBorder(60, 60, 20, 30));
        panel.setLayout(new GridLayout(0, 2));
        panel.add(button);
        panel.add(button2);
        panel.add(label);

        frame.add(panel, BorderLayout.CENTER);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("MY GUI");
        frame.pack();
        frame.setVisible(true);
    }

    public void actionPerformed(ActionEvent e)
    {
        count++;
        label.setText("Number of Clicks: " +count);
    }

    public void actionReset(ActionEvent d)
    {
        count = 0;
    }

    public static void main(String[] args)
    {
        new GUI();
    }
}
例如:

    button2.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(final ActionEvent e) {
            count = 0;
            label.setText("Number of Clicks: " +count);
        }
    });
或使用lambda:

    button2.addActionListener(e -> {
        count = 0;
        label.setText("Number of Clicks: " +count);
    });

由于Java 8,
ActionListener
是一个功能接口。因此,您可以将以下三件事传递给
JButton#addActionListener

  • 实现
    ActionListener
    的类的实例。您可以使用
    按钮。addActionListener(这个)
  • 类型为
    ActionEvent->void
    的lambda,例如
    按钮.addActionListener(事件->系统.out.println(“单击”)
  • 类型为
    ActionEvent->void
    的方法引用。您尝试使用
    按钮2.addActionListener(actionReset)来执行此操作。但是,这是一个编译错误,因为方法引用是通过
    object::method
    引用的。在您的例子中,这将是
    button2.addActionListener(this::actionReset)(@DanW关于这不可能的评论是不正确的)
我还建议在重置计数器时重置标签说明,否则标签文本仅在再次增加计数器时才会更改

public void actionReset(ActionEvent d)
{
    count = 0;
    label.setText("Number of Clicks: " +count);
}

您现在有编译错误吗?@DanW不正确。ActionListener是一个功能接口。因此,如果lambda或方法引用具有匹配的签名(在本例中为
ActionEvent->void
),则可以将它们作为操作侦听器传递。它必须以
this::
作为前缀才能编译。对于上面的代码,要说得更多一些,但是使用它,最终您会发现问题。