在 GUI 中使用不同的布局
这就是我的程序应该是什么样子,我对应该在哪里使用不同的布局有点困惑。
我有一个调用Panel 类的Window 类,而Panel 类调用InputPanel 和DisplayPanel 类。我的InputPanel 类调用我的DetailsPanel、CrimePanel 和ButtonPanel 类,以便它们构成“输入”选项卡下所看到的内容。我被告知整个窗口使用 BorderLayout,DetailsPanel(左面板)和 CrimePanel 应该使用 GridLayout。
这是否意味着我应该:
- 将 BorderLayout 代码放在 Panel 中,将 GridLayout 代码放在 CrimePanel 和 DetailsPanel 中,或者
- 将 BorderLayout 代码放在 Window 中,将 GridLayout 代码放在 Panel 中?
替代文本 http://img137.imageshack.us/img137/6422/93381955.jpg< /a>
This is what my program should look like and I'm a bit confused on where I should use different layouts.
I have a Window class which calls the Panel class and the Panel class calls the InputPanel and DisplayPanel classes. My InputPanel class calls my DetailsPanel, CrimePanel and ButtonPanel classes so they make up what is seen under the Input tab. I was told to use a BorderLayout for the whole window, and that the DetailsPanel (left panel) and CrimePanel should be GridLayout.
Does this mean I should:
- Put the BorderLayout code in Panel and the GridLayout code in CrimePanel and DetailsPanel or
- Put the BorderLayout code in Window and the GridLayout code in Panel?
alt text http://img137.imageshack.us/img137/6422/93381955.jpg
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,你的描述有点令人困惑(或者我今天还是太累了,或者还没有摄入足够的咖啡因)。您从其他人那里“调用”面板类的想法也有点奇怪。
但据我所知,你的第一个选择是正确的。
一般来说,您只是在运行时嵌套对象,因此它可能看起来有点像下面这样:
您通常在相应类的构造函数中执行此操作:
但是,我在这里看到一个小问题:由于
GridLayout
不允许组件跨越多列,您可能还需要在左侧的DetailsPanel
中嵌套其他面板。您可以使用具有所需功能的单个 GridBagLayout ,或者在其中嵌套其他面板:Okay, your description is a little bit confusing (or I'm still too tired today or didn't have enough caffeine yet). Your notion of "calling" panel classes from others is also a little weird.
But as far as I can see it, your first option is the correct one.
In general you just nest the objects at runtime, so it might look a little like the following:
You usually do this in the constructor of the appropriate class:
However, I see a small problem here: Since
GridLayout
doesn't allow components to span multiple columns you may need to nest other panels in theDetailsPanel
on the left as well. You can get away with a singleGridBagLayout
which has the needed capabilities, or you nest other panels there:根据您的描述,选项 1 应该有效。为输入
JPanel
指定一个BorderLayout
,其中详细信息、犯罪和按钮JPanel
(分别位于西边、北边和南边)具有 <代码>GridLayouts。From your description, option 1 should work. Give the Input
JPanel
aBorderLayout
, with the Details, Crime and ButtonJPanel
s (at the west, north and south, respectively) havingGridLayout
s.首先,您需要将 JTabbedPane 放入窗口中以包含两个选项卡(输入和显示),每个选项卡由一个 JPanel 组成。
正如 Joannes 所描述的,输入面板可以使用 BorderLayout 进行细分;另一种选择是 Java 中引入的 GroupLayout 6、虽然很强大,但是很难让人集中注意力。它可用于在一个面板中布局整个选项卡。
First of all you will need to put a JTabbedPane into the Window to contain your two tabs (input and display), each consisting of a JPanel.
The input panel could be subdivided using a BorderLayout as Joannes describes; another alternative is the GroupLayout introduced in Java 6, which is very powerful, but hard to wrap your mind around. It could be used to layout the entire tab in one panel.