Java 嵌套布局面板

Java 嵌套布局面板,java,swing,user-interface,jpanel,layout-manager,Java,Swing,User Interface,Jpanel,Layout Manager,我正在尝试使用JAVA为BMR计算器创建GUI 我在正确使用GUI方面遇到了一些问题,因此我一直在尝试使用不同的布局管理器/嵌套JPanel 我当前的代码有一个年龄和性别标签,它们都包含在流布局中单独的JPanel中,但问题是它们彼此相邻,而不是像我希望的那样出现在单独的行上 如何用我的代码实现这一点?我现在的laoyut如下,我希望性别在年龄以下,并且已经玩了一段时间,但无法让它工作 干杯 package v2; import javax.swing.*; import java.awt.

我正在尝试使用JAVA为BMR计算器创建GUI

我在正确使用GUI方面遇到了一些问题,因此我一直在尝试使用不同的布局管理器/嵌套JPanel

我当前的代码有一个年龄和性别标签,它们都包含在流布局中单独的JPanel中,但问题是它们彼此相邻,而不是像我希望的那样出现在单独的行上

如何用我的代码实现这一点?我现在的laoyut如下,我希望性别在年龄以下,并且已经玩了一段时间,但无法让它工作

干杯

package v2;

import javax.swing.*;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.FlowLayout;




public class BmrCalcv2 extends JFrame {

static JFrame mainFrame;
static JPanel mainPanel;

static JMenuBar menuBar;
static JMenu fileMenu, editMenu;

static JPanel agePanel;
private JLabel ageLabel;
private JTextField ageTextField;

static JPanel genderPanel;
private JLabel genderLabel;




public BmrCalcv2() {

    // Main JFrame
    mainFrame = new JFrame("BMR/TDEE Calculator");
    mainPanel = new JPanel();


    // All JPanel declarations
    menuBar = new JMenuBar();
    agePanel = new JPanel();
    genderPanel = new JPanel();

    // JPanel layout managers
    agePanel.setLayout(new FlowLayout(10));
    genderPanel.setLayout(new FlowLayout(10));


    // Menu JPanel
    fileMenu = new JMenu("File");
    editMenu = new JMenu("Edit");
    menuBar.add(fileMenu);
    menuBar.add(editMenu);

    // Age JPanel
    ageLabel = new JLabel("Age:");
    ageTextField = new JTextField(5);
    agePanel.add(ageLabel);
    agePanel.add(ageTextField);



    // Gender JPanel
    genderLabel = new JLabel("Gender:");
    genderPanel.add(genderLabel);



    // Adding sub JPanels to main JPanel
    mainPanel.add(agePanel);
    mainPanel.add(genderPanel);


    // Adding main JPanel/menubar to JFrame
    mainFrame.setJMenuBar(menuBar);
    mainFrame.add(mainPanel);

}





public static void main(String[] args) {

    BmrCalcv2 frame = new BmrCalcv2();

    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mainFrame.setVisible(true);
    mainFrame.setSize(330, 300);;
    mainFrame.setResizable(false);

    }

}


像这样的东西应该可以做到:

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

public class GridBagLayoutExample {

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

    private static void init() {
        // create the jframe
        JFrame jframe = new JFrame("BMI Calculator");
        // create the gui elements
        JLabel ageLabel = new JLabel("Age:");
        JTextField ageTxt = new JTextField(20);
        JLabel genderLabel = new JLabel("Gender:");
        JTextField genderTxt = new JTextField(20);
        // create gridbag layout and constraints
        GridBagLayout gbl = new GridBagLayout();
        // create the panel using the gbl
        JPanel pan = new JPanel(gbl);
        // create the constraints
        GridBagConstraints cons = new GridBagConstraints();
        cons.fill = GridBagConstraints.HORIZONTAL;
        // age label
        cons.gridx = 0;
        cons.gridy = 0;
        pan.add(ageLabel, cons);
        // age text
        cons.gridx = 1;
        cons.gridy = 0;
        pan.add(ageTxt, cons);
        // gender label
        cons.gridx = 0;
        cons.gridy = 1;
        pan.add(genderLabel, cons);
        // gender text
        cons.gridx = 1;
        cons.gridy = 1;
        pan.add(genderTxt, cons);
        // add the panel to the jframe
        jframe.add(pan, BorderLayout.CENTER);
        // show the jframe
        jframe.setSize(400, 200);
        jframe.setVisible(true);
    }
}
看起来像这样:


如果您不关心对齐:

mainFrame.getContentPane().setLayout(new BoxLayout(mainFrame.getContentPane(), BoxLayout.X_AXIS));
// quick pseudocode incoming
for(int x = 0; x < components.size(); x++) {
    JPanel j = new JPanel();
    j.setLayout(new FlowLayout(FlowLayout.CENTER)); // I think CENTER is the default anyhow
    j.add(getLabel(x));
    j.add(getField(x));
    mainFrame.add(j);
}
mainFrame.getContentPane().setLayout(新的BoxLayout(mainFrame.getContentPane(),BoxLayout.X_轴));
//快速伪码传入
对于(int x=0;x

如果您关心对齐,请沿
Y\u轴将
FlowLayout
交换为
BoxLayout
,并在标签和输入字段之间放置一些水平胶水。

对于标签/字段对,我建议使用
GridBagLayout
或。您建议我使用一个JPanel还是为每个字段/标签组使用一个JPanel?@Ftahir192在这种情况下,大多数情况下probably@MadProgrammer很抱歉,很可能是一个JPanel,或者每个字段/标签组一个JPanel?@Ftahir192是的,可能一个容器就足够了