gpt4 book ai didi

java - 网格布局列和行不起作用

转载 作者:行者123 更新时间:2023-11-29 07:14:33 26 4
gpt4 key购买 nike

基本上我试图将两个面板连接到我的框架上,我的框架使用 BorderLayout 并且两个面板将放置在南北方向。我完成了顶部面板,但对于底部面板,我无法正确处理。我在底部使用了 GridLayout,它应该看起来像这样。

http://i47.tinypic.com/wa0lsz.png

这是网格布局的代码

public class WeaponComp extends JPanel{


protected JPanel first = new JPanel();

public WeaponComp(){

setLayout(new GridBagLayout());
setPreferredSize(new Dimension(800,550));
GridBagConstraints c = new GridBagConstraints();
JLabel ntg = new JLabel("");
JLabel oldweap = new JLabel("OLD WEAPON");
JLabel newweap = new JLabel("NEW WEAPON");

JLabel onetwohand = new JLabel ("1H / 2H");
JLabel offhand = new JLabel ("Off Hand");
JLabel dps = new JLabel ("DPS :");
JLabel str = new JLabel ("Str :");
JLabel dex = new JLabel ("Dex :");
JLabel vit = new JLabel ("Vit :");
JLabel intel = new JLabel ("Int :");
JLabel manareg = new JLabel ("Mana Regen :");
JLabel aspd = new JLabel ("Attack Speed:");
JLabel critch = new JLabel ("Crit chance:");
JLabel critdmg = new JLabel ("Crit damage:");

JTextField dpstf = new JTextField(12);
JTextField strtf = new JTextField(5);
JTextField dextf = new JTextField(5);
JTextField vittf = new JTextField(5);
JTextField inteltf = new JTextField(5);
JTextField manaregtf = new JTextField(3);
JTextField aspdtf = new JTextField(3);
JTextField critchtf = new JTextField(3);
JTextField critdmgtf = new JTextField(3);
JTextField offdpstf = new JTextField(12);
JTextField offstrtf = new JTextField(5);
JTextField offdextf = new JTextField(5);
JTextField offvittf = new JTextField(5);
JTextField offinteltf = new JTextField(5);
JTextField offmanaregtf = new JTextField(3);
JTextField offaspdtf = new JTextField(3);
JTextField offcritchtf = new JTextField(3);
JTextField offcritdmgtf = new JTextField(3);

first.setLayout(new GridLayout(3,4));
first.setPreferredSize(new Dimension(750,150));

first.add(oldweap); first.add(ntg); first.add(newweap); first.add(ntg);
first.add(onetwohand); first.add(ntg); first.add(offhand); first.add(ntg);
first.add(dps); first.add(dpstf); first.add(dps); first.add(offdpstf);


c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0; c.gridy = 0;
add (first,c);

}
}

这是我程序的当前结果

http://i50.tinypic.com/107p9uu.png

预先感谢您的时间和答复

PS:如果你想知道,是的,它与暗黑破坏神 3 有关但我没有那么雄心勃勃,这是为了学习目的,不会有很多功能。

最佳答案

GridLayout 是一个糟糕的选择,因为所有单元格自动具有相同的大小。我建议改用 MigLayout:http://miglayout.com

布局代码如下所示:

first.setLayout(new MigLayout("wrap 2, fill"));
first.add(oldweap);
first.add(newweap);
first.add(onetwohand);
first.add(offhand);
first.add(dps);
first.add(dpstf);
first.add(dps);
first.add(offdpstf);

关于java - 网格布局列和行不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10786703/

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