关于制作某种格式的JAVA GUI的问题

发布于 2024-10-05 06:04:39 字数 249 浏览 5 评论 0原文

我正在尝试制作一个看起来像这样的 GUI:

我只知道如何使用具有 5 空间的 BorderLayout按钮。北、西、中、东、南。

由于我需要在顶线上有 6 个组件,因此这种方法行不通。我不知道如何制作才能在第一行拥有超过 1 个组件。是否有其他我可以使用的布局,或者是否有某种方法可以操纵 BorderLayout,以便我可以将 6 个组件放在顶行?

I am trying to make a GUI that looks something like this:

I only know how to use the BorderLayout which has space for 5 buttons. North, West, Center, East, and South.

Since I need to have 6 components on the top line, this approach can't work. I'm not sure how to make it so that I can have more than 1 component on the top line. Are there other layouts that I can use or is there some way I can manipulate BorderLayout so that I can put 6 components on the top line?

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

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

发布评论

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

评论(4

迟月 2024-10-12 06:04:39

您需要做的是将组件嵌套在其他组件内。例如,顶部(北)应该是一个 JPanel。该 JPanel 将包含顶部的 6 个组件。

该代码可能类似于以下内容:

JPanel northPane = new JPanel();
northPane.add(new JLabel("Principle: "));
northPane.add(principleTextBox);
... and so on
mainPanel.setLayout(new BorderLayout());
mainPanel.add(northPanel, BorderLayout.NORTH);

Center 组件可能是另一个包含两个中心按钮的 JPanel。 South 组件将是另一个包含单个 JLabel 或仅包含 JLabelJPanel

如果您不必为主面板使用 BorderLayout,那么使用 BoxLayout 可能会更容易。

What you need to do is nest components inside of other components. For example, the top (North) should be one JPanel. That JPanel will contain the 6 components on the top.

The code may look similar to the following:

JPanel northPane = new JPanel();
northPane.add(new JLabel("Principle: "));
northPane.add(principleTextBox);
... and so on
mainPanel.setLayout(new BorderLayout());
mainPanel.add(northPanel, BorderLayout.NORTH);

The Center component will probably be another JPanel containing the two center buttons. And the South component will be another JPanel containing the single JLabel or simply the JLabel.

If you don't have to use a BorderLayout for the main panel, it may be easier to use a BoxLayout.

我再次转向 miglayout,绝对是最好的 Java 布局管理器。没有嵌套的 JPanel,只是使用基于字符串的约束的简单布局。

alt text

调试模式打开:
alt text

调整窗口大小后(请注意文本字段大小的比例保持不变)
替代文本

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import net.miginfocom.swing.MigLayout;

/**
 *
 * @author nicholasdunn
 */
public class InterestCalculator extends JPanel {

    public InterestCalculator() {
        super(new MigLayout("debug, fill", "align center"));
        // Make 6 components cram into one cell
        add(new JLabel("Principal:"), "split 6");
        // This textfield grows at twice the normal rate
        add(new JTextField(), "growx 200");
        add(new JLabel("Interest rate (percentage):"));
        // This one at a normal rate
        add(new JTextField(), "growx 100");
        add(new JLabel("Years:"));
        // This one at half the normal rate
        add(new JTextField(), "growx 50, wrap");

        // The row with the two buttons
        add(new JButton("Compute simple interest"), "split 2");
        add(new JButton("Compute compound interest"), "wrap");

        // The result label
        add(new JLabel("The result with simple interest would be"));
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new InterestCalculator();
        frame.add(panel);
        frame.pack();
        frame.setVisible(true);
    }
}

Once again I turn to miglayout, the absolute best layout manager for Java. No nested JPanels, just a simple layout using string based constraints.

alt text

With debug mode on:
alt text

After resizing the window (note the ratio of the size of the textfields remains the same)
alt text

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import net.miginfocom.swing.MigLayout;

/**
 *
 * @author nicholasdunn
 */
public class InterestCalculator extends JPanel {

    public InterestCalculator() {
        super(new MigLayout("debug, fill", "align center"));
        // Make 6 components cram into one cell
        add(new JLabel("Principal:"), "split 6");
        // This textfield grows at twice the normal rate
        add(new JTextField(), "growx 200");
        add(new JLabel("Interest rate (percentage):"));
        // This one at a normal rate
        add(new JTextField(), "growx 100");
        add(new JLabel("Years:"));
        // This one at half the normal rate
        add(new JTextField(), "growx 50, wrap");

        // The row with the two buttons
        add(new JButton("Compute simple interest"), "split 2");
        add(new JButton("Compute compound interest"), "wrap");

        // The result label
        add(new JLabel("The result with simple interest would be"));
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new InterestCalculator();
        frame.add(panel);
        frame.pack();
        frame.setVisible(true);
    }
}
三岁铭 2024-10-12 06:04:39

如果我要重新创建该 UI,我会从使用 3 行 1 列的 GridLayout 的 JPanel 开始。在每一列中,我将添加一个子 JPanel。

然后,对于每一行,我将使用 GridBagLayout 来定位组件。

If I were recreating that UI, I would start with a JPanel using a GridLayout with 3 rows and 1 column. In each column I would add a child JPanel.

Then for each row I would use a GridBagLayout to position the components.

短暂陪伴 2024-10-12 06:04:39

这里是有关布局管理器的教程。

请记住,您始终可以向 JPanel 添加多个元素并将特定布局应用于该 JPanel。然后您可以嵌套面板(在其他面板内添加面板)。

Here is a tutorial about layout managers.

Remember that you can always add several elements to a JPanel and apply a specific layout to that JPanel. Then you can nest panels(add panels inside other panels).

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