如图,我想使用GridBagLayout实现图上的布局,求高人指点。
解决方案 »
- 急,java小问题。。。
- 二分插入算法,请帮我看看我的代码错在那里?我已经走进死胡同了,脑子糊涂了.好几天想不通!
- Thinking in java中代码不能编译
- 怎样往MyEclipse环境下的web项目加载jar包啊?
- 我用字符流读二进制文件....我知道应该用字节流.但是我觉得字符读小文件应该也可以可是失败了为什么
- 重金悬赏100分求程序代码(文本文件中的单词统计)
- jfreechart的旋转问题
- GUI当中的事件监听问题,大神帮忙看看呀!!!
- 学习Java看那些书比较好呢?
- 求助:oracle中有一万条纪录.....MS_EXCEL
- 菜鸟求解!
- 【求助】求高手将程序改为有输出计算过程的程序,并优化程序,谢谢!
* This class simplifies the use of the GridBagConstraints class.
* @version 1.01 2004-05-06
* @author Cay Horstmann
*/
public class GBC extends GridBagConstraints
{
/**
* Constructs a GBC with a given gridx and gridy position and all other grid
* bag 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 grid bag 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 weights.
* @param weightx the cell weight in x-direction
* @param weighty the cell weight in 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 in 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;
}
}
TestFrame.javaimport java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class TestFrame extends JFrame
{
private JTree tree = new JTree();
private JTextArea taText = new JTextArea();
private JTextField tfText = new JTextField();
private JButton btnAdd = new JButton("添加");
private JButton btnDel = new JButton("删除");
private JButton btnChange = new JButton("修改");
private JButton btnClearAll = new JButton("清除全部");
private JLabel lbl = new JLabel("这里有个JLabel");
public TestFrame()
{
this.setLayout(new GridBagLayout());
add(new JScrollPane(tree), new GBC(0, 0, 1, 2).setFill(GBC.BOTH).setWeight(1, 1));
add(taText, new GBC(1, 0).setFill(GBC.BOTH).setWeight(1, 1));
JPanel panel = new JPanel(new GridBagLayout());
panel.add(tfText, new GBC(0, 0, 4, 1).setFill(GBC.HORIZONTAL));
panel.add(btnAdd, new GBC(0, 1).setAnchor(GBC.CENTER));
panel.add(btnDel, new GBC(1, 1).setAnchor(GBC.CENTER));
panel.add(btnChange, new GBC(2, 1).setAnchor(GBC.CENTER));
panel.add(btnClearAll, new GBC(3, 1).setAnchor(GBC.CENTER));
panel.add(lbl, new GBC(0, 2, 4, 1).setFill(GBC.HORIZONTAL).setWeight(1, 1));
add(panel, new GBC(1, 1));
}
public static void main(String[] args)
{
JFrame frame = new TestFrame();
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
gridx
gridy
gridwidth
gridheight
fill
anchor
weightx
weighty
ipadx
ipady
的作用还是不理解,API上面的解释太抽象,我都看不懂。您能帮我解释一下吗?或者有相关方面的资料推荐一下,谢谢你。
GridBagLayout把一个界面分为m行n列的网格gridx表示组件位于第几列,gridy表示组件位于第几行
gridwidth表示组件占几列,gridheight表示组件占几行
fill指定当组件小于单元格大小时的填充方式
anchor表示当组件小于单元格大小时的对齐方式
weighx表示当界面有收缩放大时,组件的宽度收缩放大的程度,weighty表示组件高度收缩放大的程度
ipadx表示组件最小的宽度,ipady表示组件最小的高度如果还是有疑问,可以从网上下《Java核心技术》卷一的电子书,看看里面关于GridBagLayout的详细介绍,我用的GBC也是从它那里拷贝过来的