为什么我用gridbaglayout设置组件的位置,全聚集在panel中央位置,我想从顶端开始布局,我看了帮助是和权重有关,但我不知道怎么设置。请高手们指点一二!
解决方案 »
- HttpClient3.1为什么会打下面的DEBUG级别的log,想说明什么呢
- JSP做了一年多了,我怎么感觉对JAVA还是什么都不会呢...
- 问一个很水的问题,不要笑,请认真回答 呵呵 先谢谢了
- 救命啊!怎么总是出错??!大家帮我看下那里出错误!
- Java读取属性文件的问题
- 高手来看看,这个程序的结果
- 求一java 实验室管理系统源码 带数据库脚本的
- 如何初始化一个xml的dom树
- 如何改变 是swing的JScrollBar的滚动条的[滑块]的颜色?
- 请教高手SwingUtilities.invokeLater 和SwingUtilities.invokeAndWate
- 关于java connection的问题
- java数据类型转换
GridBagConstraints.NORTH
GridBagConstraints.SOUTH
GridBagConstraints.WEST
GridBagConstraints.EAST
GridBagConstraints.NORTHWEST
GridBagConstraints.NORTHEAST
GridBagConstraints.SOUTHWEST
GridBagConstraints.SOUTHEAST
GridBagConstraints.CENTER (the default
相对值
GridBagConstraints.PAGE_START
GridBagConstraints.PAGE_END
GridBagConstraints.LINE_START
GridBagConstraints.LINE_END
GridBagConstraints.FIRST_LINE_START
GridBagConstraints.FIRST_LINE_END
GridBagConstraints.LAST_LINE_START
GridBagConstraints.LAST_LINE_END
恩 恩 恩 dawn大哥说的有道理我感觉这个布局管理器 过于细致了 以至于 有的时候还不如用定坐标来得快
最好自己封装个方法来使用这个布局,否则要设置的属性过多。
通常来说自己只关心控件的行列位置和行列填充。
public static GridBagConstraints getGridBagConstraints(int row, int col, boolean hFill, boolean vFill) {)
方法内部根据参数把gridx,gridy和行列填充设置好,
其他的属性比如weightx,weighty等,设置个自己项目中通用的量吧。布局时如下调用:
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
panel.add(new JTextField(), getGridBagConstraints(1, 1, true, false));
上面这句的意思就是把一个text添加到panel的1行1列位置,横向填充,纵向不填充。ps:代码直接在回复里写的,如果有拼写错误请无视
可以把控件相互扯开。
package test;import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;public class TestGridBag extends JFrame { private JTextArea ta1 = new JTextArea(); private JTextArea ta2 = new JTextArea(1, 10); private JTextArea ta3 = new JTextArea(5, 20); private JPanel menber = new JPanel(); private JScrollPane js1 = new JScrollPane(ta1); private JScrollPane js2 = new JScrollPane(ta2); private JScrollPane js3 = new JScrollPane(ta3); public TestGridBag() {
JPanel jpm = new JPanel(new BorderLayout());
JPanel jpl = new JPanel(new GridLayout(2, 1));
getContentPane().add(jpm);
jpm.add(js1);
jpm.add(js3, BorderLayout.SOUTH);
add(jpl, BorderLayout.EAST);
JPanel jptop = new JPanel(new BorderLayout());
jptop.add(new JLabel("群公告:"), BorderLayout.NORTH);
jptop.add(js2);
jpl.add(jptop);
jpl.add(menber);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(1);
}
}); setTitle("TT2009 硅谷矿机群");
setSize(400, 400);
setVisible(true);
} public static void main(String[] args) {
new TestGridBag();
}}