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){
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;
}
}
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;
}
}
...全文
相关推荐
Java中GridBagLayout的用法
- 其他属性如`anchor`和`fill`被设置为默认值,表示按钮居中显示且不会填充整个单元格。
Java Swing GridBagLayout网格袋布局的实现
每个由GridBagLayout管理的组件都关联一个该约束实例,以指定组件所在显示区域的具体放置位置,以及组件在其显示区域中的对齐方式。
GridBagLayout布局详解
)* fill:组件在其显示区域的单元格中的填充方式* insets:组件四周边框与其显示区域的单元格边缘之间的间距* ipadx:组件的内部横向填充* ipady:组件的内部纵向填充 GridBagLayout
Swing的GridBagLayout布局应用详解(附Java源代码)
本文通过一个Java Swing应用程序示例,详细介绍了GridBagLayout布局管理器的使用方法。示例程序将界面分为标题、内容和按钮三个部分,并添加了多种组件。同时,介绍了自定义按钮类和图标式按
Java 最重要布局管理器GridBagLayout的使用方法
**GridBagLayout的原理** `GridBagLayout`维护了一个动态的矩形网格,每个组件对应一个显示区域,这些区域可以跨越多个网格单元。


Java SE
