一个网袋布局(GridBagLayout)的问题

feifei32 2004-09-17 03:17:58
import java.awt.*;
public class guitest6 extends Frame
{
public guitest6()
{
setTitle("网袋布局");
GridBagLayout layout=new GridBagLayout();
Panel p=new Panel(layout);
GridBagConstraints cons=new GridBagConstraints();
cons.gridx=100;
cons.gridy=100;
cons.gridwidth=1;
cons.gridheight=4;
cons.weightx=0;
cons.weighty=GridBagConstraints.RELATIVE;
Button b1=new Button("A");
layout.setConstraints(b1,cons);
p.add(b1);

cons.gridx=200;
cons.gridy=100;
cons.gridwidth=1;
cons.gridheight=1;
cons.weightx=0;
cons.weighty=0;
Button b2=new Button("B");
layout.setConstraints(b2,cons);
p.add(b2);

cons.gridx=200;
cons.gridy=125;
cons.gridwidth=1;
cons.gridheight=1;
cons.weightx=0;
cons.weighty=0;
Button b3=new Button("C");
layout.setConstraints(b3,cons);
p.add(b3);

cons.gridx=200;
cons.gridy=150;
cons.gridwidth=1;
cons.gridheight=1;
cons.weightx=0;
cons.weighty=0;
Button b4=new Button("D");
layout.setConstraints(b4,cons);
p.add(b4);

cons.gridx=200;
cons.gridy=175;
cons.gridwidth=1;
cons.gridheight=1;
cons.weightx=0;
cons.weighty=0;
Button b5=new Button("E");
layout.setConstraints(b5,cons);
p.add(b5);



add("Center",p);




}
public boolean handleEvent(Event evt)
{
if(evt.id==Event.WINDOW_DESTROY)System.exit(0);
return super.handleEvent(evt);

}

public static void main(String[] args)
{
guitest6 f=new guitest6();
f.resize(600,500);
f.setBackground(Color.white);
f.show();
}
}

想用GridBagLayout实现下面的按钮效果,那里不对,为什么?
----------------------
| ________________ |
| | | B | |
| | --------- |
| | | C | |
| | A --------- |
| | | D | |
| | --------- |
| | | E | |
| ---------------- |
|____________________|
...全文
219 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
mail25 2004-09-22
  • 打赏
  • 举报
回复
把添加组件的方法改一下就好了


import java.awt.*;

public class test {

public static void main(String args[]) {
Frame f = new Frame(" GridBagLayout Simples");
GridBagConstraints c ;

f.setLayout(new GridBagLayout());

f.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
System.exit(0);
}
});

c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.gridheight = 4;
c.fill = GridBagConstraints.VERTICAL;
f.add(new Button("A"), c);

c = new GridBagConstraints();
c.gridx = 1;
c.gridy = 0;
f.add(new Button("B"), c);

c = new GridBagConstraints();
c.gridx = 1;
c.gridy = 1;
f.add(new Button("C"), c);

c = new GridBagConstraints();
c.gridx = 1;
c.gridy = 2;
f.add(new Button("E"), c);

c = new GridBagConstraints();
c.gridx = 1;
c.gridy = 3;
f.add(new Button("E"), c);

f.pack();
f.show();


}
}
feifei32 2004-09-22
  • 打赏
  • 举报
回复
bovy(★ 天道酬勤 ★) 你的我试过了,好像不行,mail25(随风飘) 的可以。难道awt就实现不了吗?
继续等待.....
bovy 2004-09-18
  • 打赏
  • 举报
回复
package com.boasoft;

import java.awt.Button;
import java.awt.Color;
import java.awt.Event;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Panel;

public class RadioButtonDemo extends Frame
{
public RadioButtonDemo()
{
setTitle("网袋布局");
GridBagLayout layout=new GridBagLayout();

Panel p=new Panel(layout);
GridBagConstraints cons=new GridBagConstraints();
cons.gridx=100;
cons.gridy=135; // modified here
cons.gridwidth=2;
cons.gridheight=4;
cons.weightx=0;
cons.weighty=GridBagConstraints.RELATIVE;
Button b1=new Button("A");
layout.setConstraints(b1,cons);
p.add(b1);

cons.gridx=200;
cons.gridy=100;
cons.gridwidth=1;
cons.gridheight=1;
cons.weightx=0;
cons.weighty=0;
Button b2=new Button("B");
layout.setConstraints(b2,cons);
p.add(b2);

cons.gridx=200;
cons.gridy=125;
cons.gridwidth=1;
cons.gridheight=1;
cons.weightx=0;
cons.weighty=0;
Button b3=new Button("C");
layout.setConstraints(b3,cons);
p.add(b3);

cons.gridx=200;
cons.gridy=150;
cons.gridwidth=1;
cons.gridheight=1;
cons.weightx=0;
cons.weighty=0;
Button b4=new Button("D");
layout.setConstraints(b4,cons);
p.add(b4);

cons.gridx=200;
cons.gridy=175;
cons.gridwidth=1;
cons.gridheight=1;
cons.weightx=0;
cons.weighty=0;
Button b5=new Button("E");
layout.setConstraints(b5,cons);
p.add(b5);



add("Center",p);




}
public boolean handleEvent(Event evt)
{
if(evt.id==Event.WINDOW_DESTROY)System.exit(0);
return super.handleEvent(evt);

}

public static void main(String[] args)
{
RadioButtonDemo f=new RadioButtonDemo();
f.resize(600,500);
f.setBackground(Color.white);
f.show();
}
}
//是要这个效果吗?建议你使用JPanel中来布局,就像HTML使用<table>来布局一样
Collection 2004-09-18
  • 打赏
  • 举报
回复
应该用JBuilder来创建就不用发愁布局的问题了
feifei32 2004-09-18
  • 打赏
  • 举报
回复
ding
mail25 2004-09-18
  • 打赏
  • 举报
回复
这个是你想要的效果啊!测试过没问题的

import javax.swing.*;
import java.awt.*;

public class test {

public static void main(String args[]) {
JFrame f = new JFrame(" GridBagLayout Simples");
GridBagConstraints c ;

f.getContentPane().setLayout(new GridBagLayout());

f.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
System.exit(0);
}
});

c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.gridheight = 4;
c.fill = GridBagConstraints.VERTICAL;
f.getContentPane().add(new JButton("A"), c);

c = new GridBagConstraints();
c.gridx = 1;
c.gridy = 0;
f.getContentPane().add(new JButton("B"), c);

c = new GridBagConstraints();
c.gridx = 1;
c.gridy = 1;
f.getContentPane().add(new JButton("C"), c);

c = new GridBagConstraints();
c.gridx = 1;
c.gridy = 2;
f.getContentPane().add(new JButton("E"), c);

c = new GridBagConstraints();
c.gridx = 1;
c.gridy = 3;
f.getContentPane().add(new JButton("E"), c);

f.pack();
f.show();


}
}
mail25 2004-09-18
  • 打赏
  • 举报
回复
JBuilder是好用,但公司现在不用这东西,用EditPlus + JDK,就是在做GUI时比较痛苦
理想哥 2004-09-17
  • 打赏
  • 举报
回复
高手快来呀!!期待准确完整的答复。
feifei32 2004-09-17
  • 打赏
  • 举报
回复
ding
mail25 2004-09-17
  • 打赏
  • 举报
回复
这东东功能强,但不是很好用,特别是在不用IDE开发时,昨天为了一个布局折腾了一上午才搞好,看到它就头大!
bradwoo8621 2004-09-17
  • 打赏
  • 举报
回复
Insets insets = new Insets(5,5 ,5 , 5);
panel.add(a, new GridBagConstraints(0, 0, 1, 4, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, insets, 0, 0));
panel.add(b, new GridBagConstraints(1, 0, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, insets, 0, 0));
panel.add(c, new GridBagConstraints(2, 0, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, insets, 0, 0));
panel.add(d, new GridBagConstraints(3, 0, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, insets, 0, 0));
panel.add(e, new GridBagConstraints(4, 0, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, insets, 0, 0));
feifei32 2004-09-17
  • 打赏
  • 举报
回复
ding
feifei32 2004-09-17
  • 打赏
  • 举报
回复
自己ding

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧