java中怎么设置布局管理器

包含四个panel... 包含四个panel 展开
 我来答
yugi111
2014-05-09 · TA获得超过8.1万个赞
知道大有可为答主
回答量:5.1万
采纳率:70%
帮助的人:1.4亿
展开全部
import java.applet.Applet;
import java.awt.Button;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class GridBagEx extends Applet
{
private static final long serialVersionUID = 1L;

protected void makebutton ( String name, GridBagLayout gridbag, GridBagConstraints c )
{
Button button = new Button (name);
gridbag.setConstraints (button, c);
add (button);
}

public void init ()
{
GridBagLayout gridbag = new GridBagLayout ();
GridBagConstraints c = new GridBagConstraints ();
setFont (new Font ("SansSerif", Font.PLAIN, 14));
setLayout (gridbag);
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
makebutton ("Button1", gridbag, c);
makebutton ("Button2", gridbag, c);
makebutton ("Button3", gridbag, c);
c.gridwidth = GridBagConstraints.REMAINDER; // end row
makebutton ("Button4", gridbag, c);
c.weightx = 0.0; // reset to the default
makebutton ("Button5", gridbag, c); // another row
c.gridwidth = GridBagConstraints.RELATIVE; // next-to-last in row
makebutton ("Button6", gridbag, c);
c.gridwidth = GridBagConstraints.REMAINDER; // end row
makebutton ("Button7", gridbag, c);
c.gridwidth = 1; // reset to the default
c.gridheight = 2;
c.weighty = 1.0;
makebutton ("Button8", gridbag, c);
c.weighty = 0.0; // reset to the default
c.gridwidth = GridBagConstraints.REMAINDER; // end row
c.gridheight = 1; // reset to the default
makebutton ("Button9", gridbag, c);
makebutton ("Button10", gridbag, c);
setSize (300, 100);
}

public static void main ( String args[] )
{
Frame f = new Frame ("GridBag Layout Example");
GridBagEx ex = new GridBagEx ();
ex.init ();
f.add (ex);
f.pack ();
f.setLocationRelativeTo (null);
f.addWindowListener (new WindowAdapter ()
{
@Override
public void windowClosing ( WindowEvent e )
{
System.exit (0);
}
});
f.setVisible (true);
}
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式