代码运行后,双击鼠标,打印“双击”的同时也会打印“单击”,如何使两者效果分开呢?
就是说:如何实现 当单击鼠标的时候,只出现“单击”,当双击鼠标的时候,只出现“双击”。
- package cn.ittest;
- import java.awt.*;
- import java.awt.event.*;
- public class GUIDemo {
- private static final int WIDTH = Toolkit.getDefaultToolkit()
- .getScreenSize().width;
- private static final int HEIGHT = Toolkit.getDefaultToolkit()
- .getScreenSize().height;
- public static void main(String[] args) {
- Frame f = new Frame("hello world");
- f.setBounds((WIDTH - 400) / 2, (HEIGHT - 300) / 2, 400, 300);
- f.setBackground(Color.gray);
- f.setLayout(null);
- f.setVisible(true);
- // 添加按钮
- Button but = new Button("确定");
- but.setBounds(200, 150, 80, 40);
- but.setFont(new Font("楷体", 1, 12));
- f.add(but);
- // 添加鼠标监听器
- but.addMouseListener(new MouseAdapter() {
- public void mouseClicked(MouseEvent e) {
- int count = e.getClickCount();
- [color=Red] if (count == 2) {
- System.out.println("双击");
- } else if(count == 1)
- System.out.println("单击");[/color]
- }
- });
- }
- }
复制代码 |