开发者

Add and remove an icon on a JLabel

开发者 https://www.devze.com 2022-12-20 04:52 出处:网络
hi i have a label that i have set a icon for it, i want to remove this icon after clicking on a button, what is the method for 开发者_开发知识库it?// Create icon

hi i have a label that i have set a icon for it, i want to remove this icon after clicking on a button, what is the method for 开发者_开发知识库it?


// Create icon
Icon icon = new ImageIcon(getClass().getResource("/foo/bar/baz.png"));

// Create label
final JLabel lbl = new JLabel("Hello, World", icon, JLabel.LEFT_ALIGNMENT);

// Create button
JButton btn = new JButton("Click Me");
btn.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent evt) {
    // Remove icon when button is clicked.
    lbl.setIcon(null);

    // **IMPORTANT** to call revalidate() to cause JLabel to resize and be repainted.
    lbl.revalidate();
  }
});


label.setIcon(null) 

in the event handler that handles the button click, if you're using Swing.

0

精彩评论

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