自动重复点击 JButton
Repeated clicks of JButton automatically
如何让 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();
}
}
有关详细信息,请参阅 How to use Swing Timers
可以放到WindowOpened Frame事件中
Timer t = new Timer(1000, jButton.doClick());
t.start();
您可以使用 swing 的 Timer class 和
doClick 方法与 AbstractButton。
这是完整的源代码。
package Whosebug;
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();
}
});
}
}
如何让 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();
}
}
有关详细信息,请参阅 How to use Swing Timers
可以放到WindowOpened Frame事件中
Timer t = new Timer(1000, jButton.doClick());
t.start();
您可以使用 swing 的 Timer class 和
doClick 方法与 AbstractButton。 这是完整的源代码。
package Whosebug;
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();
}
});
}
}