【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太繁琐了 就甭提了诶 展开
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太繁琐了 就甭提了诶 展开
2个回答
展开全部
布局问题,你选择的布局方式不对
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询