简体   繁体   中英

How to change the icon of jLabel in code in java?

im trying to change the icon of a jLabel named "mainDot". i have another folder in the source packages (named "Images") that contains the icons i want to put on the mainDot when I press it.

private void mainDotMousePressed(java.awt.event.MouseEvent evt) {                                     
    mainDot.setIcon(DOT_SMALL.jpg);
}

How would I be able to change the icon of mainDot?

public Test() {
    JLabel t=new JLabel();
    this.setLayout(new BorderLayout());
    t.setIcon(new ImageIcon("Path to image goes here"));
    this.add(t);
    this.pack();
    this.setVisible(true);  
}

this one is working. and as suggested use an actionlistener to do it on button press

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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