Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.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 自动重复单击JButton_Java_Swing_Jbutton - Fatal编程技术网

Java 自动重复单击JButton

Java 自动重复单击JButton,java,swing,jbutton,Java,Swing,Jbutton,如何使JButton每秒自动单击一次 我已尝试编辑执行的按钮操作,但不起作用。使用SwingTimer并在按钮上调用doClick import javax.swing.JButton; import javax.swing.Timer; public class ButtonClicker implements ActionListener { private JButton btn; private Timer timer; public ButtonClick

如何使
JButton
每秒自动单击一次


我已尝试编辑执行的按钮操作,但不起作用。

使用
SwingTimer
并在按钮上调用
doClick

import javax.swing.JButton;
import javax.swing.Timer;

public class ButtonClicker implements ActionListener {

    private JButton btn;
    private Timer timer;

    public ButtonClicker(JButton btn) {
        this.btn = btn;
        timer = new Timer(1000, this);
    }

    public void start() {
        timer.start();
    }

    public void stop() {
        timer.stop();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        btn.doClick();
    }

}

有关更多详细信息,请参见使用
SwingTimer
并在按钮上调用
doClick

import javax.swing.JButton;
import javax.swing.Timer;

public class ButtonClicker implements ActionListener {

    private JButton btn;
    private Timer timer;

    public ButtonClicker(JButton btn) {
        this.btn = btn;
        timer = new Timer(1000, this);
    }

    public void start() {
        timer.start();
    }

    public void stop() {
        timer.stop();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        btn.doClick();
    }

}

有关更多详细信息,请参见

您可以将其放入WindowOpened Frame事件中

Timer t = new Timer(1000, jButton.doClick());
t.start();

您可以将其放入WindowOpen Frame事件中

Timer t = new Timer(1000, jButton.doClick());
t.start();

您可以使用swing的计时器类和

doClick方法和抽象按钮。 这是一个完整的源代码

package stackoverflow;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class JButtonTimer extends JPanel {

    private static final long serialVersionUID = 1L;
    private static final int SCREEN_WIDTH = 500;
    private static final int SCREEN_HEIGHT = 500;

    public JButtonTimer() {
        final JButton clickBtn = new JButton("clickMe");
        clickBtn.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
              System.out.println(e.getActionCommand());

          }
        });

        add(clickBtn);

        Timer timer = new Timer(1 * 1000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                clickBtn.doClick();
            }

        });
        timer.start();


    }


    public Dimension getPreferredSize() {
        return new Dimension(SCREEN_WIDTH, SCREEN_HEIGHT);
    }

    public static void createAndShowGui() {
        JFrame frame = new JFrame();
        frame.add(new JButtonTimer());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.pack();
        frame.setVisible(true);

    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }
}

您可以使用swing的计时器类和

doClick方法和抽象按钮。 这是一个完整的源代码

package stackoverflow;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class JButtonTimer extends JPanel {

    private static final long serialVersionUID = 1L;
    private static final int SCREEN_WIDTH = 500;
    private static final int SCREEN_HEIGHT = 500;

    public JButtonTimer() {
        final JButton clickBtn = new JButton("clickMe");
        clickBtn.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
              System.out.println(e.getActionCommand());

          }
        });

        add(clickBtn);

        Timer timer = new Timer(1 * 1000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                clickBtn.doClick();
            }

        });
        timer.start();


    }


    public Dimension getPreferredSize() {
        return new Dimension(SCREEN_WIDTH, SCREEN_HEIGHT);
    }

    public static void createAndShowGui() {
        JFrame frame = new JFrame();
        frame.add(new JButtonTimer());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.pack();
        frame.setVisible(true);

    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }
}

你为什么要这样做?如果你想自动触发某个动作,那么点击按钮是错误的方法!1) 请看,按钮的操作侦听器调用方法可能更好。然后使用Swing
计时器调用相同的方法。2) “我试过编辑执行的按钮操作,但不起作用。”为什么不?什么,特别是失败?3) 为了更快地获得更好的帮助,请发布一个or。您为什么要这样做?如果你想自动触发某个动作,那么点击按钮是错误的方法!1) 请看,按钮的操作侦听器调用方法可能更好。然后使用Swing
计时器调用相同的方法。2) “我试过编辑执行的按钮操作,但不起作用。”为什么不?什么,特别是失败?3) 为了更快地获得更好的帮助,请发布or。因为返回
void
您的代码示例不会编译-因为
void
不是
ActionListener
的实例,因为返回
void
您的代码示例不会编译-因为
void
不是
ActionListener