Java 累积getClickCount()
你好 我正在制作一个考勤程序,点击一次会显示一个橙色按钮,点击两次会显示红色按钮,点击三次会显示黑色按钮。我遇到了如何累积getClickCount()值的问题,因为要使按钮注册3次单击,必须快速单击按钮3次 这是密码Java 累积getClickCount(),java,Java,你好 我正在制作一个考勤程序,点击一次会显示一个橙色按钮,点击两次会显示红色按钮,点击三次会显示黑色按钮。我遇到了如何累积getClickCount()值的问题,因为要使按钮注册3次单击,必须快速单击按钮3次 这是密码 button1.addMouseListener(new MouseAdapter(){ public void mousePressed(MouseEvent a){ if (a.getClickCoun
button1.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent a){
if (a.getClickCount() == 1){
button1.setBackground(Color.ORANGE);
}
else if (a.getClickCount() == 2){
button1.setBackground(Color.RED);
}
else if (a.getClickCount() == 3){
button1.setBackground(Color.BLACK);
}
}
});
frame.setVisible(true);
frame.pack();
}
}
步骤1:创建您自己的
mouseStener
类。在本例中,我选择创建一个扩展MouseAdapter
的外部类
public class MyMouseListener extends MouseAdapter
{
public void mousePressed(MouseEvent event)
{
Object obj = event.getSource();
if (obj instanceof JButton)
{
if JButton btn = (JButton)obj;
if (a.getClickCount() == 1)
{
btn.setBackground(Color.ORANGE);
}
else if (a.getClickCount() == 2)
{
btn.setBackground(Color.RED);
}
else if (a.getClickCount() == 3)
{
btn.setBackground(Color.BLACK);
}
}
}
}
步骤2:将此侦听器的独立实例添加到每个按钮
button1.addMouseListener(new MyMouseListener());
button2.addMouseListener(new MyMouseListener());
button3.addMouseListener(new MyMouseListener());
button4.addMouseListener(new MyMouseListener());
button5.addMouseListener(new MyMouseListener());
button6.addMouseListener(new MyMouseListener());
button7.addMouseListener(new MyMouseListener());
button8.addMouseListener(new MyMouseListener());
button9.addMouseListener(new MyMouseListener());
button10.addMouseListener(new MyMouseListener());
button11.addMouseListener(new MyMouseListener());
button12.addMouseListener(new MyMouseListener());
那应该对你有用。但是,正如我所提到的,这可以正常工作,但不是很好。如果我理解正确,基本上您希望每次按下按钮时根据之前按下按钮的次数更改颜色
MouseListener
对于JButton
来说不是一个好的选择,因为按钮可以通过键盘(通过快捷方式或聚焦活动)和编程方式激活,MouseListener
不会检测到任何按钮
相反,您应该使用ActionListener
此示例将在每次单击按钮时更改颜色。我使用了一组颜色
使生活更简单,但是一般的概念应该适用于if else
语句,您只需要在计数器达到极限时重置计数器
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
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.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class ButtonClicker {
public static void main(String[] args) {
new ButtonClicker();
}
public ButtonClicker() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public static class TestPane extends JPanel {
private static final Color[] COLORS = new Color[]{Color.ORANGE, Color.RED, Color.BLACK};
private int clickCount;
public TestPane() {
setLayout(new GridBagLayout());
JButton clicker = new JButton("Color Changer");
clicker.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
clickCount++;
setBackground(COLORS[Math.abs(clickCount % COLORS.length)]);
}
});
setBackground(COLORS[clickCount]);
add(clicker);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
}
请查看并了解更多详细信息
mouseStener
不是按钮的好选择。考虑提供一个说明你的问题的方法。这将减少混乱和更好的响应。我刚刚运行了您的代码,看起来工作正常(不是很好)。但是,您需要做的是使您的MouseListener
成为每个按钮都可以使用的类(您不希望创建多个相同的匿名侦听器来执行相同的操作)。然后将侦听器的独立实例添加到每个按钮。您应该创建一个方法createButton
,该方法获取一个int
作为参数,并返回带有按钮的新子面板。您当前已将该代码复制了12次。另外,请阅读以下内容:。我会做clickCount=(clickCount+1)%COLORS.length代码>,以防万一。@Radiodef为什么?数组的索引为0?或者应该是COLORS.length-1
?我的意思是防止溢出。如果用户点击按钮足够多次(比如说,他们使用的是自动点击器),它最终将换行为Integer.MIN\u值。它将抛出一个异常,因为Java的模可以产生一个负结果。(对于MCVE来说并不重要。)@Radiodef类似于Math.abs(clickCount%COLORS.length)
的东西不是更合适吗?我确实尝试了Integer.MIN_值%3
,它给出了-2
和((Integer.MIN_值+1)%3)
只给出了-1
)