简体   繁体   English

Java-如何在GridLayout中对齐JPanels

[英]Java - How to align JPanels inside a GridLayout

This is currently what my form looks like: 当前,这是我的表格:

在此处输入图片说明

I am trying to create a form with 4 columns of radio buttons but when I try to add the panels, holding the buttons, to the grid layout of another panel they seem to mis-align. 我正在尝试创建具有4列单选按钮的表单,但是当我尝试将按住按钮的面板添加到另一个面板的网格布局时,它们似乎未对齐。 I have not set any height of boundaries on the grid layout So I am not sure what is causing it to shift around. 我尚未在网格布局上设置任何边界高度,所以我不确定是什么导致其移动。

My code: 我的代码:

public class Window extends JFrame{
public void paint(Graphics g) {
    super.paint(g); // ??
    getContentPane().setBackground(Color.WHITE); // background color
}

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

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

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

     Container mains = new JPanel(new GridLayout(7, 0));
     mains.setBackground(Color.RED);
     JLabel mainsHeader = new JLabel("Mains");
     mainsHeader.setFont(mainsHeader.getFont().deriveFont(24f));
     mains.add(mainsHeader);

     Container noodles = new JPanel(new GridLayout(4, 0));
     noodles.setBackground(Color.GREEN);
     JLabel noodlesHeader = new JLabel("Noodles");
     noodlesHeader.setFont(noodlesHeader.getFont().deriveFont(24f));
     noodles.add(noodlesHeader);

     Container sauces = new JPanel(new GridLayout(3, 0));
     sauces.setBackground(Color.BLUE);
     JLabel saucesHeader = new JLabel("Sauce");
     saucesHeader.setFont(saucesHeader.getFont().deriveFont(24f));
     sauces.add(saucesHeader);


     Container extras = new JPanel(new GridLayout(6, 0));
     extras.setBackground(Color.YELLOW);
     JLabel extrasHeader = new JLabel("Extra");
     extrasHeader.setFont(extrasHeader.getFont().deriveFont(24f));
     extras.add(extrasHeader)

     Container menuSelection = new JPanel(new GridLayout(0,4));
     menuSelection.add(mains);
     menuSelection.add(noodles);
     menuSelection.add(sauces);
     menuSelection.add(extras);

     w.getContentPane().add(menuSelection);

     w.setVisible(true);
}   
}

You don't need to set a GridLayout on every sub-panel. 您无需在每个子面板上都设置GridLayout。 You should only set it on mains , possibly with correct number of rows and columns. 您只应在mains上进行设置,可能具有正确的行数和列数。

If you only need labels, you can add them directly to mains, and it should lay them out for you. 如果只需要标签,则可以将它们直接添加到电源中,这样可以为您布置它们。

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

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