在 GUI 中使用不同的布局

发布于 2024-08-08 11:59:10 字数 611 浏览 3 评论 0原文

这就是我的程序应该是什么样子,我对应该在哪里使用不同的布局有点困惑。

我有一个调用Panel 类的Window 类,而Panel 类调用InputPanel 和DisplayPanel 类。我的InputPanel 类调用我的DetailsPanel、CrimePanel 和ButtonPanel 类,以便它们构成“输入”选项卡下所看到的内容。我被告知整个窗口使用 BorderLayout,DetailsPanel(左面板)和 CrimePanel 应该使用 GridLayout。

这是否意味着我应该:

  1. 将 BorderLayout 代码放在 Panel 中,将 GridLayout 代码放在 CrimePanel 和 DetailsPanel 中,或者
  2. 将 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:

  1. Put the BorderLayout code in Panel and the GridLayout code in CrimePanel and DetailsPanel or
  2. Put the BorderLayout code in Window and the GridLayout code in Panel?

alt text http://img137.imageshack.us/img137/6422/93381955.jpg

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

又爬满兰若 2024-08-15 11:59:11

好吧,你的描述有点令人困惑(或者我今天还是太累了,或者还没有摄入足够的咖啡因)。您从其他人那里“调用”面板类的想法也有点奇怪。

但据我所知,你的第一个选择是正确的。

一般来说,您只是在运行时嵌套对象,因此它可能看起来有点像下面这样:

InputPanel (has BorderLayout)
+--DetailsPanel (put in BorderLayout.WEST; has GridLayout)
|  +--nameLabel
|  +--nameTextField
|  +--...
+--CrimePanel (put in BorderLayout.NORTH; has GridLayout)
|  +--murderRadioButton
|  +--arsonRadioButton
|  +--...
+--ButtonPanel (put in BorderLayout.CENTER; has GridLayout)
   +--button

您通常在相应类的构造函数中执行此操作:

public class InputPanel {
    public InputPanel() {
        this.setLayout(new BorderLayout());
        this.add(new DetailsPanel(), BorderLayout.WEST);
        this.add(new CrimePanel(), BorderLayout.NORTH);
        this.add(new ButtonPanel(), BorderLayout.CENTER);
    }
}

public class DetailsPanel {

    JLabel nameLabel;
    JTextField nameField;
    // ...

    public DetailsPanel() {
        this.setLayout(new GridLayout(5, 1));

        nameLabel = new JLabel("Name");
        nameField = new JTextField();
        // ...

        this.add(nameLabel);
        this.add(nameField);
        // ...
    }
}

...

但是,我在这里看到一个小问题:由于 GridLayout不允许组件跨越多列,您可能还需要在左侧的 DetailsPanel 中嵌套其他面板。您可以使用具有所需功能的单个 GridBagLayout ,或者在其中嵌套其他面板:

DetailsPanel (has BorderLayout)
+--panel1 (has GridLayout with 2 rows, 1 column; put in BorderLayout.NORTH)
|  +--nameLabel
|  +--nameField
+--panel2 (has GridLayout with 3 rows, 2 columns; put in BorderLayout.CENTER)
   +--dayField
   +--dayLabel
   +--monthField
   +--...

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:

InputPanel (has BorderLayout)
+--DetailsPanel (put in BorderLayout.WEST; has GridLayout)
|  +--nameLabel
|  +--nameTextField
|  +--...
+--CrimePanel (put in BorderLayout.NORTH; has GridLayout)
|  +--murderRadioButton
|  +--arsonRadioButton
|  +--...
+--ButtonPanel (put in BorderLayout.CENTER; has GridLayout)
   +--button

You usually do this in the constructor of the appropriate class:

public class InputPanel {
    public InputPanel() {
        this.setLayout(new BorderLayout());
        this.add(new DetailsPanel(), BorderLayout.WEST);
        this.add(new CrimePanel(), BorderLayout.NORTH);
        this.add(new ButtonPanel(), BorderLayout.CENTER);
    }
}

public class DetailsPanel {

    JLabel nameLabel;
    JTextField nameField;
    // ...

    public DetailsPanel() {
        this.setLayout(new GridLayout(5, 1));

        nameLabel = new JLabel("Name");
        nameField = new JTextField();
        // ...

        this.add(nameLabel);
        this.add(nameField);
        // ...
    }
}

...

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 the DetailsPanel on the left as well. You can get away with a single GridBagLayout which has the needed capabilities, or you nest other panels there:

DetailsPanel (has BorderLayout)
+--panel1 (has GridLayout with 2 rows, 1 column; put in BorderLayout.NORTH)
|  +--nameLabel
|  +--nameField
+--panel2 (has GridLayout with 3 rows, 2 columns; put in BorderLayout.CENTER)
   +--dayField
   +--dayLabel
   +--monthField
   +--...
倒数 2024-08-15 11:59:11

根据您的描述,选项 1 应该有效。为输入 JPanel 指定一个 BorderLayout,其中详细信息、犯罪和按钮 JPanel(分别位于西边、北边和南边)具有 <代码>GridLayouts。

From your description, option 1 should work. Give the Input JPanel a BorderLayout, with the Details, Crime and Button JPanels (at the west, north and south, respectively) having GridLayouts.

慵挽 2024-08-15 11:59:11

首先,您需要将 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文