开发者

create jlabel on fly

开发者 https://www.devze.com 2023-02-04 06:02 出处:网络
How can I configure which JLabel is pressed? String [] a={\"lable1\",\"label2\"}; for(int i=0;i<2;i++){

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());
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号