简体   繁体   English

Java有组织的网格布局

[英]Java Organized Grid Layout

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: 如果用户输入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. 由于某种原因,我无法使Java中的网格布局每行只有2列。

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);
}

What this GUI really needs is an entirely different layout. 该GUI真正需要的是完全不同的布局。 Here is how your planned GUI would look. 这是您计划的GUI的外观。

在此处输入图片说明

Here is how it should probably be laid out. 这可能是应该如何布置的。

在此处输入图片说明

ExcellentGUI.java 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();
            }
        });
    }
}

You understand that this new GridLayout(2, testCount) means two rows and testCount columns, which isn't what I think you want. 您知道,这个new GridLayout(2, testCount)意味着两行和testCount列,这不是我想要的。 I strongly urge you to read the GridLayout API to see exactly how this and the other constructors work. 我强烈建议您阅读GridLayout API,以准确了解它和其他构造函数的工作方式。

You can simplify things greatly by making your layout new GridLayout(0, 2); 通过使布局成为new GridLayout(0, 2);可以大大简化事情new GridLayout(0, 2); which means any number of rows and always 2 columns. 这意味着任意数量的行,总是2列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM