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

Java 使用FlowLayout管理器将3个面板保持在不同的行中,java,button,window,jpanel,grid-layout,Java,Button,Window,Jpanel,Grid Layout,我使用GridLayout管理器来创建gui,但我不喜欢额外的空间使我的按钮看起来很小。 我希望它看起来更像这个。 我曾尝试将前两行放在另一个gridlayout中,并将其全部放在一个flow布局中,但没有成功。它们只是并排结束,除非窗口变得非常小,因为它是一个flowlayout。有什么想法吗 标准服务面板构造器 public StandardServices() { // Create GridLayout manager with 5 rows and 1 column

我使用GridLayout管理器来创建gui,但我不喜欢额外的空间使我的按钮看起来很小。

我希望它看起来更像这个。

我曾尝试将前两行放在另一个gridlayout中,并将其全部放在一个flow布局中,但没有成功。它们只是并排结束,除非窗口变得非常小,因为它是一个flowlayout。有什么想法吗

标准服务面板构造器

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 ($25.00)");
    ram = new JCheckBox("Install Ram ($15.00)");
    virus = new JCheckBox("Remove Virus ($50.00)");
    fHardDrive = new JCheckBox("Format Hard Drive ($80.00)");
    labourQuote = new JCheckBox("Hourly Labour Qoute ($10.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);

}
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);
}
小时服务小组建造师

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 ($25.00)");
    ram = new JCheckBox("Install Ram ($15.00)");
    virus = new JCheckBox("Remove Virus ($50.00)");
    fHardDrive = new JCheckBox("Format Hard Drive ($80.00)");
    labourQuote = new JCheckBox("Hourly Labour Qoute ($10.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);

}
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);
}
LU存储GUI构造函数

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 ($25.00)");
    ram = new JCheckBox("Install Ram ($15.00)");
    virus = new JCheckBox("Remove Virus ($50.00)");
    fHardDrive = new JCheckBox("Format Hard Drive ($80.00)");
    labourQuote = new JCheckBox("Hourly Labour Qoute ($10.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);

}
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()删除不必要的空格。。
将其作为最后一行。

使用
网格布局
将根据最大组件为所有组件提供相等的间距,而不是使用
网格布局
,它不仅为您提供更多的控制,而且将遵循单个组件的首选尺寸


使用
GridBagLayout
而不是您是否了解
GridBagLayout
的工作原理?因为,这不会解决所描述的问题,为什么不导入javax.swing.*和java.awt.*而不是特定的导入?这是为了澄清还是有其他原因?谢谢。@grant2088有一些编译器的优点(或者过去有),但大多数情况下,我的IDE是自动完成的