秋千的 BoxModel 如何工作?
假设我想创建一个简单的计算器。它由 3 个字段组成。用于显示结果的文本字段、用于选择系统的带有复选框的字段以及带有数字的字段。
我应该为每个元素使用什么样的组件? 如何在窗口中定位元素? 如何在组件内定位元素(即复选框)?
这就是我想要实现的目标。
Let's say I would like to create a simple calculator. It consists of 3 fields. Text field to show result, field with checkboxes to select system and field with numbers.
What kind of component should I use for each element ?
How can I position elements in my window ?
How can I position elements inside component (ie checkboxes) ?
This is what I'm trying to achieve.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将使用
JTextField
作为数字窗口JRadioButton
作为单选按钮,使用JButton
作为按钮。组件的布局应该服从所谓的布局管理器。 (查看使用布局管理器。在这种情况下,
GridLayout
和/或GridBagLayout
就可以了。此代码应该可以帮助您入门:
I would use
JTextField
for the number-windowJRadioButton
for the radio buttons, andJButton
for the buttons.The layout of the components should be deferred to a so called layout-manager. (Have a look at Using Layout Managers. In this case a
GridLayout
and/or aGridBagLayout
would do fine.This code should get you started:
首先,您首先确定
您应该使用 LayoutManager
来排列您的三个字段。GridBagLayout
肯定会做你想做的事,但很难编程。您可以尝试使用更简单的BorderLayout
,这将使您的应用程序在调整大小时看起来很奇怪。您可以使用GroupLayout
< /a> 也是。BoxLayout
、GridLayout
和FlowLayout
不是您想要使用的。现在您有很多选择来布置顶级元素。使用
JTextField
的结果。使用JCheckBox
对于复选框,您将其放入带有蚀刻边框的JPanel
内部(通过JPanel.setBorder(BorderFactory.createEtchedBorder())
)和FlowLayout
代码>.不要忘记将复选框放在复选框组
。最后但并非最不重要的一点是,使用JPanel
对呃按钮的JButton
进行分组。使用GridLayout
(5行,4列)来排列这些按钮。First off, you start with determining, which
LayoutManager
you should use, to arrange your three fields.GridBagLayout
will definitely do what you want, but is quite hard to program. You could try to get away with the simplerBorderLayout
, which will make your application look odd, while resizing. You can useGroupLayout
too.BoxLayout
,GridLayout
andFlowLayout
are not what you want to use. Now you have a lot of options, to lay out you top elements.Use a
JTextField
for the result. UseJCheckBox
for the checkboxes, which you put insider aJPanel
with an etched border (viaJPanel.setBorder(BorderFactory.createEtchedBorder())
) and aFlowLayout
. Don't forget to put the checkboxes in aCheckboxGroup
. Last but not least use aJPanel
to group theJButton
s for the, uhh, buttons. Use aGridLayout
(5 rows, 4 columns) to arrange these buttons.