以下按钮网格定义为:
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);
}
}
}
目前所有按钮都涂成绿色,但在某种情况下,我想将特定按钮涂成白色(这是 else 部分中的代码)。例如,当!GameState.getIfComplete()
返回时,false
我想将第一列中的按钮涂成白色。所以我称之为repaint
:
buttons[0].repaint();
buttons[3].repaint();
buttons[6].repaint();
但这不起作用!在第一列中,其他一些按钮也被涂成白色。这是为什么 ?
通话有什么问题?如何绘制特定按钮?