2012-05-28 110 views
1

基本上我试图将两个面板连接到我的框架上, 我的框架使用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 但我不是有远大抱负,这是学习的目的和将不会有很多功能。

回答

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

+1,对于1 JPanel中的许多JComponents,MigLayout总是不错的选择。 – Hidde

+0

感谢Emmanual,我会看看miglayout ,但这并不能解决我的好奇心,我认为在这种特殊情况下,网格对于3行和4列是完全正确的,但它们的排列顺序很奇怪,这让我非常好奇。 我仍在寻找基于gridlayout的解决方案,但我仍然会检查mig,再次感谢您。 –

+0

GridLayout真的看起来很可怕,文本框将有一个非标准的高度,文本基线将不会对齐。如果你真的想看看它的外观,你必须复制'ntg'空白元素,因为一个对象只能添加一次到一个容器。 –