GridBagLayout不应用网格更改

时间:2017-01-12 20:45:45

标签: java swing user-interface layout-manager gridbaglayout

我正在尝试创建个人DnD角色表程序。主要思想是有4个大面板,每个面板包含一个基本字符表的主要部分之一。我目前正在制作第一个有统计数据和豁免检定的面板。我在尝试使用GridBagLayout时更加舒服,但是我遇到了设置gridy的问题。我已经访问过GridBagLayout而不遵守gridx和gridy(除非我只是愚蠢),这对我没有帮助。我使用GridBagLayout和GridBagConstraints进行statsPanel()和gridy工作正常。

问题在于:每当我将gridy设置为proficinciesAndSkillsPanel()中的下一个网格时,它就像我刚刚更改了gridx一样。我的目标是让一列有很多行,而不是一行有很多列。谢谢你的时间

//this builds the jframe and sets the primary jpanel
private void buildComponents()
{
    setSize(800, 600);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel mp = (JPanel)getContentPane();
    mp.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.fill = GridBagConstraints.BOTH;

    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.weightx = 1.0;
    gbc.weighty = 1.0;
    gbc.gridheight = 1;
    gbc.gridwidth = 1;
    gbc.gridx = 0;
    gbc.gridy = 0;

    mp.add(panelA(), gbc);



    gbc.gridx = 1;

    mp.add(new JButton("test"), gbc);

    createMenuBar();
    setVisible(true);
}

//this creates the first real panel that i'm currently working with
private JPanel panelA()
{
    JPanel result = new JPanel();
    result.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();

    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.fill = GridBagConstraints.BOTH;

    gbc.gridx = 0;
    gbc.gridy = 0;

//I left out the code for statsPanel() because that works fine
    result.add(statsPanel(), gbc);

    gbc.gridx = 1;

    result.add(proficinciesAndSkillsPanel(), gbc);

    return result;
}

//this builds the second half of the upper portion of panel A
private JPanel proficinciesAndSkillsPanel()
{
    JPanel result = new JPanel();
    GridBagConstraints gbc = new GridBagConstraints();

    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.fill = GridBagConstraints.BOTH;
    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.weightx = 1;
    gbc.weighty = 1;

    result.add(labeledTextField("Inspiration", inspirationField = new JTextField(2), null, 1), gbc);

    gbc.gridx = 0;
    gbc.gridy = 1;

    result.add(labeledTextField("Proficiency bonus", proficiencyField = new JTextField(2), null, 1),gbc);

    gbc.gridx = 0;
    gbc.gridy = 2;

    result.add(labeledRadioField("Strength", strSTField = new JTextField(2), strRB = new JRadioButton()),gbc);

    return result;
}

//this creates a JTextField with the appropriate label and a sub-JTextField
private JPanel labeledTextField(String str, JTextField jtf, JTextField bonjtf, int space)
{
    JPanel result = new JPanel();
    JPanel subResult = new JPanel();
    result.setLayout(new FlowLayout());
    result.add(new JLabel(str));
    result.add(Box.createHorizontalStrut(space));
    subResult.add(jtf);
    jtf.setHorizontalAlignment(JTextField.CENTER);
    try
    {
        subResult.add(bonjtf);
        bonjtf.setHorizontalAlignment(JTextField.CENTER);
        bonjtf.setEditable(false);
        bonjtf.setText("+0");
    }catch(NullPointerException e){}
    jtf.addKeyListener(new JTF_Listener(){
        public void update() {
            String str2 = "";
            try
            {
                int result = (Integer.parseInt(jtf.getText())-10)/2;
                if(result >=0)
                {
                    str2 += "+"+Integer.toString(result);
                }
                else
                {
                    str2 += Integer.toString(result);
                }
            }catch(NumberFormatException nfe){}
            bonjtf.setText(str2);
        }
    });
    result.add(subResult);
    return result;
}

//this does the same as labeledTextField, just with a radioButton
private JPanel labeledRadioField(String str, JTextField jtf, JRadioButton jrb)
{
    JPanel result = new JPanel();
    result.setLayout(new FlowLayout());
    result.add(jrb);
    result.add(jtf);
    result.add(new JLabel(str));
    jtf.setHorizontalAlignment(JTextField.CENTER);
    jtf.setText("+0");
    jtf.addKeyListener(new JTF_Listener(){
        public void update(){
            String str2 = "";
            try
            {
                int result = Integer.parseInt(jtf.getText());
                str2+= "+" + Integer.toString(result);
            }catch(NumberFormatException nfe){}
            jtf.setText(str2);
        }
    });

    return result;
}

1 个答案:

答案 0 :(得分:2)

不确定这是你的问题,因为你没有发布有效的MCVE(请更正!),但是在这里:

private JPanel proficinciesAndSkillsPanel()
{
    JPanel result = new JPanel();  // ******** here *********
    GridBagConstraints gbc = new GridBagConstraints();

    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.fill = GridBagConstraints.BOTH;
    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.weightx = 1;
    gbc.weighty = 1;

    result.add(labeledTextField("Inspiration", inspirationField = new JTextField(2), null, 1), gbc);

    gbc.gridx = 0;
    gbc.gridy = 1;

    result.add(labeledTextField("Proficiency bonus", proficiencyField = new JTextField(2), null, 1),gbc);

    gbc.gridx = 0;
    gbc.gridy = 2;

    result.add(labeledRadioField("Strength", strSTField = new JTextField(2), strRB = new JRadioButton()),gbc);

    return result;
}

您正在将此结果视为JPanel,就好像它使用的是GridBagLayout,实际上并非如此,它使用的是JPanel的默认FlowLayout

一个令人困惑的地方:你有很多JPanel变量,它们被赋予相同的名称,结果。在您的代码中,您实际上调用result.setLayout(new GridBagLayout()),但不是我上面显示的JPanel,这可能让您感到困惑。我建议您避免在代码中使用相同的变量名来避免这种混淆。

如果您需要更具体的帮助,那么请先告诉我们更多详细信息,并将相关代码显示为有效的minimal example program or MCVE。如果你坐在我们的位置,并试图了解其他人的令人困惑的代码,那么如果他们付出努力使这些代码可编辑并且可以为我们运行,则会产生巨大的差异。