现在尝试用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();}});
}
}
就是这个我弄一晚上了,有劳各位多多纠正啊。
预期效果:
//姓名:(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();}});
}
}
就是这个我弄一晚上了,有劳各位多多纠正啊。
new GridLayout(2,3,5,5);
2表示将显示为2行,3表示将显示为3列
在数目不是刚好的情况下,以行为主,
后面两个5是,之间的像素距离
lz的GridLayout();
具体的可以看看GridLayout的api吧,里面很清楚
//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();}});
}
}
你的代码中存在如下错误:
1、你只创建了一个JLabel对象,却三次使用它,应该创建三个JLabel对象分别设置;
2、你对class结构不太了解,class中只有三个部分:constructors, fields, and methods。
你看你的代码中constructor部分之前的代码什么都不属于。这些代码应该放入constuctor部分或者methods部分。
感谢啊!!
第二点那constructor部分之前的代码不是成员变量么?
这个是GridBagLayout哦,不过谢谢你的热心!
建议你学习CoreJava原文。