开发者

create jlabel on fly

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜