请问下面的布局哪里有问题?总是对不齐,请帮忙看看,谢谢!public class LoginFrame extends JFrame {
private JPasswordField pwdField;
private JTextField username;
private final Font font = new Font("宋体",Font.LAYOUT_LEFT_TO_RIGHT,13);
private final Font font_1 = new Font("TimesRoman",Font.LAYOUT_LEFT_TO_RIGHT,15);
private final Color color = new Color(225,245,252);
private final Color color_1 = new Color(242,249,255);

public LoginFrame(){
super();
setTitle("登录系统");
setResizable(false);
setLayout(new GridBagLayout());
Dimension frameLocation = FrameBounds.getLoginframeLocation();
setBounds(frameLocation.height,frameLocation.width,360,230);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setBackground(color);
getGlassPane().setBackground(color);

this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
System.exit(0);
}
});

final JLabel titleLabel = new JLabel();
GridBagConstraints gridBagConstraints_0 = new GridBagConstraints();
gridBagConstraints_0.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints_0.gridx = 0;
gridBagConstraints_0.gridy = 0;
gridBagConstraints_0.gridheight = 1;
gridBagConstraints_0.gridwidth = 3;

titleLabel.setIcon(CreatedIcon.create("title.jpg"));
getContentPane().add(titleLabel,gridBagConstraints_0);

final JPanel loginPanel = new JPanel();
loginPanel.setLayout(new FlowLayout());
loginPanel.setBackground(color_1);
TitledBorder border = BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "管理员登录",TitledBorder.DEFAULT_JUSTIFICATION,TitledBorder.TOP,font_1);
loginPanel.setBorder(border);
GridBagConstraints gridBagConstraints_1 = new GridBagConstraints();
gridBagConstraints_1.gridx = 1;
gridBagConstraints_1.gridy = 1;
gridBagConstraints_0.gridheight = 1;
gridBagConstraints_0.gridwidth = 1;

final JLabel userLabel = new JLabel();
userLabel.setText("管 理 员 ");
userLabel.setFont(font_1);
loginPanel.add(userLabel);
username = new JTextField(20);
username.setFont(font);
loginPanel.add(username);

final JLabel pwdLabel = new JLabel();
pwdLabel.setText("密    码 ");
pwdLabel.setFont(font_1);
loginPanel.add(pwdLabel);
pwdField = new JPasswordField(20);
pwdField.setFont(font);
pwdField.setEchoChar('*');
loginPanel.add(pwdField);
getContentPane().add(loginPanel,gridBagConstraints_1);

final JPanel buttonPanel = new JPanel();
final FlowLayout flowLayout = new FlowLayout();
buttonPanel.setLayout(flowLayout);
buttonPanel.setBackground(color);
GridBagConstraints gridBagConstraints_2 = new GridBagConstraints();
gridBagConstraints_2.gridx = 1;
gridBagConstraints_2.gridy = 2;
gridBagConstraints_0.gridheight = 1;
gridBagConstraints_0.gridwidth = 1;

final JButton loginButton = new JButton();
loginButton.setText("登录");
loginButton.setFont(font_1);

final JButton resetButton = new JButton();
resetButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
username.setText("");
username.grabFocus();
pwdField.setText("");
}
});
resetButton.setText("重置");
resetButton.setFont(font_1);

final JButton exitButton = new JButton();
exitButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
dispose();
}
});
exitButton.setText("退出");
exitButton.setFont(font_1);

getContentPane().add(exitButton);
getContentPane().add(resetButton);
getContentPane().add(loginButton);

getContentPane().add(buttonPanel,gridBagConstraints_2);
}

public static void main(String[] args){
try{
LoginFrame loginFrame = new LoginFrame();
loginFrame.pack();
loginFrame.setVisible(true);
}catch(Exception e){
JOptionPane.showMessageDialog(null, "登录异常,请重新登录!","登录异常",JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
}
}
}

解决方案 »

  1.   

    给描述一下界面的布局,你的GridBagConstraints参数都设乱了。还是使用带参数的构造函数 
    new GridBagConstraints(gridX, gridY, gridWidth, gridHeight, weightX, weightY, anchor, fill,new Insets(0, 0, 0, 0), 0, 0)
      

  2.   

    都不检查自己的代码吗?建议好好看看GridBagConstraints再使用gridBagConstraints_2.gridx = 1; //再说,这里如果要另起一行添加的话,gridx初始应该为0,如果前面不需要占位的话
    gridBagConstraints_2.gridy = 2; 
    gridBagConstraints_0.gridheight = 1; 
    gridBagConstraints_0.gridwidth = 1;gridBagConstraints_1.gridx = 1; 
    gridBagConstraints_1.gridy = 1; 
    gridBagConstraints_0.gridheight = 1; 
    gridBagConstraints_0.gridwidth = 1;getContentPane().add(exitButton); //应该是buttonPanel
    getContentPane().add(resetButton); 
    getContentPane().add(loginButton);
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.Font;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;import javax.swing.BorderFactory;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JPasswordField;
    import javax.swing.JTextField;
    import javax.swing.border.TitledBorder;public class LoginFrame extends JFrame {
    private JPasswordField pwdField;
    private JTextField username;
    private final Font font = new Font("宋体", Font.LAYOUT_LEFT_TO_RIGHT, 13);
    private final Font font_1 = new Font("TimesRoman",
    Font.LAYOUT_LEFT_TO_RIGHT, 15);
    private final Color color = new Color(225, 245, 252);
    private final Color color_1 = new Color(242, 249, 255); public LoginFrame() {
    super();
    setTitle("登录系统");
    setResizable(false);
    getContentPane().setLayout(new GridBagLayout());
    setBounds(0, 0, 360, 230);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    getContentPane().setBackground(color);
    getGlassPane().setBackground(color); this.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    dispose();
    System.exit(0);
    }
    }); final JLabel titleLabel = new JLabel();
    GridBagConstraints gridBagConstraints_0 = new GridBagConstraints(0, 0,
    1, 1, 0.0, 0.0, GridBagConstraints.EAST,
    GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 0), 0, 0); titleLabel
    .setIcon(new ImageIcon(getClass().getResource("catalog.gif")));
    getContentPane().add(titleLabel, gridBagConstraints_0); final JPanel loginPanel = new JPanel();
    loginPanel.setLayout(new FlowLayout());
    loginPanel.setBackground(color_1);
    TitledBorder border = BorderFactory.createTitledBorder(BorderFactory
    .createEtchedBorder(), "管理员登录",
    TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.TOP, font_1);
    loginPanel.setBorder(border);
    GridBagConstraints gridBagConstraints_1 = new GridBagConstraints(0, 1,
    1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
    GridBagConstraints.BOTH, new Insets(0, 5, 0, 5), 0, 0); final JLabel userLabel = new JLabel();
    userLabel.setText("管 理 员 ");
    userLabel.setFont(font_1);
    loginPanel.add(userLabel);
    username = new JTextField(20);
    username.setFont(font);
    loginPanel.add(username); final JLabel pwdLabel = new JLabel();
    pwdLabel.setText("密    码 ");
    pwdLabel.setFont(font_1);
    loginPanel.add(pwdLabel);
    pwdField = new JPasswordField(20);
    pwdField.setFont(font);
    pwdField.setEchoChar('*');
    loginPanel.add(pwdField);
    getContentPane().add(loginPanel, gridBagConstraints_1); final JPanel buttonPanel = new JPanel();
    final FlowLayout flowLayout = new FlowLayout();
    buttonPanel.setLayout(flowLayout);
    buttonPanel.setBackground(color);
    GridBagConstraints gridBagConstraints_2 = new GridBagConstraints(0, 2,
    1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
    GridBagConstraints.BOTH, new Insets(0, 5, 0, 5), 0, 0); final JButton loginButton = new JButton();
    loginButton.setText("登录");
    loginButton.setFont(font_1); final JButton resetButton = new JButton();
    resetButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    username.setText("");
    username.grabFocus();
    pwdField.setText("");
    }
    });
    resetButton.setText("重置");
    resetButton.setFont(font_1); final JButton exitButton = new JButton();
    exitButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    dispose();
    }
    });
    exitButton.setText("退出");
    exitButton.setFont(font_1); buttonPanel.add(exitButton);
    buttonPanel.add(resetButton);
    buttonPanel.add(loginButton); getContentPane().add(buttonPanel, gridBagConstraints_2);
    } public static void main(String[] args) {
    try {
    LoginFrame loginFrame = new LoginFrame();
    loginFrame.pack();
    loginFrame.setVisible(true);
    } catch (Exception e) {
    JOptionPane.showMessageDialog(null, "登录异常,请重新登录!", "登录异常",
    JOptionPane.ERROR_MESSAGE);
    e.printStackTrace();
    }
    }
    }
      

  3.   

    这个不是我强项。不熟悉GUI。其他人继续