【Java】关于图形用户编程(为什么最后一个按钮会布满整个窗体)

//importjava.awt.BorderLayout;importjava.awt.Container;importjava.awt.Dimension;//imp... //import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
//import java.awt.FlowLayout;
//import java.awt.GridBagConstraints;
//import java.awt.GridBagLayout;
//import java.awt.GridLayout;
//import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
//import javax.swing.JPanel;
import javax.swing.JTextField;

public class GridLayoutDemo extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
JLabel name,sex,address;
JTextField nameText,addressText;
JButton ok,exit;
JComboBox sexBox;
public GridLayoutDemo(){
Container contentPane = this.getContentPane();
//contentPane.setLayout(new GridBagLayout());
//contentPane.setLayout(new FlowLayout());

name = new JLabel("姓名:");
sex = new JLabel("性别:");
address = new JLabel("地址:");
nameText = new JTextField();
addressText = new JTextField();
ok = new JButton("确认");
exit = new JButton("退出");
sexBox = new JComboBox();
sexBox.addItem("男");
sexBox.addItem("女");
contentPane.add(name);
contentPane.add(nameText);
contentPane.add(sex);
contentPane.add(sexBox);
contentPane.add(address);
contentPane.add(addressText);
contentPane.add(ok);
contentPane.add(exit);
//contentPane.add();
//new JSeparator(JSeparator.HORIZONTAL);

name.setBounds(10, 10, 40,20);
nameText.setBounds(55, 10, 80, 20);
sex.setBounds(140, 10, 40, 20);
sexBox.setBounds(185, 10, 80, 20);
address.setBounds(10, 35, 40, 20);
addressText.setBounds(55,35,185,20);
ok.setBounds(65,60,80,30);
//exit.setBounds(155,60,80,30);
//exit.setPreferredSize(new Dimension(80,40));
/*
* 固定组件大小方法
*/
Dimension dim = new Dimension(80,40);
exit.setMaximumSize(dim);
exit.setMinimumSize(dim);
exit.setPreferredSize(dim);

/*
contentPane.add(name, new GridBagConstraints(0,0,1,1,0,0,GridBagConstraints.CENTER,
GridBagConstraints.HORIZONTAL,new Insets(0,5,5,5),0,0));
contentPane.add(nameText, new GridBagConstraints(GridBagConstraints.RELATIVE,0,5,1,0,0,GridBagConstraints.NORTHWEST,
GridBagConstraints.HORIZONTAL,new Insets(5,5,5,5),0,0));
contentPane.add(sex, new GridBagConstraints(7,0,1,1,0,0,GridBagConstraints.NORTH,
GridBagConstraints.HORIZONTAL,new Insets(5,5,5,5),0,0));
contentPane.add(sexBox, new GridBagConstraints(8,0,3,1,0,0,GridBagConstraints.CENTER,
GridBagConstraints.HORIZONTAL,new Insets(5,5,5,5),0,0));
/* contentPane.add(address, new GridBagConstraints(1,2,1,1,0,0,GridBagConstraints.CENTER,
GridBagConstraints.HORIZONTAL,new Insets(5,5,5,5),0,0));
*/
validate();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100,100,200,200);
}

public static void main(String[] args){
new GridLayoutDemo();
}
}
能不能具体详细点 ,谢谢 我试过其他布局,那个GridBagConstraints太繁琐了 就甭提了诶
展开
 我来答
bellCliff
2010-05-04 · TA获得超过650个赞
知道小有建树答主
回答量:820
采纳率:0%
帮助的人:614万
展开全部
布局问题,你选择的布局方式不对
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
帐号已注销
2010-05-04 · TA获得超过5622个赞
知道大有可为答主
回答量:1459
采纳率:0%
帮助的人:1379万
展开全部
contentPane.setLayout(null);
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式