How can I configure which JLabel
is pressed?
String [] a={"lable1","label2"};
for(int i=0;i<2;i++){
JLabel lb=new JLabel("");
lb.setText(a[i]);
scanPanel.add(lb);
add(scanPanel);
lb.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
//HERE I NEED THE SOURCE NAME:)
System.out.prin开发者_开发知识库tln(e.getSource());
}
});
You get one free answer. If you can't take the time to format a question properly even after you've been asked to so I'm not going to waste time helping.
Still not sure what the "source name" is, but I'll make a wild guess:
JLabel label = (JLabel)e.getSource();
System.out.println(label.getText());
精彩评论