Java 组织的网格布局

发布于 2024-12-29 17:34:58 字数 865 浏览 2 评论 0原文

我正在制作一个程序,要求用户提供一个整数,然后它应该根据整数的大小显示一个像这样的新窗口。

如果用户输入 2,这将是布局:

Enter test score:    JTextField over here
Enter test score:    JTextField over here
Reset             Submit

出于某种原因,我无法使 java 中的网格布局每行只有 2 列。

这是我的代码:

public void createAverageGUI()
{
    averageFrame = new JFrame("Get All Test Scores");
    txtAllAverages = new JTextField[testCount];
    averageFrame.setLayout(new GridLayout(2, testCount));
    for (int i = 0; i < testCount; i++)
    {
        averageFrame.add(new JLabel("Enter test score: "));
        JTextField field = new JTextField(10);
        averageFrame.add(field);
        txtAllAverages[i] = field;
    }
    averageFrame.add(resetAverages);
    averageFrame.add(submitAverages);
    averageFrame.setSize(400, 600);
    averageFrame.setVisible(true);
}

I'm making a program that asks for an integer from the user, it then should display a new window like this depending on how big the integer is.

This would be the layout if the user entered 2:

Enter test score:    JTextField over here
Enter test score:    JTextField over here
Reset             Submit

For some reason I can't make the grid layout in java have only 2 columns per row.

Here is my code:

public void createAverageGUI()
{
    averageFrame = new JFrame("Get All Test Scores");
    txtAllAverages = new JTextField[testCount];
    averageFrame.setLayout(new GridLayout(2, testCount));
    for (int i = 0; i < testCount; i++)
    {
        averageFrame.add(new JLabel("Enter test score: "));
        JTextField field = new JTextField(10);
        averageFrame.add(field);
        txtAllAverages[i] = field;
    }
    averageFrame.add(resetAverages);
    averageFrame.add(submitAverages);
    averageFrame.setSize(400, 600);
    averageFrame.setVisible(true);
}

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

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

发布评论

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

评论(2

篱下浅笙歌 2025-01-05 17:34:58

这个 GUI 真正需要的是完全不同的布局。这是您计划的 GUI 的外观。

在此处输入图像描述

下面是它应该如何布局。

在此处输入图像描述

ExcellentGUI.java

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;

class ExcellentGUI {

    private JTextField[] txtAllAverages;
    int testCount = 2;

    public void createAverageGUI()
    {
        txtAllAverages = new JTextField[testCount];

        JPanel inputControls = new JPanel(new BorderLayout(5,5));

        JPanel inputControlsLabels = new JPanel(new GridLayout(0,1,3,3));
        JPanel inputControlsFields = new JPanel(new GridLayout(0,1,3,3));
        inputControls.add(inputControlsLabels, BorderLayout.WEST);
        inputControls.add(inputControlsFields, BorderLayout.CENTER);
        for (int i = 0; i < testCount; i++)
        {
            inputControlsLabels.add(new JLabel("Test score: "));
            JTextField field = new JTextField(10);
            inputControlsFields.add(field);
            txtAllAverages[i] = field;
        }

        JPanel controls = new JPanel(new FlowLayout(FlowLayout.CENTER,5,2));

        controls.add(new JButton("Reset"));
        controls.add(new JButton("Submit"));

        JPanel gui = new JPanel(new BorderLayout(10,10));
        gui.setBorder(new TitledBorder("Averages"));
        gui.add(inputControls, BorderLayout.CENTER);
        gui.add(controls, BorderLayout.SOUTH);

        JFrame averageFrame = new JFrame("Get All Test Scores");
        averageFrame.setContentPane(gui);
        // let the components assume the natural size
        // averageFrame.setSize(400, 600);
        averageFrame.pack();
        averageFrame.setLocationByPlatform(true);
        averageFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        averageFrame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new ExcellentGUI().createAverageGUI();
            }
        });
    }
}

What this GUI really needs is an entirely different layout. Here is how your planned GUI would look.

enter image description here

Here is how it should probably be laid out.

enter image description here

ExcellentGUI.java

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;

class ExcellentGUI {

    private JTextField[] txtAllAverages;
    int testCount = 2;

    public void createAverageGUI()
    {
        txtAllAverages = new JTextField[testCount];

        JPanel inputControls = new JPanel(new BorderLayout(5,5));

        JPanel inputControlsLabels = new JPanel(new GridLayout(0,1,3,3));
        JPanel inputControlsFields = new JPanel(new GridLayout(0,1,3,3));
        inputControls.add(inputControlsLabels, BorderLayout.WEST);
        inputControls.add(inputControlsFields, BorderLayout.CENTER);
        for (int i = 0; i < testCount; i++)
        {
            inputControlsLabels.add(new JLabel("Test score: "));
            JTextField field = new JTextField(10);
            inputControlsFields.add(field);
            txtAllAverages[i] = field;
        }

        JPanel controls = new JPanel(new FlowLayout(FlowLayout.CENTER,5,2));

        controls.add(new JButton("Reset"));
        controls.add(new JButton("Submit"));

        JPanel gui = new JPanel(new BorderLayout(10,10));
        gui.setBorder(new TitledBorder("Averages"));
        gui.add(inputControls, BorderLayout.CENTER);
        gui.add(controls, BorderLayout.SOUTH);

        JFrame averageFrame = new JFrame("Get All Test Scores");
        averageFrame.setContentPane(gui);
        // let the components assume the natural size
        // averageFrame.setSize(400, 600);
        averageFrame.pack();
        averageFrame.setLocationByPlatform(true);
        averageFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        averageFrame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new ExcellentGUI().createAverageGUI();
            }
        });
    }
}
┊风居住的梦幻卍 2025-01-05 17:34:58

您知道这个 new GridLayout(2, testCount) 意味着两行和 testCount 列,这不是我认为您想要的。我强烈建议您阅读 GridLayout API 以了解该构造函数和其他构造函数的工作原理。

您可以通过使布局 new GridLayout(0, 2); 大大简化事情,这意味着可以有任意数量的行,并且始终为 2 列。

You understand that this new GridLayout(2, testCount) means two rows and testCount columns, which isn't what I think you want. I strongly urge you to read the GridLayout API to see exactly how this and the other constructors work.

You can simplify things greatly by making your layout new GridLayout(0, 2); which means any number of rows and always 2 columns.

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