网格布局显示不对,困扰我好几天了.
import java.awt.*;import java.awt.event.*;
import javax.swing.*;
public class GriBagLayout extends JFrame
{
void buildCo(GridBagConstraints gbc, int gx,int gy,int gw,int gh,int wx,int wy)
{
gbc.gridx=gx;
gbc.gridy=gy;
gbc.gridwidth=gw;
gbc.gridheight=gh;
gbc.weightx=wx;
gbc.weighty=wy;
}
public GriBagLayout()
{
super("网格布局");
setSize(290,110);
GridBagLayout gridbag=new GridBagLayout();
GridBagConstraints constraints=new GridBagConstraints();
JPanel pan=new JPanel();
pan.setLayout(gridbag);
//Name label
buildCo(constraints,0,0,1,1,10,40);
constraints.fill=GridBagConstraints.NONE;
constraints.anchor=GridBagConstraints.EAST;
JLabel label=new JLabel("Name:",JLabel.LEFT);
gridbag.setConstraints(label,constraints);
pan.add(label);
//Name text field
buildCo(constraints,1,0,1,1,90,0);
constraints.fill=GridBagConstraints.HORIZONTAL;
JTextField text=new JTextField();
gridbag.setConstraints(text,constraints);
pan.add(text);
// passwore label
buildCo(constraints,0,1,1,1,0,40);
constraints.fill=GridBagConstraints.NONE;
constraints.anchor=GridBagConstraints.EAST;
JLabel labe=new JLabel("password:",JLabel.LEFT);
pan.add(labe);
// password text
buildCo(constraints,1,1,1,1,0,0);
constraints.fill=GridBagConstraints.HORIZONTAL;
// constraints.anchor=GridBagConstraints.CENTER;
JPasswordField tex=new JPasswordField();
tex.setEchoChar('*');
gridbag.setConstraints(tex,constraints);
pan.add(tex);
//OK button
buildCo(constraints,0,2,2,1,0,20);
constraints.fill=GridBagConstraints.NONE;
constraints.anchor=GridBagConstraints.CENTER;
JButton button=new JButton("OK");
gridbag.setConstraints(button,constraints);
pan.add(button);
//content pane
setContentPane(pan);
}
public static void main(String[] args)
{
GriBagLayout grib=new GriBagLayout();
ExitWindow exit=new ExitWindow();
grib.addWindowListener(exit);
grib.show();
}
}
class ExitWindow extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
为什么出现这样的图形,我好像没有写错啊!!!! 怎么到现在还没有帮我解决啊!!!!!
[此贴子已经被作者于2007-5-25 8:32:17编辑过]