微科社区,轻松开发从此开始! 请登陆 免费注册

微科社区

当前位置:首页 > Java平台 > J2SE >

GridBagLayout不能正常显示 问题在哪 没有异常输

时间:2014-04-10 09:47  浏览:努力统计中...
importjava.awt.*; importjavax.swing.*; importjavax.swing.border.LineBorder; publicclassCategoryPanelextendsJPanel{ /**buildthecategorypanelfortheserieswarehousehandling * *@paramargs */ publicCategoryPanel(){ String[]s={ 20000 , 40000 , 6000
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;
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线------