使用 FlowLayout 管理器将 3 个面板保持在不同的行中

Using FlowLayout manager to keep 3 panels in distinct rows

我使用 GridLayout mananger 来创建我的图形用户界面,但我不喜欢额外的 space 使我的按钮看起来很小。 My GUI

我想让它看起来更像这个。 Good GUI

我试过将前两行放在另一个网格布局中,然后将它们全部放在流式布局中,但没有成功。它们最终并排在一起,除非 window 变得非常小,因为它是一个流式布局。有什么想法吗?

标准服务面板构造函数

public StandardServices()
{
    // Create GridLayout manager with 5 rows and 1 column
    setLayout(new GridLayout(5,1));

    // Create the check boxes.
    iHardDrive = new JCheckBox("Install Hard Drive (.00)");
    ram = new JCheckBox("Install Ram (.00)");
    virus = new JCheckBox("Remove Virus (.00)");
    fHardDrive = new JCheckBox("Format Hard Drive (.00)");
    labourQuote = new JCheckBox("Hourly Labour Qoute (.00)");

    //Add a border around the panel.
    setBorder(BorderFactory.createTitledBorder("Standard Services"));

    // Add the checkboxes to the panel.
    add(iHardDrive);
    add(ram);
    add(virus);
    add(fHardDrive);
    add(labourQuote);
}

每小时服务面板构造函数

public HourlyService()       
{
    // Created grid layout with 2 rows, 1 column
    setLayout(new GridLayout(2,1));

    // Create labels to display instructions.
    cost = new JLabel("Parts Cost:");
    labour = new JLabel("Hours of Labour:");

    // Create two text fields 10 characters wide.
    costTextField = new JTextField(10);
    labourTextField = new JTextField(10);

    // Place a 0 in the text fields.
    costTextField.setText("0");
    labourTextField.setText("0");

    // Add a border around the layout
    setBorder(BorderFactory.createTitledBorder("Hourly Service"));

    // Add labels and text fields to the panel.
    add(cost);
    add(costTextField);
    add(labour);
    add(labourTextField);

}

LU Store GUI 构造器

public MyStoreGui()
{
    //Display a title
    setTitle("LU Computer Store");

    //Specify a default action for the close button
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Set the size of the window
    setSize(WINDOW_WIDTH, WINDOW_HEIGHT);

    // Create a GridLayout manager
    setLayout(new GridLayout(3,1));
    //setLayout(new FlowLayout());
    // create custom panels
    standard =  new StandardServices();
    hourly = new HourlyService();

    //Create the button panel
    buildButtonPanel();


    add(standard);
    add(hourly);
    add(buttonPanel);

    // Display the window
    setVisible(true);
}

使用 pack() 方法删除不必要的空格.. 将它用作最后一行。

不要使用 GridLayout,它会根据最大的组件为所有组件提供相等的间距,而是使用 GridBagLayout,它不仅会为您提供更多控制,而且会尊重各个组件的首选大小

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new MyStoreGui());
                frame.pack();
                frame.setVisible(true);
            }
        });
    }

    public final class MyStoreGui extends JPanel {

        public MyStoreGui() {
            setLayout(new GridBagLayout());
            //setLayout(new FlowLayout());
            // create custom panels
            StandardServices standard = new StandardServices();
            HourlyService hourly = new HourlyService();

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.fill = GridBagConstraints.HORIZONTAL;

            //Create the button panel
            JPanel buttonPanel = buildButtonPanel();

            add(standard, gbc);
            add(hourly, gbc);
            add(buttonPanel, gbc);

            // Display the window
            setVisible(true);
        }

        protected JPanel buildButtonPanel() {
            JPanel panel = new JPanel();
            panel.add(new JButton("Calclate Changes"));
            panel.add(new JButton("Exit"));
            return panel;
        }
    }

    public class StandardServices extends JPanel {

        public StandardServices() {
            // Create GridLayout manager with 5 rows and 1 column
            setLayout(new GridLayout(5, 1));

            // Create the check boxes.
            JCheckBox iHardDrive = new JCheckBox("Install Hard Drive (.00)");
            JCheckBox ram = new JCheckBox("Install Ram (.00)");
            JCheckBox virus = new JCheckBox("Remove Virus (.00)");
            JCheckBox fHardDrive = new JCheckBox("Format Hard Drive (.00)");
            JCheckBox labourQuote = new JCheckBox("Hourly Labour Qoute (.00)");

            //Add a border around the panel.
            setBorder(BorderFactory.createTitledBorder("Standard Services"));

            // Add the checkboxes to the panel.
            add(iHardDrive);
            add(ram);
            add(virus);
            add(fHardDrive);
            add(labourQuote);
        }
    }

    public class HourlyService extends JPanel {

        public HourlyService() {
            // Created grid layout with 2 rows, 1 column
            setLayout(new GridLayout(2, 1));

            // Create labels to display instructions.
            JLabel cost = new JLabel("Parts Cost:");
            JLabel labour = new JLabel("Hours of Labour:");

            // Create two text fields 10 characters wide.
            JTextField costTextField = new JTextField(10);
            JTextField labourTextField = new JTextField(10);

            // Place a 0 in the text fields.
            costTextField.setText("0");
            labourTextField.setText("0");

            // Add a border around the layout
            setBorder(BorderFactory.createTitledBorder("Hourly Service"));

            // Add labels and text fields to the panel.
            add(cost);
            add(costTextField);
            add(labour);
            add(labourTextField);

        }
    }
}