简体   繁体   English

如何使用 MouseListener 单击 JLabel?

[英]How do I click a JLabel using MouseListener?

I am trying to make this program for minecraft, and now im just getting started.我正在尝试为我的世界制作这个程序,现在我才刚刚开始。 I want that if you click a label, it will check what label is it and will do something.我希望如果你点击一个标签,它会检查它是什么标签并会做一些事情。

addMouseListener(new MouseAdapter() { 
          public void mousePressed(MouseEvent me) { 

            System.out.println(me.getX()+", "+me.getY()+"."); 
            Object source = me.getSource();
            int intx =  me.getX();
            int inty = me.getY();

            if(me.getX()>=1 && me.getY()>=1 && me.getX()<=70 && me.getY()<=45){
                permissionsframe.setLocation(810,250);
                System.out.println(p1p.length);
                permissionsframe.pack();
                permissionsframe.setSize(200, 200);
                permissionsframe.setVisible(true);
                JLabel playerperms = new JLabel("Player "+p1s+" has "+p1p.length+" permissions.");
                playerperms.setBounds(1, 1, 150, 150);
                permissionsframe.add(playerperms);
                System.out.println("You chose "+player1.getText()+".");
            }
            else{
                System.out.println("You did not click any label.");
            }

        }
    });

This selection area is adapted to the name I have now - NonameSL.这个选择区域适应了我现在的名字——NonameSL。 But if the name will be longer or shorter, the selection area will obviuosly be different...但是如果名称更长或更短,则选择区域显然会有所不同......

Is there a way to get the excact label?有没有办法获得精确的标签? I tried if(source.equals(player1)) (Player 1 is the label) but I placed the label in 1, 1 and I have to click the excact point that I defined the label in, X=1, Y=1.我试过if(source.equals(player1)) (玩家 1 是标签),但我将标签放在 1, 1 中,我必须单击我在其中定义标签的精确点,X=1, Y=1。 How can I make a mouse listener listen to a label?如何让鼠标侦听器收听标签?

There is no need to check if the mouse coordinates are inside your JLabel. 无需检查鼠标坐标是否在JLabel中。 You can bind a Listener to every JLabel an process the click/press event in your MyMouseListener.class 您可以将一个Listener绑定到每个JLabel上的MyMouseListener.class中的click / press事件MyMouseListener.class

To do so: 为此:

You have to add the MouseListener to every JLabel: 您必须将MouseListener添加到每个JLabel:

MyMouseListener myMouseListener = new MyMouseListener();

label01.setName("name01");
label01.addMouseListener(myMouseListener);

label02.setName("name02");
label02.addMouseListener(myMouseListener);

To identify the JLabel you could do something like this: 要标识JLabel,您可以执行以下操作:

class MyMouseListener extends MouseAdapter {

    @Override
    public void mouseClicked(MouseEvent e) {
         JLabel l = (JLabel) e.getSource();

         if(l.getName().equals("name01"))
             doSomething01();
         else if(l.getName().equals("name02"))
             doSomething02();
    }
}

The correct way to do this is to use the .getComponent() method instead of the .getSource() since this is MouseEvent which is something different than ActionEvent .正确的方法是使用.getComponent()方法而不是.getSource()因为这是MouseEvent ,它不同于ActionEvent

Implement the Listener to your class:为您的类实现监听器:

public class Main implements MouseListener {

public static void main(String[] args) {
    
    //setup JFrame and some label and then 

    label.addMouseListener(this);
}

@Override
    public void mousePressed(MouseEvent e) {
        if (e.getComponent().equals(label)) {

        System.out.println("clicked");
    }
}
//the other orderride methods..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM