网格袋布局问题???
我想要的结果是利用网格袋显示name,文本域;password,文本域;ok按钮。可是程序执行结果是顺序显示name,竖立的两杠和ok按钮。为什么?大家帮我看看程序有什么问题:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class namepass extends jframe {
void buildconstraints(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 namepass() {
super("username and password");
setsize(290, 110);
gridbaglayout gridbag = new gridbaglayout();
gridbagconstraints constraints = new gridbagconstraints();
jpanel pane = new jpanel();
pane.setlayout(gridbag);
// name label
buildconstraints(constraints, 0, 0, 1, 1, 10, 40);
constraints.fill = gridbagconstraints.none;
constraints.anchor = gridbagconstraints.east;
jlabel label1 = new jlabel("name:", jlabel.left);
gridbag.setconstraints(label1, constraints);
pane.add(label1);
// name text field
buildconstraints(constraints, 1, 0, 1, 1, 90, 0);
constraints.fill = gridbagconstraints.horizontal;
jtextfield tfname = new jtextfield();
gridbag.setconstraints(tfname, constraints);
pane.add(tfname);
// password label
buildconstraints(constraints, 0, 1, 1, 1, 0, 40);
constraints.fill = gridbagconstraints.none;
constraints.anchor = gridbagconstraints.east;
jlabel label2 = new jlabel("password:", jlabel.left);
gridbag.setconstraints(label2, constraints);
pane.add(label2);
// password text field
buildconstraints(constraints, 1, 1, 1, 1, 0, 0);
constraints.fill = gridbagconstraints.horizontal;
jpasswordfield tfpass = new jpasswordfield();
tfpass.setechochar(*);
gridbag.setconstraints(tfpass, constraints);
pane.add(tfpass);
// ok button
buildconstraints(constraints, 0, 2, 2, 1, 0, 20);
constraints.fill = gridbagconstraints.none;
constraints.anchor = gridbagconstraints.center;
jbutton okb = new jbutton("ok");
gridbag.setconstraints(okb, constraints);
pane.add(okb);
// content pane
setcontentpane(pane);
}
public static void main(string[] arguments) {
namepass frame = new namepass();
exitwindow exit = new exitwindow();
frame.addwindowlistener(exit);
frame.show();
}
}
class exitwindow extends windowadapter {
public void windowclosing(windowevent e) {
system.exit(0);
}
}
推荐阅读


讨论区