求解,GridBagLayout布局,组件没有垂直间距,都连在一起
秋月夜 2014-03-04 08:52:40 组件垂直间距没有,都连在一起,不知道问题出在哪里,求解
import javax.swing.*;
import MDB.MDB;
import MainFrame.MainFrame;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class Logo{
private JFrame LogoFrame;
private JLabel lbTitle,lbName,lbPwd;
private JTextField tfName;
private JPasswordField tfPwd;
private JButton btnLogo,btnOut;
private GridBagLayout Layout;
private GridBagConstraints constraints;
public Logo(){
LogoFrame=new JFrame("登陆");
LogoFrame.setSize(300,200);
LogoFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Layout=new GridBagLayout();
//LogoFrame.setResizable(false);
LogoFrame.setLocation(500,300);
constraints=new GridBagConstraints();
LogoFrame.setLayout(Layout);
//添加用户名文本框
constraints.fill=GridBagConstraints.HORIZONTAL;
constraints.anchor=GridBagConstraints.CENTER;
tfName=new JTextField();
tfName.addKeyListener(new KeyListener(){
public void keyTyped(KeyEvent e) {}
public void keyPressed(KeyEvent e) {
if((e.getKeyChar() == KeyEvent.VK_ENTER )&&(tfName.getText().trim() != "")){
tfPwd.requestFocus();
}
}
public void keyReleased(KeyEvent e) {}
});
AddComponent(LogoFrame,Layout,constraints,tfName,1,1,1,1,70,70);
//添加秘密文本
tfPwd=new JPasswordField();
tfPwd.addKeyListener(new KeyListener(){
public void keyTyped(KeyEvent e) {}
public void keyPressed(KeyEvent e) {
if((e.getKeyChar() == KeyEvent.VK_ENTER )&&(tfPwd.getText().trim() != "")){
btnLogo.requestFocus();
}
}
public void keyReleased(KeyEvent e) {}
});
AddComponent(LogoFrame,Layout,constraints,tfPwd,1,2,1,1,70,70);
//添加标题
constraints.fill=GridBagConstraints.NONE;
lbTitle=new JLabel("酒店管理系统");
lbTitle.setForeground(Color.red);
lbTitle.setFont(new Font("Monospaced",Font.BOLD, 30));
AddComponent(LogoFrame,Layout,constraints,lbTitle,0,0,2,1,0,0);
//添加用户名标签
lbName=new JLabel("用户名");
AddComponent(LogoFrame,Layout,constraints,lbName,0,1,1,1,30,30);
//添加秘密标签
lbPwd=new JLabel("密码");
AddComponent(LogoFrame,Layout,constraints,lbPwd,0,2,1,1,30,30);
//添加登陆按钮
btnLogo=new JButton("登陆");
AddComponent(LogoFrame,Layout,constraints,btnLogo,0,3,1,1,50,50);
btnLogo.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//如果用户名为空,则弹出对话框提示,并且tfUSer获得焦点
if((tfName.getText().trim() == "")){
JOptionPane.showMessageDialog(null,"请输入用户名","失败",JOptionPane.ERROR_MESSAGE);
tfName.requestFocus();
}
if((tfPwd.getText().trim() =="")){
JOptionPane.showMessageDialog(null,"请输入密码","失败",JOptionPane.ERROR_MESSAGE);
tfPwd.requestFocus();
}
//如果都不为空
if(tfName.getText().trim()!=""&& tfPwd.getText().trim()!=""){
//定义b接受用户名密码判断的结果
boolean b = MDB.check(tfName.getText().trim(), tfPwd.getText().trim());
//用户名密码匹配,则登陆框消失,初始化主界面,设置参数
if(b==true){
LogoFrame.dispose();
new MainFrame(tfName.getText().trim());
}
//用户名密码不匹配,则清空已输入项,tfUser获得焦点,并给出提示
else{
tfName.setText("");
tfPwd.setText("");
tfName.requestFocus();
javax.swing.JOptionPane.showMessageDialog(null, "用户名或密码错误,请重新登陆","登录失败",JOptionPane.ERROR_MESSAGE);
}
}
}
});
//添加退出按钮
btnOut=new JButton("退出");
btnOut.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
LogoFrame.dispose();
}
});
AddComponent(LogoFrame,Layout,constraints,btnOut,1,3,1,1,50,50);
LogoFrame.show();
}
private void AddComponent(JFrame frame,GridBagLayout Layout,GridBagConstraints gbc,Component com,int row,int column,int numRows,int numColumns,int Weightx,int Weighty){
gbc.gridx=row;
gbc.gridy=column;
gbc.gridwidth=numRows;
gbc.gridheight=numColumns;
gbc.weightx=Weightx;
gbc.weightx=Weighty;
Layout.setConstraints(com, gbc);
frame.add(com);
}
public static void main(String []args){
new Logo();
}
}