gpt4 book ai didi

java - GridBagLayout 不应用网格更改

转载 作者:行者123 更新时间:2023-12-02 03:06:43 29 4
gpt4 key购买 nike

我正在尝试创建一个个人 DnD 角色表程序。主要思想是有 4 个大面板,每个面板包含基本字符表的主要部分之一。我目前正在制作第一个包含统计数据和保存掷骰的面板。我在制作这个时试图更熟悉 GridBagLayout,但我遇到了设置 gridy 的问题。我已经访问过 GridBagLayout 不遵守 gridx 和 gridy 并且(除非我只是愚蠢),这对我没有帮助。我将 GridBagLayout 与 GridBagConstraints 用于 statsPanel() 并且 gridy 工作得很好。

问题是:每当我在 ProficinciesAndSkillsPanel() 中将 gridy 设置为下一个网格时,它就会被视为我刚刚更改了 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;
}

最佳答案

不确定这是否是您的问题,因为您尚未发布有效的 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。在您的代码中,您实际上确实调用了 result.setLayout(new GridBagLayout()),但不是针对我上面显示的 JPanel,这可能会让您感到困惑。我建议您避免在代码中使用相同的变量名称,以避免这种混淆。

如果您需要更具体的帮助,那么首先请告诉我们更多详细信息,并向我们显示您的相关代码作为有效的 minimal example program or MCVE 。如果您站在我们的角度,并试图理解其他人令人困惑的代码,那么如果他们付出努力使该代码对我们来说可编译和可运行,就会产生巨大差异。

关于java - GridBagLayout 不应用网格更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41622646/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com