按鈕的下面的網格定義爲畫的特定按鈕:從按鈕的格子
JButton button_x = new RoundButton();
其中RoundButton
定義爲:
public class RoundButton extends JButton {
public RoundButton(String label) {
super(label);
this.setContentAreaFilled(false);
Dimension size = this.getPreferredSize();
size.height = size.width = Math.max(size.height, size.width);
this.setPreferredSize(size);
}
@Override
protected void paintComponent(Graphics g) {
if(!GameState.getIfComplete()) { // If the game is not complete or has just started
this.setBorder(null);
g.setColor(Color.BLACK);
g.fillRect(0, 0, this.getSize().width, this.getSize().height);
if(this.getModel().isArmed()) {
g.setColor(Color.RED);
}else {
g.setColor(Color.GREEN);
}
g.fillOval(0,0,this.getSize().width-1,this.getSize().height-1);
super.paintComponent(g);
}else {
this.setBorder(null);
g.setColor(Color.BLACK);
g.fillRect(0, 0, this.getSize().width, this.getSize().height);
g.setColor(Color.WHITE);
g.fillOval(0,0,this.getSize().width-1,this.getSize().height-1);
super.paintComponent(g);
}
}
}
目前所有的按鈕都塗成綠色,但在一個cer上tain條件我想繪製白色的特定按鈕(這是else部分中的代碼)。例如,當!GameState.getIfComplete()
返回false
我想在第一列用白色繪製按鈕。所以我打電話repaint
爲:
buttons[0].repaint();
buttons[3].repaint();
buttons[6].repaint();
但這不起作用!在第一列中,其他一些按鈕也被塗成白色。這是爲什麼 ?
呼叫有什麼問題?我如何繪製一個特定的按鈕?
你或許應該只是創造彩色圖標及將它們設置爲一個標準的按鈕。爲了更快地獲得更好的幫助,請發佈[SSCCE](http://sscce.org/)。 – 2013-04-06 04:30:18
我確定我已經解決了調用'this.setBorder(null);'是一個可怕的壞主意。如果您不想設置邊框,請嘗試覆蓋'getBorder'方法並從中返回'null' – MadProgrammer 2013-04-06 04:46:22
@MadProgrammer是啊!我也解決了這個問題。它只是反映在這裏。 – saplingPro 2013-04-06 05:06:09