[英]Change the JButton's shape
I'm making the game Master Mind, I've filled my matrix up with JButtons so people can click them to change the color. 我正在制作游戏Master Mind,我在矩阵中填充了JButtons,以便人们可以单击它们来更改颜色。
Now I want to change the shape of my rectangular buttons to circles, is there a way I can change them all at once since I worked with a loop to create all them. 现在,我想将矩形按钮的形状更改为圆形,因为我使用循环创建了所有按钮,因此可以立即更改所有按钮。
Here are some methods that have to be overwritten to edit the shape of a component. 这是一些必须重写以编辑组件形状的方法。 (Including sample code) (包括示例代码)
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);
}
You can search Google for a tutorial about this. 您可以在Google上搜索有关此内容的教程。
This is an easy tutorial: How to change the shape of a JButton 这是一个简单的教程: 如何更改JButton的形状
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.