如何知道在单击操作或拖动操作后触发释放侦听器?

时间:2014-05-21 05:34:17

标签: java swing awt

我几小时前问过一个模糊的问题,希望这个描述可以说清楚。

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;




public class TestMouseEvent {
    public void createUI(){
        JFrame frame = new JFrame("Test Mouse Event");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        MainPanel mainPanel = new MainPanel();

        mainPanel.setPreferredSize(new Dimension(800, 600));
        mainPanel.addMouseListener(new ImageMouseListener());
        mainPanel.addMouseMotionListener(new ImageMouseAdapter());

        frame.add(mainPanel,BorderLayout.CENTER);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        TestMouseEvent testMouseEvent = new TestMouseEvent();
        testMouseEvent.createUI();
    }

    @SuppressWarnings("serial")
    class MainPanel extends JPanel{
        protected void paintComponent(Graphics g){
            super.paintComponent(g);
            g.setFont(new Font("Arial", Font.PLAIN, 20));
            g.drawString("I'm a panel and I'm being listened now", 200, 300);
        }
    }

    class ImageMouseListener implements MouseListener{

        @Override
        public void mouseClicked(MouseEvent e) {
            // TODO Auto-generated method stub
            System.out.println("clicked");
        }

        @Override
        public void mousePressed(MouseEvent e) {
            // TODO Auto-generated method stub
            System.out.println("pressed");
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            // TODO Auto-generated method stub
            System.out.println("released");     
            JOptionPane.showMessageDialog(null, "I only want to be showed when \"drag\" event over but not for click event!");
        }

        @Override
        public void mouseEntered(MouseEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseExited(MouseEvent e) {
            // TODO Auto-generated method stub

        }


    }

    class ImageMouseAdapter extends MouseAdapter{       
        public void mouseDragged(MouseEvent e){
            if (e.getModifiers() == InputEvent.BUTTON1_MASK) {
                System.out.println("dragged");              
            }
        }
    }
}

我只想在拖动操作后立即显示JOptionPane,而不是点击操作。所以我怎么知道差异?

2 个答案:

答案 0 :(得分:3)

在ImageMouseAdapter内部设置一个标志(如果将dragInProgressFlag命名为),则为true。在释放时检查标志。如果它是真的显示JOptionPane并将其重置为false。

答案 1 :(得分:1)

首先,将您的MouseListenerMouseMotionListener统一到一个班级中,您只需使用MouseAdapter即可完成此操作...

其次,添加一个实例变量作为标志,以指示是否正在进行拖动操作......

class ImageMouseListener extends MouseAdapter {

    private boolean isDragging = false;

    @Override
    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub
        System.out.println("clicked");
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub
        if (isDragging) {
            System.out.println("released");
            JOptionPane.showMessageDialog(null, "I only want to be showed when \"drag\" event over but not for click event!");
        }
        isDragging = false
    }

    public void mouseDragged(MouseEvent e) {
        if (e.getModifiers() == InputEvent.BUTTON1_MASK) {
            isDragging = true;
            System.out.println("dragged");
        }
    }
}

创建ImageMouseListener的单个实例,并将其添加为面板的mouseListenermouseMotionListener ...

ImageMouseListener handler = new ImageMouseListener();
mainPanel.addMouseListener(handler);
mainPanel.addMouseMotionListener(handler);

您可以考虑在Java中使用内置的Drag' n' Drop支持,具体取决于您想要实现的目标,例如,查看Java - How to drag and drop JPanel with its components