Java 是否可以更改GridLayout中每个网格的高度?

Java 是否可以更改GridLayout中每个网格的高度?,java,swing,layout-manager,grid-layout,flowlayout,Java,Swing,Layout Manager,Grid Layout,Flowlayout,我正在尝试创建一个简单的菜单界面,其中包含4行不同的按钮和标签,使用GridLayout在每个网格内使用FlowLayout来组织元素。然而,按钮和标签的空间只占一行,占用了大量空间 这就是我的界面的外观: 这就是它看起来最大化的样子: 我希望设置标签面板/网格的最大大小,以便只占用少量空间 我试图使所有元素都可见,而不隐藏任何内容,窗口大小尽可能小,如下所示: 这是我的代码: public class Window extends JFrame{ public Window() {

我正在尝试创建一个简单的菜单界面,其中包含4行不同的按钮和标签,使用
GridLayout
在每个网格内使用
FlowLayout
来组织元素。然而,按钮和标签的空间只占一行,占用了大量空间

这就是我的界面的外观:

这就是它看起来最大化的样子:

我希望设置标签面板/网格的最大大小,以便只占用少量空间

我试图使所有元素都可见,而不隐藏任何内容,窗口大小尽可能小,如下所示:

这是我的代码:

public class Window extends JFrame{
public Window() {
    super("TastyThai Menu Ordering");
}

public static void main(String[] args) {
    Window w = new Window();
     w.setSize(500, 500);
     w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

     JLabel title = new JLabel("TastyThai Menu Order", SwingConstants.CENTER);
     title.setFont(title.getFont().deriveFont(32f));

     //generate page title
     Container titlePanel = new JPanel(); // used as a container
     titlePanel.setBackground(Color.WHITE);
     FlowLayout flow = new FlowLayout(); // Create a layout manager
     titlePanel.setLayout(flow);// assign flow layout to panel
     titlePanel.add(title); // add label to panel
     w.getContentPane().add(BorderLayout.NORTH,titlePanel);

     //generate row containers
     Container r1 = new JPanel(new FlowLayout());
     Container r2 = new JPanel(new FlowLayout());
     Container r3 = new JPanel(new FlowLayout());
     Container r4 = new JPanel(new FlowLayout());

     //generate mains radio buttons
     Container mains = new JPanel(new GridLayout(7, 0));
     mains.setBackground(Color.RED);
     JLabel mainsHeader = new JLabel("Mains");
     mains.add(mainsHeader);
     String[] mainsChoices = {"Vegetarian", "Chicken", "Beef", "Pork", "Duck", "Seafood Mix"};
     JRadioButton[] mainsRadioButton = new JRadioButton[6];
     ButtonGroup mainsButtons = new ButtonGroup();
     for(int i = 0; i < mainsChoices.length; i++) {
        mainsRadioButton[i] = new JRadioButton(mainsChoices[i]);
        mains.add(mainsRadioButton[i]);
        mainsButtons.add(mainsRadioButton[i]); 
     }

     //generate noodles radio buttons
     Container noodles = new JPanel(new GridLayout(7, 0));
     noodles.setBackground(Color.GREEN);
     JLabel noodlesHeader = new JLabel("Noodles");
     noodlesHeader.setFont(noodlesHeader.getFont().deriveFont(24f));
     noodles.add(noodlesHeader);
     String[] noodlesChoices = {"Pad Thai", "Pad Siew", "Ba Mee"};
     JRadioButton[] noodlesRadioButton = new JRadioButton[3];
     ButtonGroup noodlesButtons = new ButtonGroup();
     for(int i = 0; i < noodlesChoices.length; i++) {
         noodlesRadioButton[i] = new JRadioButton(noodlesChoices[i]);
         noodles.add(noodlesRadioButton[i]);
         noodlesButtons.add(noodlesRadioButton[i]); 
     }

     //generate sauces radio buttons
     Container sauces = new JPanel(new GridLayout(7, 0));
     sauces.setBackground(Color.BLUE);
     JLabel saucesHeader = new JLabel("Sauce");
     saucesHeader.setFont(saucesHeader.getFont().deriveFont(24f));
     sauces.add(saucesHeader);
     String[] saucesChoices = {"Soy Sauce", "Tamarind Sauce"};
     JRadioButton[] saucesRadioButton = new JRadioButton[2];
     ButtonGroup saucesButtons = new ButtonGroup();
     for(int i = 0; i < saucesChoices.length; i++) {
         saucesRadioButton[i] = new JRadioButton(saucesChoices[i]);
         sauces.add(saucesRadioButton[i]);
         saucesButtons.add(saucesRadioButton[i]); 
     }

     //generate extras check boxes
     Container extras = new JPanel(new GridLayout(7, 0));
     extras.setBackground(Color.YELLOW);
     JLabel extrasHeader = new JLabel("Extra");
     extrasHeader.setFont(extrasHeader.getFont().deriveFont(24f));
     extras.add(extrasHeader);
     String[] extrasChoices = {"Mushroom", "Egg", "Broccoli", "Beansrpout", "Tofu"};
     JCheckBox[] extrasBoxes = new JCheckBox[5];
     for(int i = 0; i < extrasChoices.length; i++) {
         extrasBoxes[i] = new JCheckBox(extrasChoices[i]);
         extras.add(extrasBoxes[i]);
     } 

     JLabel selectionPrice = new JLabel("Selection Price: $ ");
     JLabel selectionPriceVal = new JLabel("_______________");
     JButton addToOrder = new JButton("Add to Order");

     JLabel totalPrice = new JLabel("Total Price: $ ");
     JLabel totalPriceVal = new JLabel("_______________");
     JButton clearOrder = new JButton("Clear Order");

     JRadioButton pickUp = new JRadioButton("Pick Up");
     JRadioButton delivery = new JRadioButton("Delivery");
     ButtonGroup pickupDelivery = new ButtonGroup();
     pickupDelivery.add(pickUp);
     pickupDelivery.add(delivery);
     JButton completeOrder = new JButton("Complete Order");

     Container menuSelection = new JPanel(new GridLayout(4,0));
     menuSelection.add(r1);
     r1.add(mains);
     r1.add(noodles);
     r1.add(sauces);
     r1.add(extras);
     menuSelection.add(r2);
     r2.add(selectionPrice);
     r2.add(selectionPriceVal);
     r2.add(addToOrder);
     menuSelection.add(r3);
     r3.add(totalPrice);
     r3.add(totalPriceVal);
     r3.add(clearOrder);
     menuSelection.add(r4);
     r4.add(pickUp);
     r4.add(delivery);
     r4.add(completeOrder);

     w.getContentPane().add(BorderLayout.CENTER, menuSelection);

     w.setVisible(true);
}   
}
公共类窗口扩展JFrame{
公共窗口(){
超级(“TastyThai菜单订购”);
}
公共静态void main(字符串[]args){
窗口w=新窗口();
w、 设置大小(500500);
w、 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel title=新的JLabel(“TastyThai菜单顺序”,SwingConstants.CENTER);
title.setFont(title.getFont().deriveFont(32f));
//生成页面标题
容器titlePanel=new JPanel();//用作容器
标题板背景(颜色:白色);
FlowLayout flow=新建FlowLayout();//创建布局管理器
titlePanel.setLayout(流);//将流布局分配给面板
titlePanel.add(title);//将标签添加到面板
w、 getContentPane().add(BorderLayout.NORTH,titlePanel);
//生成行容器
容器r1=新的JPanel(新的FlowLayout());
容器r2=新的JPanel(新的FlowLayout());
容器r3=新的JPanel(新的FlowLayout());
容器r4=新的JPanel(新的FlowLayout());
//生成电源单选按钮
集装箱干线=新JPanel(新网格布局(7,0));
电源。背景(颜色。红色);
JLabel mainsHeader=新JLabel(“主电源”);
主管道。添加(主管道);
String[]mainsChoices={“素食”、“鸡肉”、“牛肉”、“猪肉”、“鸭子”、“海鲜配料”};
JRadioButton[]mainsRadioButton=新的JRadioButton[6];
按钮组mainsButtons=新按钮组();
对于(int i=0;i
GridLayout不支持此功能。所有矩形的大小都相同


看看GridBagLayout,它支持动态调整大小和更多功能。

GridBagLayout不支持这种功能。所有矩形的大小都相同


看看GridBagLayout,它支持动态调整大小和更多功能。

它与组件的最大大小无关。GridLayout javadoc sa