不同JPanels的鼠标侦听器事件

时间:2016-05-10 11:10:50

标签: java swing events jlabel mousemotionlistener

是否可以使用具有不同效果的全局鼠标移动侦听器,具体取决于单击JPanel的内容(仅使用一个鼠标移动侦听器)?

例如:我有一个JFrame,其中添加了两个JPanel,并在JFrame中添加了一个鼠标移动侦听器。我希望在单击一个JPanel时调整屏幕大小,但是当我单击另一个时,我想要拖动JFrame。我认为这可以使用JLabel使用JLabel的文本进行检查,与JButton相同。

编辑:是的,这绝对不是正确的做事方式,只是想知道是否有可能,如果有可能,怎么样?

编辑:为了让事情更清楚一点,我有一个扩展ActionListener,MouseMotionListener,MouseListener的类。是否有可能让这一个类处理JFrame的所有事件,这些事件附加了许多不同的JPanel,并根据JPanel的压力做了不同的事情? (例如将一个ID附加到JPanels,我可以将event.getSource()与之比较)

3 个答案:

答案 0 :(得分:1)

首先,一个“全局”监听器,为不同的组件做不同的事情,是一个坏主意,它将过多的逻辑放在一个单独的代码中,将代码耦合起来,成为维护的噩梦。

话虽如此,您可以使用单个MouseListener添加到每个组件,例如......

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JPanel left = new TestPane();
                JPanel right = new TestPane();

                left.setBackground(Color.RED);
                right.setBackground(Color.BLUE);

                left.setName("left");
                right.setName("right");

                MouseListener listener = new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        System.out.println(((JPanel)e.getSource()).getName());
                    }
                };

                left.addMouseListener(listener);
                right.addMouseListener(listener);

                JFrame frame = new JFrame("Testing");
                frame.setLayout(new GridLayout(0, 2));
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(left);
                frame.add(right);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

    }

}

然后您可以简单地使用MouseEvent#getSource来确定触发事件的组件。为简单起见,我为每个面板提供了name并显示出来,在做出决定之前,我会用其他方法来识别组件。

更好的解决方案是提供一个特定的MouseListener,根据需要为每个面板执行特定的工作,这变得更容易管理,隔离责任,解耦代码并变得更容易维护和管理< / p>

答案 1 :(得分:0)

我不确定这是否是最好的方法,但我只测试了它并且它确实有效,至少就我的代码测试它而言。当按下鼠标时,找到鼠标所在的框,并相应地填写。

public static void main(String[] args) {
    JFrame main = new JFrame("test");
    JPanel first = new JPanel();
    JPanel second = new JPanel();
    first.setSize(400, 500);
    first.setBackground(Color.BLUE);
    second.setBackground(Color.RED);
    second.setSize(400,500);
    main.setSize(800, 500);
    main.add(first);
    first.setLocation(0,0);
    second.setLocation(400, 500);
    main.add(second);
    main.pack();
    main.setVisible(true);
    main.setSize(800, 500);
    main.addMouseListener(new MouseListener() {

        @Override
        public void mousePressed(MouseEvent e) {
            if(first.getBounds().contains(new Point(e.getX(), e.getY())))
            {
                System.out.println("first box");
            }else if(second.getBounds().contains(new Point(e.getX(), e.getY())))
            {
                System.out.println("Second box");
            }
        }
    });
}

答案 2 :(得分:0)

编辑2

这应该是你的答案,我注意到在调试时,当我点击框架上的空白区域时,我得到一个名为"null.contentPane"的组件,所以我把它放在条件中并且它有效!我希望我帮助

public class PanelsListener extends JFrame {

public static void main(String[] args) {
    PanelsListener pl = new PanelsListener();
    pl.setSize(700, 700);
    pl.setLayout(new FlowLayout());
    JPanel fp = new JPanel();

    fp.setName("First");
    fp.setSize(200, 200);
    fp.setBorder(new LineBorder(Color.red));
    JPanel sp = new JPanel();
    sp.setName("Second");
    sp.setSize(200, 200);
    sp.setBorder(new LineBorder(Color.blue));
    pl.add(fp);
    pl.add(sp);

    pl.addMouseListener(new MouseListener() {
        @Override
        public void mouseClicked(MouseEvent e) {
            JPanel pnl = (JPanel) javax.swing.SwingUtilities
                    .getDeepestComponentAt((PanelsListener) e.getSource(),
                            e.getX(), e.getY());
            if (pnl != null && (!pnl.getName().equals("null.contentPane"))) {
                String name = pnl.getName();
                if (name != null) {
                    if (name.equals("First")) {
                        pl.setSize(500, 500);
                    } else if (name.equals("Second")) {
                        pl.setSize(800, 800);
                    }
                    pl.repaint();
                }
            }

        }

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

        }

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

        }

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

        }

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

        }
    });

    pl.setVisible(true);
}

}

编辑1

据我的评论了解,请尝试以下方法:

public class PanelsListener extends JFrame{

public static int  panel_identifier=0;

public static void main(String[] args) {
    PanelsListener pl = new PanelsListener();
    pl.setSize(700, 700);
    pl.setLayout(new FlowLayout());
    JPanel fp=new JPanel();
    fp.setSize(200,200);
    fp.setBorder(new LineBorder(Color.red));
    JPanel sp=new JPanel();
    sp.setSize(200,200);
    sp.setBorder(new LineBorder(Color.blue));

    pl.add(fp);
    pl.add(sp);

    //panel_identifier is used to specify which panel has been clicked.

    fp.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent me) {
           pl.panel_identifier=1;
           pl.mouseClicked(me);
        }
    });
    sp.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent me) {
            pl.panel_identifier=2;
             pl.mouseClicked(me);
        }
    });

    pl.setVisible(true);
}
protected void mouseClicked(MouseEvent me) {
    if(panel_identifier==1)
    {
        this.setSize(500, 500);
        panel_identifier=0;
    }
    else if(panel_identifier==2)
    {
        this.setSize(800, 800);
        panel_identifier=0;
    }

}
}

正常解决方案

公共类PanelsListener扩展了JFrame {

public static void main(String[] args) {
    PanelsListener pl = new PanelsListener();
    pl.setSize(700, 700);
    pl.setLayout(new FlowLayout());
    pl.add(new FirstPanel(pl));
    pl.add(new SecondPanel(pl));
    pl.setVisible(true);
}
}

class FirstPanel extends JPanel {

public FirstPanel(Frame frm) {
    setBorder(new LineBorder(Color.red));
    setSize(200, 200);
    addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            frm.setSize(500, 500);
            frm.repaint();
        }
    });
    addMouseMotionListener(new MouseAdapter() {
        @Override
        public void mouseDragged(MouseEvent e) {
            // do some action when mouse dragged over this panel
        }
    });
 }
 }

 class SecondPanel extends JPanel {

 public SecondPanel(Frame frm) {
    setBorder(new LineBorder(Color.blue));
    setSize(200, 200);
     addMouseListener(new MouseAdapter() {
     @Override
     public void mouseClicked(MouseEvent e) {
     frm.setSize(800,800);
     frm.repaint();
     }
     });

    addMouseMotionListener(new MouseAdapter() {
        @Override
        public void mouseDragged(MouseEvent e) {
            // do some action when mouse dragged over this panel
        }
    });
 }
 }