2010-10-02 152 views
0

我有一个GridBagLayout,但是一个标签(maxSizeLbl)发疯,另一个(maxDateLbl)不可见,任何人都知道我做错了什么?GridBagLayout变得疯狂

下面是图片:

alt text

这里是代码:

import java.awt.Component; 
import java.awt.Container; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 

import javax.swing.JButton; 
import javax.swing.JCheckBox; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 


public class SearchGUI { 
    public static void main(String args[]) { 
     JFrame frame = new JFrame("Search Files"); 
     JPanel panel = new JPanel(); 
     JTextField rootTF = new JTextField(20); 
     JTextField containsTF = new JTextField(20); 
     JTextField containsNotTF = new JTextField(20); 
     JTextField minSizeTF = new JTextField(10); 
     JTextField maxSizeTF = new JTextField(10); 
     JTextField suffixTF = new JTextField(10); 
     JTextField prefixTF= new JTextField(10); 
     JTextField minDateTF = new JTextField(10); 
     JTextField maxDateTF = new JTextField(10); 
     JCheckBox hiddenCB = new JCheckBox("search hidden"); 
     JCheckBox recursiveCB = new JCheckBox("search subfolders"); 
     JButton searchBtn = new JButton("search"); 
     JLabel rootLbl = new JLabel("search in: ", JLabel.CENTER); 
     JLabel containsLbl = new JLabel("Filename contains: ", JLabel.CENTER); 
     JLabel containsNotLbl = new JLabel(" Filename contains not", JLabel.CENTER); 
     JLabel minSizeLbl = new JLabel("min. Size", JLabel.CENTER); 
     JLabel maxSizeLbl = new JLabel("max. Size", JLabel.CENTER); 
     JLabel suffixLbl = new JLabel("Filetypes", JLabel.CENTER); 
     JLabel prefixLbl = new JLabel("begins with", JLabel.CENTER); 
     JLabel minDateLbl = new JLabel("min. Date", JLabel.CENTER); 
     JLabel maxDateLbl = new JLabel("max Date", JLabel.CENTER); 

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     Container c = frame.getContentPane(); 
     GridBagLayout gbl = new GridBagLayout(); 
     c.setLayout(gbl); 


//  addComponent(c, gbl,); 
     //                x y w h wx wy 
     addComponent(c, gbl,    rootLbl, 0, 0, 2, 1, 1.0, 0.0); 
     addComponent(c, gbl,     rootTF, 0, 1, 2, 1, 1.0, 0.0); 
     addComponent(c, gbl,   containsLbl, 0, 2, 2, 1, 1.0, 0.0); 
     addComponent(c, gbl,   containsTF, 0, 3, 2, 1, 1.0, 0.0); 
     addComponent(c, gbl, containsNotLbl, 0, 4, 2, 1, 1.0, 0.0); 
     addComponent(c, gbl,  containsNotTF, 0, 5, 2, 1, 1.0, 0.0); 
     addComponent(c, gbl,   minSizeLbl, 0, 6, 1, 1, 1.0, 0.0); 
     addComponent(c, gbl,   maxSizeLbl, 1, 6, 1, 1, 1.0, 0.0); 
     addComponent(c, gbl,   minSizeTF, 0, 7, 1, 1, 1.0, 0.0); 
     addComponent(c, gbl,   maxSizeTF, 1, 7, 1, 1, 1.0, 0.0); 
     addComponent(c, gbl,    suffixLbl, 0, 8, 2, 1, 1.0, 0.0); 
     addComponent(c, gbl,     suffixTF, 0, 9, 2, 1, 1.0, 0.0); 
     addComponent(c, gbl,   minDateLbl, 0, 10, 1, 1, 1.0, 0.0); 
     addComponent(c, gbl,   maxSizeLbl, 1, 10, 1, 1, 1.0, 0.0); 
     addComponent(c, gbl,    minDateTF, 0, 11, 1, 1, 1.0, 0.0); 
     addComponent(c, gbl,   maxDateTF, 1, 11, 1, 1, 1.0, 0.0); 
     addComponent(c, gbl,    searchBtn, 0, 12, 2, 1, 1.0, 0.0); 

     frame.setSize(gbl.preferredLayoutSize(c)); 
     frame.setVisible(true); 

    } 
    static void addComponent(Container cont, 
      GridBagLayout gbl, 
      Component c, 
      int x, int y, 
      int width, int height, 
      double weightx, double weighty) 
    { 
       GridBagConstraints gbc = new GridBagConstraints(); 
       gbc.fill = GridBagConstraints.BOTH; 
       gbc.gridx = x; gbc.gridy = y; 
       gbc.gridwidth = width; gbc.gridheight = height; 
       gbc.weightx = weightx; gbc.weighty = weighty; 
       gbl.setConstraints(c, gbc); 
       cont.add(c); 
    } 

} 

在此先感谢。

回答

2

请确保您首先彻底研究了不可或缺的GridBagLayout教程http://madbean.com/anim/totallygridbag/

看起来好像你不使用任何IDE。 Eclipse在maxDateLbl以下显示一条好奇的黄色波浪线 - 它没有被使用。如果你点击maxSizeLbl,你会看到它被使用了两次。

将第二个maxSizeLbl替换为maxDateLbl

然后再看教程。

2

包括Geoffrey关于maxSizeLbl/maxDateLbl的发现,这对我来说很合适,并且假定字段标签占据文本字段的左侧而不是顶部。 (代码只显示相关的部分。)


addComponent(c, gbl,  rootLbl, 0, 0, 1, 1, 1.0, 0.0); 
addComponent(c, gbl,   rootTF, 1, 0, 3, 1, 1.0, 0.0); 

addComponent(c, gbl, containsLbl, 0, 1, 1, 1, 1.0, 0.0); 
addComponent(c, gbl,  containsTF, 1, 1, 3, 1, 1.0, 0.0); 

addComponent(c, gbl, containsNotLbl, 0, 2, 1, 1, 1.0, 0.0); 
addComponent(c, gbl, containsNotTF, 1, 2, 3, 1, 1.0, 0.0); 

addComponent(c, gbl,  minSizeLbl, 0, 3, 1, 1, 1.0, 0.0); 
addComponent(c, gbl,  minSizeTF, 1, 3, 1, 1, 1.0, 0.0); 
addComponent(c, gbl,  maxSizeLbl, 2, 3, 1, 1, 1.0, 0.0); 
addComponent(c, gbl,  maxSizeTF, 3, 3, 1, 1, 1.0, 0.0); 

addComponent(c, gbl,  suffixLbl, 0, 4, 1, 1, 1.0, 0.0); 
addComponent(c, gbl,  suffixTF, 1, 4, 3, 1, 1.0, 0.0); 

addComponent(c, gbl,  minDateLbl, 0, 5, 1, 1, 1.0, 0.0); 
addComponent(c, gbl,  minDateTF, 1, 5, 1, 1, 1.0, 0.0); 
addComponent(c, gbl,  maxDateLbl, 2, 5, 1, 1, 1.0, 0.0); 
addComponent(c, gbl,  maxDateTF, 3, 5, 1, 1, 1.0, 0.0); 

addComponent(c, gbl,  searchBtn, 0, 6, 4, 1, 1.0, 0.0); 

+0

我忘了补充一点,你可能要要使用“frame.pack ();”作为“frame.setSize(gbl.preferredLayoutSize(c))”的替代;“声明,否则某些视口会被裁剪。 – JTP 2010-10-02 19:52:12

3

您添加maxSizeLbl两次(而不是maxDateLbl