[英]A method for automatically generating JButtons?
I am currently a java newbie of sorts writing a program that is, essentially, a board game, almost identical to checkers. 我目前是一名Java新手,正在编写实质上是一个棋盘游戏,几乎与跳棋完全相同的程序。 Unfortunately, this also means I need one hundred JButtons for the checkerboard.
不幸的是,这也意味着我需要一百个JButton用于棋盘。 I know there is a way to have automatically generated JButtons, I've seen it done.
我知道有一种方法可以自动生成JButton,我已经看到了。 I have no idea how I would go about this, though.
不过,我不知道该如何处理。 Any help would be appreciated!
任何帮助,将不胜感激!
If you want to generate a board game, you should use a GridLayout
which will help you a lot positioning your buttons with minimal effort. 如果要生成棋盘游戏,则应使用
GridLayout
,这将帮助您以最小的努力来定位按钮。 Something like this: 像这样:
public JPanel createBoardGame()
JPanel boardGame = new JPanel(new GridLayout(numberOfRows,numberOfColumns));
for (int i=0; i<numberOfRows*numberOfColumns; i++) {
boardGame.add(new JButton(""));
}
}
Here is a nice article that should help you a lot implementing your needs. 这是一篇很好的文章 ,应该可以帮助您实现自己的需求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.