日期:2014-05-20  浏览次数:20622 次

GridBagLayout不能正常显示 问题在哪 没有异常输出
import java.awt.*;

import javax.swing.*;
import javax.swing.border.LineBorder;


public class CategoryPanel extends JPanel {

/**build the categorypanel for the series warehouse handling
 * 
 * @param args
 */
public CategoryPanel(){
String [] s = {"20000","40000","60000","60000","00008","400000","900000","00002"};
JList brandlist = new JList(s);
brandlist.setPreferredSize(new Dimension(120,30));
JList serieslist = new JList(s);
JLabel namelabel  = new JLabel("name");
JTextField nametf = new JTextField(20);
JLabel amountlabel = new JLabel("amount");
JTextField amounttf= new JTextField(20);
JLabel pricelabel = new JLabel("price");
JTextField pricetf = new JTextField(20);
JLabel discountlabel = new JLabel("discount");
JTextField discounttf = new JTextField(20);

JPanel inputpanel = new JPanel();
GridBagLayout gbl = new GridBagLayout();
inputpanel.setLayout(gbl);
JPanel enterpanel = new JPanel();
JButton ok = new JButton("ok");
ok.setPreferredSize(new Dimension(120,30));
//enterpanel.setLayout(new GridLayout(1,4));
enterpanel.add(ok);


inputpanel.add(new JScrollPane(brandlist),new GBC(0,0).setFill(GBC.VERTICAL).setWeight(0, 0).setAnchor(GBC.CENTER));
inputpanel.add(new JScrollPane(serieslist),new GBC(1,0).setFill(GBC.HORIZONTAL).setWeight(0, 0));
inputpanel.add(namelabel,new GBC(2,0).setFill(GBC.HORIZONTAL).setWeight(0, 0));
inputpanel.add(nametf,new GBC(3,0).setFill(GBC.HORIZONTAL).setWeight(0, 0));
inputpanel.add(amountlabel,new GBC(4,0).setInsets(20).setFill(GBC.BOTH).setWeight(0, 0));
inputpanel.add(amounttf,new GBC(5,0).setInsets(20).setFill(GBC.BOTH).setWeight(0, 0));
inputpanel.add(pricelabel,new GBC(6,0).setInsets(20).setFill(GBC.BOTH).setWeight(0, 0));
inputpanel.add(pricetf,new GBC(7,0).setInsets(20).setFill(GBC.BOTH).setWeight(0, 0));
inputpanel.add(discountlabel,new GBC(8,0).setInsets(20).setFill(GBC.BOTH).setWeight(0, 0));
inputpanel.add(discounttf,new GBC(9,0).setInsets(20).setFill(GBC.BOTH).setWeight(0, 0));

LineBorder lb = new LineBorder(Color.cyan,2);
inputpanel.setBorder(lb);

setLayout(new BorderLayout());
add(inputpanel, BorderLayout.CENTER);
add(enterpanel, BorderLayout.SOUTH);

}
public static void main(String[] args) {
// TODO Auto-generated method stub
          JFrame f = new JFrame();
          f.add(new CategoryPanel());
          f.setSize(900,320);
          f.setVisible(true);
          f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          
}

}


import java.awt.*;
import javax.swing.*;


public class GBC extends GridBagConstraints{

/**Constructs a GBC with a given gridx and gridy position and all other 
 * gridbag constraint values set to the default.
 * @param gridx the gridx position
 * @param gridy the gridy position
 */
public GBC(int gridx,int gridy){
this.gridx = gridx;
this.gridy = gridy;

}

/**Constructs a GBC with given gridx,gridy,gridwidth,gridheight and all other gridbag
 * constraint values set to the default.
 * 
 * @param gridx the gridx position
 * @param gridy the gridy position 
 * @param gridwidth the cell span in x-direction
 * @param gridheight the cell span in y-direction
 */
public GBC(int gridx,int gridy,int gridwidth, int gridheight){
this.gridx = gridx;
this.gridy = gridy;
this.gridwidth = gridwidth;
this.gridheight = gridheight;

}

/**Sets the anchor
 * 
 * @param anchor the anchor value
 * @return this object for further modification
 */
public GBC setAnchor(int anchor){
this.anchor = anchor;
return this;
}

/**Sets the fill direction
 * @param fill the fill direction
 * @return this object for further modification
 * 
 */

public GBC  setFill(int fill){