在 Java JFrame 中绘制多个正方形并访问其中的每一个?
我基本上想将 300 x 300 JFrame 窗口分成大约 30 个正方形(无需单独绘制它们)并能够访问每个正方形(也许将它们放入数组/数组列表/堆栈/向量/等中)。如果不写drawRect(x, y, width, height) 30次,这可能吗?提前致谢。
I basically want to divide a 300 by 300 JFrame window into like 30 squares (without drawing them all separately) and be able to access each of the squares (maybe put them into an array/arraylist/stack/vector/etc). Is this possible without writing drawRect(x, y, width, height) 30 times? Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将组件保存在 ArrayList 中并使用 GridLayout
和以下代码片段:
其中 x 和 y 分别表示行数和列数。
之后,您可以将组件添加到 JFrame 对象的内容中。
示例代码:
You can keep your components in an ArrayList and get use of GridLayout
and the following snippet:
where x and y denotes the number of rows and columns respectively.
After that you can add components to the content of your JFrame object.
Sample code:
使用布局管理器
http://docs.oracle.com/javase/tutorial /uiswing/layout/using.html
use a layout manager
http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html
为什么不选择 JTable 而不是这个呢?
Why not go for a JTable instead of this ?