GridBagLayout小问题

gridbagwt 2011-11-16 09:22:06
刚刚学GridBagLayout,不太会,看api也没太理解
下面是我写的东西,运行结果是三个按钮在中间排列放着
我想实现的是让在三个按钮从最上开始依次往下排,不是从中间开始,该怎么该?

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

public class GridBagEx22 extends JPanel {

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();
setLayout(gridbag);

c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.gridwidth = GridBagConstraints.REMAINDER; //end row
makebutton("Button1", gridbag, c);
makebutton("Button2", gridbag, c);
makebutton("Button3", gridbag, c);

setSize(500, 300);
}

public static void main(String args[]) {
Frame f = new Frame("GridBag Layout Example");
GridBagEx22 ex1 = new GridBagEx22();

ex1.init();

f.add("Center", ex1);
f.setSize(500,400);
f.setVisible(true);
}
}
...全文
62 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
jxhxl1990 2011-11-17
  • 打赏
  • 举报
回复
c.gridx = 0;
c.gridy = 0;

gridy渐涨?

67,542

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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