[新人求解]用GridBagLayout设计图形界面
现在尝试用GridBagLayout设计图形界面,可是总打不到预期效果。
预期效果:
//姓名:(JTextField)
//性别:(JRadioButton)(JRadioButton)
//QQ:(JTextField)
(括号中是对应的组件。)
代码如下:
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class AddContact extends JFrame{
JTextField JTName=new JTextField();
JTextField JTQQ=new JTextField();
JTextField JTPhone=new JTextField();
JTextField JTEmail=new JTextField();
JTextField JTAddr=new JTextField();
JRadioButton JRBM=new JRadioButton("男");
JRadioButton JRBF=new JRadioButton("女");
JButton JBAdd=new JButton("提交");
public AddContact()
{
JPanel jp=new JPanel();
GridBagLayout gb=new GridBagLayout();
GridBagConstraints c=new GridBagConstraints();
JLabel jl=new JLabel();
jp.setLayout(gb);
c.anchor = GridBagConstraints.NORTHWEST;
c.fill = GridBagConstraints.BOTH;
//set name info.
c.gridx=0;
c.gridy=0;
c.gridwidth=1;
c.gridheight=1;
jl.setText("姓名:");
gb.setConstraints(jl, c);
jp.add(jl);
c.gridx=1;
c.gridy=0;
c.gridwidth=2;
c.gridheight=1;
gb.setConstraints(JTName, c);
jp.add(JTName);
//set sex info.
c.gridx=0;
c.gridy=1;
c.gridwidth=1;
c.gridheight=1;
jl.setText("性别:");
gb.setConstraints(jl, c);
jp.add(jl);
c.gridx=1;
c.gridy=1;
c.gridwidth=1;
c.gridheight=1;
gb.setConstraints(JRBM, c);
jp.add(JRBM);
c.gridx=2;
c.gridy=1;
c.gridwidth=1;
c.gridheight=1;
gb.setConstraints(JRBF, c);
jp.add(JRBF);
ButtonGroup bg=new ButtonGroup();
bg.add(JRBM);
bg.add(JRBF);
//set QQ info.
c.gridx=0;
c.gridy=2;
c.gridwidth=1;
c.gridheight=1;
jl.setText("QQ:");
gb.setConstraints(jl, c);
jp.add(jl);
c.gridx=1;
c.gridy=2;
c.gridwidth=2;
c.gridheight=1;
gb.setConstraints(JTQQ, c);
jp.add(JTQQ);
this.add(jp);
this.setVisible(true);
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new AddContact();}});
}
}
就是这个我弄一晚上了,有劳各位多多纠正啊。
------解决方案--------------------
在你的基础上作出修改,代码如下:
Java code
//import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
//import java.awt.event.ActionEvent;
//import java.awt.event.ActionListener;
import javax.swing.*;
public class GBCTest extends JFrame{
public GBCTest()
{
JTextField JTName=new JTextField();
JTextField JTQQ=new JTextField();
//JTextField JTPhone=new JTextField();
//JTextField JTEmail=new JTextField();
//JTextField JTAddr=new JTextField();
JRadioButton JRBM=new JRadioButton("男");
JRadioButton JRBF=new JRadioButton("女");
//JButton JBAdd=new JButton("提交");
JPanel jp=new JPanel();
GridBagLayout gb=new GridBagLayout();
GridBagConstraints c=new GridBagConstraints();
JLabel jl= new JLabel();
JLabel j2 = new JLabel();
JLabel j3 = new JLabel();
jp.setLayout(gb);
c.anchor = GridBagConstraints.NORTHWEST;
c.fill = GridBagConstraints.BOTH;
//set name info.
c.gridx=0;
c.gridy=0;
c.gridwidth=1;
c.gridheight=1;
jl.setText("姓名:");
gb.setConstraints(jl, c);
jp.add(jl);
c.gridx=1;
c.gridy=0;
c.gridwidth=2;
c.gridheight=1;
gb.setConstraints(JTName, c);
jp.add(JTName);
//set sex info.
c.gridx=0;
c.gridy=1;
c.gridwidth=1;
c.gridheight=1;
j2.setText("性别:");
gb.setConstraints(j2, c);
jp.add(j2);
c.gridx=1;
c.gridy=1;
c.gridwidth=1;
c.gridheight=1;
gb.setConstraints(JRBM, c);
jp.add(JRBM);
c.gridx=2;
c.gridy=1;
c.gridwidth=1;
c.gridheight=1;
gb.setConstraints(JRBF, c);
jp.add(JRBF);
ButtonGroup bg=new ButtonGroup();
bg.add(JRBM);
bg.add(JRBF);
//set QQ info.
c.gridx=0;
c.gridy=2;
c.gridwidth=1;
c.gridheight=1;
j3.setText("QQ:");
gb.setConstraints(j3, c);
jp.add(j3);
c.gridx=1;
c.gridy=2;
c.gridwidth=2;
c.gridheight=1;
gb.setConstraints(JTQQ, c);
jp.add(JTQQ);
this.add(jp);
this.setVisible(true);
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new GBCTest();}});
}
}