当前位置: 技术问答>java相关
GridBag Layout里的GridBagConstraint的weightx,weighty的取值问题?
来源: 互联网 发布时间:2015-03-09
本文导语: 先看这个程序,用GB布局的 import java.awt.*; public class GB1 extends Panel { private Panel tallPanel=new Panel(); private Panel tallPanel2=new Panel(); public GB1() { tallPanel.setLayout(new GridLayout(3, 1)); tallPanel.ad...
先看这个程序,用GB布局的
import java.awt.*;
public class GB1 extends Panel
{
private Panel tallPanel=new Panel();
private Panel tallPanel2=new Panel();
public GB1()
{
tallPanel.setLayout(new GridLayout(3, 1));
tallPanel.add(new Button("Press"));
tallPanel.add(new Button("Any"));
tallPanel.add(new Button("One"));
tallPanel2.setLayout(new GridLayout(3, 1));
tallPanel2.add(new Button("Don't"));
tallPanel2.add(new Button("Press"));
tallPanel2.add(new Button("These"));
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
add(new Button("TopLeft"), c);
c.gridx = 1;
add(new Button("TopMiddle"), c);
c.gridx = 2;
c.weightx = 50.0; //This is a stretchy column
add(new Button("TopRight"), c);
c.weightx = 0.0; //No other column strectches
c.gridx = 0;
c.gridy = 1;
add(new Button("LeftHandSideMiddle"), c);
c.gridx = 1;
add(tallPanel, c);
c.gridy = 2; // note, sets _y_
add(new Button("BottomCenter"), c);
c.gridx = 2;
c.weighty = 1.0; //This is a stretchy row
add(tallPanel2, c);
c.weighty = 0.0; //No other row stretches
}
public static void main(String args[])
{
Frame f = new Frame("GridBag 1 example");
f.add(new GB1());
f.pack();
f.setVisible(true);
}
}
问问:
1.我在注释的那几行里修改weightx,weighty的值,从1到50,然后resize窗口大小,看山去都一样嘛?究竟weightx,weighty应该怎么取值啊?
2.weightx,weighty的值一定要是double的吗?
import java.awt.*;
public class GB1 extends Panel
{
private Panel tallPanel=new Panel();
private Panel tallPanel2=new Panel();
public GB1()
{
tallPanel.setLayout(new GridLayout(3, 1));
tallPanel.add(new Button("Press"));
tallPanel.add(new Button("Any"));
tallPanel.add(new Button("One"));
tallPanel2.setLayout(new GridLayout(3, 1));
tallPanel2.add(new Button("Don't"));
tallPanel2.add(new Button("Press"));
tallPanel2.add(new Button("These"));
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
add(new Button("TopLeft"), c);
c.gridx = 1;
add(new Button("TopMiddle"), c);
c.gridx = 2;
c.weightx = 50.0; //This is a stretchy column
add(new Button("TopRight"), c);
c.weightx = 0.0; //No other column strectches
c.gridx = 0;
c.gridy = 1;
add(new Button("LeftHandSideMiddle"), c);
c.gridx = 1;
add(tallPanel, c);
c.gridy = 2; // note, sets _y_
add(new Button("BottomCenter"), c);
c.gridx = 2;
c.weighty = 1.0; //This is a stretchy row
add(tallPanel2, c);
c.weighty = 0.0; //No other row stretches
}
public static void main(String args[])
{
Frame f = new Frame("GridBag 1 example");
f.add(new GB1());
f.pack();
f.setVisible(true);
}
}
问问:
1.我在注释的那几行里修改weightx,weighty的值,从1到50,然后resize窗口大小,看山去都一样嘛?究竟weightx,weighty应该怎么取值啊?
2.weightx,weighty的值一定要是double的吗?
|
1.weightx,weighty表示控件使用剩余空间得 相对分额。只要相对分额不变,你就看不出变化
|
如果一行中放了2个控件,第一个控件weightx=1;2nd control weightx=1;那么1st control占1/(1+1)的剩余空间(x方向)
第一个控件weightx=2;2nd control weightx=1;那么1st control占2/(2+1)的剩余空间(x方向)
第一个控件weightx=2;2nd control weightx=1;那么1st control占2/(2+1)的剩余空间(x方向)
|
f.pack();//没有剩余空间了,weightx就没有意义了
|
weightx,weighty的有效值在0.0-1.0之间.
实际上它是对单元格起作用的.
不是对组件起作用的.
看不出效果,你需要加这么一句话 c.fill = c.HORIZONTAL;
实际上它是对单元格起作用的.
不是对组件起作用的.
看不出效果,你需要加这么一句话 c.fill = c.HORIZONTAL;
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。