布置 JPanel 来制作简单的 GUI
首先,这或多或少是我的第一个 GUI,而且我学习 Java 的时间不超过一周,因此它可能包含一些严重的编程错误。
我现在拥有的是: 按钮和标签是 OptionPanel 的一部分,位于左侧,DrawingPanel 大小约为 5x5 像素,位于右侧。
我想做的是一个简单的测试,让我更熟悉 GUI。当用户单击相应的按钮时,矩形应该是可移动的并可调整大小: http://www.upload.ee/image/612005/JFrame2.jpg
现在我有:
JFrame MainFrame - 制作 JFrame (不使用 setSize 函数。使用 .pack() 代替。不确定)
JPanel MergedPanel - FlowLayout - 将 JPanel OptionsPanel 和 JPanel DrawingPanel 添加到一起并注入到 JFrame MainFrame
JPanel DrawPanel - 这个 JPanel 负责绘制矩形。 JPanel OptionPanel - FlowLayout - 此 JPanel 负责按钮。
请帮忙。
first of all, this is more or less my first GUI and ive learned Java for not more then a week, so it might contain some serious programming errors.
What i have right now is:
Buttons and labels are part of OptionPanel and are on the left, DrawingPanel is about 5x5 px in size and is on the right.
What I am trying to do is a simple test, to get me more familiar with the GUI. The rectangle should be movable and re-sizeable by the user when clicking the respective buttons:
http://www.upload.ee/image/612005/JFrame2.jpg
Right now i have:
JFrame MainFrame - Makes JFrame (Not using the setSize function. using .pack() instead. not sure about it)
JPanel MergedPanel - FlowLayout - Adds JPanel OptionsPanel and JPanel DrawingPanel together and gets injected to JFrame MainFrame
JPanel DrawPanel - This JPanel is responsible of drawing the rectangle.
JPanel OptionPanel - FlowLayout - This JPanel is responsible of the buttons.
Help please.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您永远不应该在代码中调用
setSize()
。在 Java 中,您可以使用 布局管理器 来执行以下操作:布局(阅读该教程)。子类化
JPanel
来实现组成 UI 的不同部分是一种很好的做法,但不应该过度(有一个 UI 类添加 3 个其他普通JPanel
就可以了)用于布局目的的实例)。You should never call
setSize()
in your code. In Java, you use layout managers to do the layout (read that tutorial).Subclassing
JPanel
to implement different parts of which the UI is composed is a good practice, but should not be overdone (it's fine to have an UI class that adds 3 other plainJPanel
instances to itself for layout purposes).查看 MiG Layout :可以用它轻松制作 java 布局。
Check out MiG Layout : one can make pretty easily java layouts with that.