Java 如何在按住按钮时执行JButton?
首先,我的程序很简单。我只需要单击或按Alt+Enter按钮来增加计数器 以下是您可以尝试的程序:Java 如何在按住按钮时执行JButton?,java,swing,jbutton,Java,Swing,Jbutton,首先,我的程序很简单。我只需要单击或按Alt+Enter按钮来增加计数器 以下是您可以尝试的程序: import java.awt.*; import java.awt.event.*; import javax.swing.*; public class holdDownClass implements ActionListener { private static JButton exebouton; private JTextArea ecran = new JTextA
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class holdDownClass implements ActionListener {
private static JButton exebouton;
private JTextArea ecran = new JTextArea();
private JScrollPane scrollecran = new JScrollPane(ecran);
private int counter = 0;
public static void main(String[] args) {
new holdDownClass();
}
private holdDownClass() {
// Window
JFrame frame = new JFrame("Name");
frame.setBounds(400, 350, 625, 355);
frame.setLayout(null);
Container container = frame.getContentPane();
// Panel
JPanel panneau = new JPanel();
panneau.setLayout(null);
panneau.setBounds(2, 42, 146, 252);
frame.add(panneau);
JLabel nglabel = new JLabel("Click or Press Alt+Enter");
nglabel.setBounds(5, 0, 200, 20);
panneau.add(nglabel);
// Button
exebouton = new JButton("Execute");
exebouton.setMnemonic(KeyEvent.VK_ENTER); // Shortcut: Alt + Enter
exebouton.setBounds(4, 18, 138, 47);
exebouton.addActionListener(this);
panneau.add(exebouton);
// Text Area
ecran.setEditable(true);
scrollecran.setBounds(150, 42, 467, 252);
container.add(scrollecran);
// Show
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
Object test = e.getSource();
if (test.equals(exebouton)) {
counter += 1;
ecran.setText(ecran.getText() + counter + "\n");
}
}
}
我的目标是:不要重复按Alt+Enter键,我想按住Alt+Enter键,使计数器“更快”递增。以下是您可以做到的方法-
private boolean mousePressed;
还有一个鼠标侦听器-
exebouton.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
mousePressed = true;
new Thread() {
public void run() {
while (mousePressed) {
counter += 1;
ecran.setText(ecran.getText() + counter + "\n");
}
}
}.start();
}
public void mouseReleased(MouseEvent e) {
mousePressed = false;
}
});
就这样。这是你可以做到的方法-
private boolean mousePressed;
还有一个鼠标侦听器-
exebouton.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
mousePressed = true;
new Thread() {
public void run() {
while (mousePressed) {
counter += 1;
ecran.setText(ecran.getText() + counter + "\n");
}
}
}.start();
}
public void mouseReleased(MouseEvent e) {
mousePressed = false;
}
});
就是这样。你可以使用
鼠标听音器,但是,就我个人而言,我觉得这不是实现你想要实现的目标的最合适的方法,因为它会与按钮的工作方式产生冲突
相反,您可以将更改侦听器附加到按钮模型,当按钮的状态保持按下时,循环一个Swing计时器
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class TestButton04 {
public static void main(String[] args) {
new TestButton04();
}
private int counter = 0;
private Timer trigger;
private JButton btn;
public TestButton04() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
btn = new JButton("0");
trigger = new Timer(125, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
counter++;
btn.setText(String.valueOf(counter));
}
});
trigger.setCoalesce(true);
trigger.setRepeats(true);
btn.getModel().addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
if (btn.getModel().isPressed()) {
trigger.start();
} else {
trigger.stop();
}
}
});
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(btn);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
你可以使用MouseListener
,但就个人而言,我觉得这不是实现你想要实现的目标的最合适的方法,因为它会与按钮的工作方式产生冲突
相反,您可以将更改侦听器附加到按钮模型,当按钮的状态保持按下时,循环一个Swing计时器
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class TestButton04 {
public static void main(String[] args) {
new TestButton04();
}
private int counter = 0;
private Timer trigger;
private JButton btn;
public TestButton04() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
btn = new JButton("0");
trigger = new Timer(125, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
counter++;
btn.setText(String.valueOf(counter));
}
});
trigger.setCoalesce(true);
trigger.setRepeats(true);
btn.getModel().addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
if (btn.getModel().isPressed()) {
trigger.start();
} else {
trigger.stop();
}
}
});
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(btn);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
您是否尝试处理鼠标按下和鼠标释放事件。按下鼠标时开始递增,松开鼠标时停止。@Sudhanshu我没有尝试过。我不知道该怎么做。我该怎么做?为此,您必须将keyListener注册到JButton
exebouton
@VishalK。您是否有一个示例?您是否尝试处理鼠标按下和鼠标释放事件。按下鼠标时开始递增,松开鼠标时停止。@Sudhanshu我没有尝试过。我不知道该怎么做。我该怎么做?为此,您必须将keyListener注册到JButton
exebouton
@VishalK。您有一个例子吗?请回答这个问题,但图形组件的更改可能应该在EDT上完成。使用SwingUtilities.invokeLater
,或者使用SwingUtilities.isEventDispatchThread()
进行检查。回答要切中要害,但对图形组件的更改可能应该在EDT上完成。使用SwingUtilities.invokeLater
,或者使用SwingUtilities.isEventDispatchThread()
进行检查。您知道为什么要在JLabel中设置文本“滞后”吗?即使我在计时器上设置了500毫秒的延迟…你是否将其设置为EDT?您正在阻止EDT吗?您知道为什么在JLabel中设置文本“滞后”吗?即使我在计时器上设置了500毫秒的延迟…你是否将其设置为EDT?你在阻止EDT吗?