【发布时间】:2023-03-24 13:12:01
【问题描述】:
这很简单,但我似乎无法弄清楚,我想定义我在 Y 轴上有多少空间,并定义我想要多少个按钮以及每个按钮之间的间隙,它应该弄清楚每个按钮的高度并放置按钮。
例如:
public void drawButtons(int buttons, int gap, int ySpace, int x, int width){
for(int i = 0; i < buttons; i++){
//Workout the y and the height here!
int height = (ySpace / buttons) - (gap * (buttons - 1))
//Other stuff here!
g.fillRect(x, theY, width, height);
}
}
按钮的 x 和宽度不是问题,我稍后会编辑此方法以在按钮的中心绘制文本并使用mousePressed 方法来确定按钮是否被单击。
【问题讨论】:
-
您在
y空间中有n按钮并希望将它们均匀分布? -
是的,每个按钮之间都有间隙
-
我的程序不是应用程序,而是游戏,所以我想制作自定义按钮
标签: java button for-loop methods