设置和禁用JToggleButton的图标

嗨,我想做一个匹配的记忆游戏,我使用JToggleButton。 最主要的是当我按下它必须显示一个图片,我必须find其他相同的图片。 所以问题是当我创build一个没有任何图标的button,我不能使用其他方法例如.setRollOverIcon() .setPressedIcon()等,所以我赞赏,如果你能帮助我。 并感谢:)

 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ButtonsIcon extends JFrame { private static final long serialVersionUID = 1L; private ImageIcon errorIcon = (ImageIcon) UIManager.getIcon("OptionPane.errorIcon"); private ImageIcon infoIcon = (ImageIcon) UIManager.getIcon("OptionPane.informationIcon"); private ImageIcon warnIcon = (ImageIcon) UIManager.getIcon("OptionPane.warningIcon"); public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { ButtonsIcon t = new ButtonsIcon(); } }); } public ButtonsIcon() { setLayout(new GridLayout(1, 1, 4, 4)); final JToggleButton toggleButton = new JToggleButton(); //toggleButton.setIcon((errorIcon)); toggleButton.setRolloverIcon((infoIcon)); toggleButton.setPressedIcon(warnIcon); toggleButton.setDisabledIcon(warnIcon); toggleButton.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { if (toggleButton.isSelected()) { } else { } } }); add(toggleButton); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); setVisible(true); } } 

一种方法是执行以下操作:

  • 使用select的状态来指示是否显示或隐藏Icon
  • 使用启用状态来指示一对已被匹配。

代码大纲:

 /** Handle ItemEvents. */ @Override public void itemStateChanged(ItemEvent e) { GameButton gb = (GameButton) e.getItem(); gb.setState(); } /** Remove a and b from play. */ private void retirePair(GameButton a, GameButton b) { a.setSelected(true); a.setEnabled(false); b.setSelected(true); b.setEnabled(false); } class GameButton extends JToggleButton { ... public void setState() { if (this.isSelected() || !this.isEnabled()) { this.setIcon(icon); } else { this.setIcon(hidden); } } } 

1) JToggleButton更好地实现ItemListener

2)这里是SSCCE

在这里输入图像描述

 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ButtonsIcon extends JFrame { private static final long serialVersionUID = 1L; private Icon errorIcon = UIManager.getIcon("OptionPane.errorIcon"); private Icon infoIcon = UIManager.getIcon("OptionPane.informationIcon"); private Icon warnIcon = UIManager.getIcon("OptionPane.warningIcon"); public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { ButtonsIcon t = new ButtonsIcon(); } }); } public ButtonsIcon() { setLayout(new GridLayout(2, 2, 4, 4)); JButton button = new JButton(); button.setBorderPainted(false); button.setBorder(null); button.setFocusable(false); button.setMargin(new Insets(0, 0, 0, 0)); button.setContentAreaFilled(false); button.setIcon((errorIcon)); button.setRolloverIcon((infoIcon)); button.setPressedIcon(warnIcon); button.setDisabledIcon(warnIcon); add(button); JButton button1 = new JButton(); button1.setBorderPainted(false); button1.setBorder(null); button1.setFocusable(false); button1.setMargin(new Insets(0, 0, 0, 0)); button1.setContentAreaFilled(false); button1.setIcon((errorIcon)); button1.setRolloverIcon((infoIcon)); button1.setPressedIcon(warnIcon); button1.setDisabledIcon(warnIcon); add(button1); button1.setEnabled(false); final JToggleButton toggleButton = new JToggleButton(); toggleButton.setIcon((errorIcon)); toggleButton.setRolloverIcon((infoIcon)); toggleButton.setPressedIcon(warnIcon); toggleButton.setDisabledIcon(warnIcon); toggleButton.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { if (toggleButton.isSelected()) { } else { } } }); add(toggleButton); final JToggleButton toggleButton1 = new JToggleButton(); toggleButton1.setIcon((errorIcon)); toggleButton1.setRolloverIcon((infoIcon)); toggleButton1.setPressedIcon(warnIcon); toggleButton1.setDisabledIcon(warnIcon); toggleButton1.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { if (toggleButton1.isSelected()) { } else { } } }); add(toggleButton1); toggleButton1.setEnabled(false); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); setVisible(true); } }