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

香至 2014-04-05 10:16:41
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){
this.fill = fill;
return this;

}

/**Sets the cell weight
* @param weightx the cell weight in x-direction
* @param weighty the cell weight int y-direction
* @return this object for further modification
*/

public GBC setWeight(double weightx , double weighty){
this.weightx = weightx;
this.weighty = weighty;
return this;
}

/**Sets the insets of this cell.
* @param distance the spacing to use all directions
* @return this object for further modification
*/

public GBC setInsets(int distance){
this.insets= new Insets(distance,distance,distance,distance);
return this;

}

/**Sets the insets of this cell.
* @param top the spacing to use on top
* @param left the spacing to use to the left
* @param bottom the spacing to use on the bottom
* @param right the spacing to use to the right
* @return this object for further modification
*
*/
public GBC setInsets(int top, int left, int bottom, int right){
this.insets = new Insets(top,left, bottom,right);
return this;
}

/**Sets the internal padding
* @param ipadx the internal padding in x-direction
* @param ipady the internal padding in y-direction
* @return this object for further modification
*/

public GBC setIpad(int ipadx, int ipady){
this.ipadx = ipadx;
this.ipady = ipady;
return this;
}



}
...全文
192 4 打赏 收藏 举报
写回复
4 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
香至 2014-04-07
如下是截图
  • 打赏
  • 举报
回复
香至 2014-04-07
把代码改了一下,还是那个样, 看样子问题不在这里 谢谢
  • 打赏
  • 举报
回复
香至 2014-04-07
import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.GridLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextField; import javax.swing.border.LineBorder; public class CategoryComponent extends JPanel { /** * @param args */ public CategoryComponent(){ JPanel inputpanel = new JPanel(); GridBagLayout gbl = new GridBagLayout(); inputpanel.setLayout(gbl); String [] s = {"20000","40000","60000","60000","00008","400000","900000","00002"}; JList brandlist = new JList(s); brandlist.setPreferredSize(new Dimension(120,30)); GridBagConstraints gbc1 = new GridBagConstraints(); gbc1.gridx= 0; gbc1.gridy = 0; inputpanel.add(new JScrollPane(brandlist),gbc1); JList serieslist = new JList(s); GridBagConstraints gbc2 = new GridBagConstraints(); gbc2.gridx=1; gbc2.gridy=0; inputpanel.add(new JScrollPane(serieslist),gbc2); JLabel namelabel = new JLabel("name"); GridBagConstraints gbc3 = new GridBagConstraints(); gbc3.gridx = 2; gbc3.gridy = 0; inputpanel.add(namelabel, gbc3); JTextField nametf = new JTextField(20); GridBagConstraints gbc4 = new GridBagConstraints(); gbc4.gridx= 3; gbc4.gridy=0; inputpanel.add(nametf,gbc4); JLabel amountlabel = new JLabel("amount"); GridBagConstraints gbc5 = new GridBagConstraints(); gbc5.gridx= 4; gbc5.gridy=0; inputpanel.add(amountlabel,gbc5); JTextField amounttf= new JTextField(20); GridBagConstraints gbc6 = new GridBagConstraints(); gbc6.gridx= 5; gbc6.gridy= 0; inputpanel.add(amounttf,gbc6); JLabel pricelabel = new JLabel("price"); GridBagConstraints gbc7 = new GridBagConstraints(); gbc7.gridx= 6; gbc7.gridy=0; inputpanel.add(pricelabel,gbc7); JTextField pricetf = new JTextField(20); GridBagConstraints gbc8 = new GridBagConstraints(); gbc8.gridx=7; gbc8.gridy = 0; inputpanel.add(pricetf,gbc8); JLabel discountlabel = new JLabel("discount"); GridBagConstraints gbc9 = new GridBagConstraints(); gbc9.gridx= 8; gbc9.gridy= 0; inputpanel.add(discountlabel,gbc9); JTextField discounttf = new JTextField(20); GridBagConstraints gbc10 = new GridBagConstraints(); gbc10.gridx= 9; gbc10.gridy=0; inputpanel.add(discounttf,gbc10); 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)); //inputpanel.add(new JScrollPane(serieslist)); //inputpanel.add(namelabel); //inputpanel.add(nametf); //inputpanel.add(amountlabel); //inputpanel.add(amounttf); //inputpanel.add(pricelabel); //inputpanel.add(pricetf); //inputpanel.add(discountlabel); //inputpanel.add(discounttf); 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 CategoryComponent()); f.setSize(900,120); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
  • 打赏
  • 举报
回复
瓜瓜东西 2014-04-06
是不是 每天在面板里添加 组件,可以参考一个例子 http://blog.csdn.net/cgwcgw_/article/details/19114245
  • 打赏
  • 举报
回复
相关推荐
发帖
Java SE
加入

6.2w+

社区成员

Java 2 Standard Edition
社区管理员
  • Java SE
申请成为版主
帖子事件
创建了帖子
2014-04-05 10:16
社区公告
暂无公告