累积getClickCount()

时间:2014-11-18 23:18:42

标签: java

美好的一天!

我正在制作一个出勤检查程序,点击一次显示橙色按钮,红色按钮,两次点击,黑色按钮为3.我有关于如何积累getClickCount()的问题值,因为按钮注册3次点击,按钮必须快速点击3次。

这是代码

        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();

    }
}

2 个答案:

答案 0 :(得分:1)

如果我理解正确,基本上你想在每次按下按钮时改变颜色,这取决于按钮之前被按下的次数。

MouseListener不是JButton的不错选择,因为键盘可以激活按钮(通过快捷方式或焦点活动),而且可以通过编程方式激活MouseListener无法检测到的按钮

相反,您应该使用ActionListener

此示例将在每次单击按钮时更改颜色。我使用了一组Color来简化生活,但是一般概念应该适用于if-else语句,你只需要在计数器达到极限时重置它

Clicker

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);
        }

    }

}

请查看How to Use Buttons, Check Boxes, and Radio ButtonsHow to Write an Action Listeners了解详情

答案 1 :(得分:0)

第1步:创建自己的MouseListener课程。在这种情况下,我选择创建一个扩展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());

这对你有用。但是,正如我所提到的,这可行,但不是很好。