1
我正在制作游戏Master Mind,我用JButton填充了我的矩阵,以便人们可以点击它们来更改颜色。更改JButton的形状
现在我想将矩形按钮的形状更改为圆形,是否有一种方法可以一次将它们全部更改,因为我使用循环来创建所有这些按钮。
我正在制作游戏Master Mind,我用JButton填充了我的矩阵,以便人们可以点击它们来更改颜色。更改JButton的形状
现在我想将矩形按钮的形状更改为圆形,是否有一种方法可以一次将它们全部更改,因为我使用循环来创建所有这些按钮。
您可以在Google上搜索关于此的教程。
这是一个简单的教程:How to change the shape of a 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);
}
要注意的是[只有链路答案往往被删除(http://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-在其他地方,真的好,答案) – 2015-07-15 07:56:18