我正在制作 Master Mind 游戏,我用 JButton 填充了我的矩阵,以便人们可以单击它们来更改颜色。
现在我想将矩形按钮的形状更改为圆形,有没有一种方法可以一次全部更改,因为我使用循环创建了所有这些按钮。
最佳答案
以下是一些必须重写才能编辑组件形状的方法。 (包括示例代码)
protected void paintComponent(Graphics g)
{
if (getModel().isArmed()) {
g.setColor(Color.lightGray);
} else {
g.setColor(getBackground());
}
g.fillOval(0, 0, getSize().width-1,getSize().height-1);
super.paintComponent(g);
}
protected void paintBorder(Graphics g) {
g.setColor(getForeground());
g.drawOval(0, 0, getSize().width-1, getSize().height-1);
}
Shape shape;
public boolean contains(int x, int y) {
if (shape == null ||
!shape.getBounds().equals(getBounds())) {
shape = new Ellipse2D.Float(0, 0, getWidth(), getHeight());
}
return shape.contains(x, y);
}
关于java - 更改 JButton 的形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15361259/