2012-05-21 29 views

回答

5
yourButton.addActionListener(new ActionListener() { 
@Override 
    public void actionPerformed(ActionEvent e) { 
     yourButton.setIcon(new ImageIcon("yourImage")); 
    } 
}); 

當你點擊JButton時會調用ActionListener。這種方式最常用。

7

我做棋盤遊戲項目,我代表由Jbuttons中的細胞。

  • 使用JToggleButton遊戲基於按鈕陣列和鼠標事件,而不是JButton

  • 使用ButtonModel代替任何XxxListener

  • JButtonJToggleButton實施了API在這些方法直接

setIcon(Icon i); 
setRolloverIcon(Icon i); 
setPressedIcon(Icon i); 
setDisabledIcon(Icon i); 
4

作爲替代方案,使用Unicode字形也考慮setText()所示here