简体   繁体   中英

How to add a icon in JButton

I'm trying to create a icon in a button in JButton, the button should look like this
在此处输入图片说明

But when i add this in my code it looks like this

在此处输入图片说明

Here is the button portion of my code

private JButton backButton = new JButton();
backButton.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {
    actionBack();
  }
});
backButton.setIcon(new ImageIcon(Hello.class.getResource("/239706184.png")));
backButton.setEnabled(false);
buttonPanel.add(backButton);

Your button is dark because you set eneable to false

remove this line:

backButton.setEnabled(false);

If you set backButton.setEnabled(false); then it will be showing in dark state.

Try it set to true.

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