java JLabel 点击事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4894112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 08:31:30  来源:igfitidea点击:

JLabel click event

javaswing

提问by jjj

If you have two JLabels in a JFrameboth with the same MouseListenerclick event added to them, how can you tell which JLabelwas clicked without creating a second actionlistener?

如果你有两个JLabelsJFrame都添加了相同的MouseListener点击事件,你如何JLabel在不创建第二个 actionlistener 的情况下判断哪个被点击了?

Note: both labels have the same text written on them so that cannot be used to tell them apart.

注意:两个标签上都写有相同的文字,因此无法区分它们。

回答by Romain Hippeau

This will get you a reference to the component ...

这将为您提供对组件的引用......

public void mousePressed(MouseEvent e) 
{
JComponent reference = e.getComponent();
}

For a more complete description look at the Swing Tutorial on MouseListeners

有关更完整的描述,请查看MouseListeners上的Swing 教程

回答by ColinD

Just make the two JLabels fields and then check the source of the MouseEvent:

只需制作两个JLabels 字段,然后检查 的来源MouseEvent

if (e.getSource() == firstLabel) {
  ...
} else if (e.getSource() == secondLabel) {
  ...
}

回答by user2276611

Since you are using JLabel which comes from JComponent it has a method called putClientProperty("myValue", myValue). You could put some unique identifier in there upon JLabel creation and retrieve it at event time with getClientProperty("myValue") and then test it.

由于您使用的是来自 JComponent 的 JLabel,它有一个名为 putClientProperty("myValue", myValue) 的方法。您可以在创建 JLabel 时在其中放置一些唯一标识符,并在事件时间使用 getClientProperty("myValue") 检索它,然后对其进行测试。

回答by Ephraim

take for example, a keyboard. what I did when I created one, is sent over the button to the action listener. I then had the action listener do a myButton.getText(); and I would just type the text onto my screen (a JTextfield in my case). in your main method write:

以键盘为例。我在创建一个时所做的,通过按钮发送到动作侦听器。然后我让动作监听器做一个 myButton.getText(); 我只会在我的屏幕上输入文本(在我的例子中是 JTextfield)。在你的主要方法中写:

JTextField textfield = new JTextField("", 37);  
JButton myButton = new new JButton("button text here");  
myButton.addActionListener(new MyActionListener (textfield, myButton));

the full action listener would look like this:

完整的动作侦听器如下所示:

//thisMethod is for a keyboard typing into a JTextfield  
import javax.swing.JTextField;  
import java.awt.event.ActionListener;  
import java.awt.event.ActionEvent;  
import java.lang.*;    
class MyActionListener implements ActionListener {  
JTextField textfield;  
MyActionListener(JTextField textfield, JButton button) {  
    this.textfield = textfield;  
}  
public void actionPerformed(ActionEvent e) {  
        String letter = javax.xml.bind.DatatypeConverter.printString(textfield.getText()).concat(button.getText());
        textfield.setText (letter);  
    }  
 }

this same priciple applies when refering to which button was pressed. you could send over a string, and that string could be used in conditional statements to determine which button was pressed.

当提到按下了哪个按钮时,同样的原则也适用。您可以发送一个字符串,该字符串可以在条件语句中使用以确定按下了哪个按钮。