我正在尝试创建漂亮的JFrame登录面板

时间:2014-04-10 21:25:12

标签: java swing jframe jpanel jtextfield

截至目前,我正在尝试创建一个基本的登录GUI。我希望看起来像这样

              UserID                              Password
    <EDITABLE AREA FOR USERID>           <EDITABLE AREA FOR Password> 
   "Error Msg displayed here if there exists error, otherwise NO SPACE"
                            [Login Button]

以下是我目前正在尝试使用的代码。我知道目前没有任何功能,我将添加监听器并在以后提供功能。我目前的问题是间距似乎已经关闭,我不知道如何改进它。

public class Login extends JFrame{

    /**
     * 
     */
    private static final long serialVersionUID = 1023608189590961526L;

    JTextField userName, passWord, userNameLabel, passWordLabel; 
    JTextField errorMsg; 
    JButton login; 
    JPanel userNamePanel, passWordPanel, errorMsgPanel, loginPanel; 
    public Login(String Title){  
        super(Title);//Set Frame Title 

        login = new JButton("Login");
        initTextFields(); 
        setLayout(new GridBagLayout()); 

        userNamePanel = new JPanel(new GridBagLayout()); 
        passWordPanel = new JPanel(new GridBagLayout());
        GridBagConstraints constraints = new GridBagConstraints();   
            constraints.fill = GridBagConstraints.BOTH; 
            constraints.weightx = 1; 
            constraints.weighty = 1; 
            constraints.gridwidth = GridBagConstraints.REMAINDER; 
            constraints.gridx = 0; 
            constraints.gridy = 0; 
        userNamePanel.add(userNameLabel, constraints);
        passWordPanel.add(passWordLabel, constraints);
            constraints.gridy++; 
        userNamePanel.add(userName, constraints); 
        passWordPanel.add(passWord, constraints);
        constraints.gridwidth = GridBagConstraints.RELATIVE; 
        constraints.fill = GridBagConstraints.NONE;
        this.add(userNamePanel, constraints);
            constraints.gridwidth = GridBagConstraints.REMAINDER; 
            constraints.gridx = 2; 
        this.add(passWordPanel, constraints);
            constraints.gridy++; 
            constraints.gridy++;
            constraints.gridx--; 
        this.add(login, constraints); 
            constraints.gridwidth = GridBagConstraints.REMAINDER; 
            constraints.gridx = 0; 
            constraints.gridy--;
        this.add(errorMsg, constraints); 
        errorMsg.setVisible(false);



    }

    private boolean initTextFields(){ 
        userName = new JTextField(20); 
        passWord = new JTextField(20);
        userNameLabel = new JTextField("UserID", 20);
            userNameLabel.setBorder(javax.swing.BorderFactory.createEmptyBorder());
            userNameLabel.setEditable(false);
            userNameLabel.setHorizontalAlignment(JTextField.CENTER);
        passWordLabel = new JTextField("Password", 20); 
            passWordLabel.setBorder(javax.swing.BorderFactory.createEmptyBorder());
            passWordLabel.setEditable(false);
            passWordLabel.setHorizontalAlignment(JTextField.CENTER);
        errorMsg = new JTextField("", 50);
            errorMsg.setBorder(javax.swing.BorderFactory.createEmptyBorder());
            errorMsg.setEditable(false);
            errorMsg.setForeground(Color.red);
            passWordLabel.setHorizontalAlignment(JTextField.CENTER);

            return true; 
    }

}

我在我的主要方法中初始化如下:

Login testLog = new Login("Login GUI");
        testLog.setSize(525, 200);// Sets the Frame Size
        testLog.setMinimumSize(new Dimension(525,200));
        testLog.setResizable(false);//Makes the size fixed
        testLog.setLocationRelativeTo(null); // Make the Frame appear centered
        testLog.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//Tells program to exit when the 'x'(close) button is pressed
        testLog.pack();
        testLog.setVisible(true);

1 个答案:

答案 0 :(得分:0)

试试这个:

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Login extends JFrame{

    /**
     * 
     */
    private static final long serialVersionUID = 1023608189590961526L;

    JTextField userName, passWord, userNameLabel, passWordLabel; 
    JTextField errorMsg; 
    JButton login; 
    JPanel userNamePanel, passWordPanel, errorMsgPanel, loginPanel; 
    public Login(String Title){  
        super(Title);//Set Frame Title 

        login = new JButton("Login");
        initTextFields(); 
        setLayout(new GridBagLayout()); 

        userNamePanel = new JPanel(new GridBagLayout()); 
        passWordPanel = new JPanel(new GridBagLayout());
        GridBagConstraints constraints = new GridBagConstraints();   
            constraints.fill = GridBagConstraints.NONE; 
            constraints.weightx = 1; 
            constraints.weighty = 1; 

            constraints.gridx = 0; 
            constraints.gridy = 0; 
        userNamePanel.add(userNameLabel, constraints);
        passWordPanel.add(passWordLabel, constraints);
            constraints.gridy++; 
        userNamePanel.add(userName, constraints); 
        passWordPanel.add(passWord, constraints);
        constraints.gridwidth = GridBagConstraints.RELATIVE; 
        constraints.weighty = 0.01; 
        this.add(userNamePanel, constraints);
            constraints.gridwidth = GridBagConstraints.REMAINDER; 
            constraints.gridx = 2; 
        this.add(passWordPanel, constraints);
            constraints.gridy++;
            constraints.gridx = 0; 
            constraints.anchor = GridBagConstraints.NORTH;
        this.add(login, constraints); 
            constraints.gridy++;
        this.add(errorMsg, constraints); 
        errorMsg.setVisible(false);


    }

    private boolean initTextFields(){ 
        userName = new JTextField(20); 
        passWord = new JTextField(20);
        userNameLabel = new JTextField("UserID", 20);
            userNameLabel.setBorder(javax.swing.BorderFactory.createEmptyBorder());
            userNameLabel.setEditable(false);
            userNameLabel.setHorizontalAlignment(JTextField.CENTER);
        passWordLabel = new JTextField("Password", 20); 
            passWordLabel.setBorder(javax.swing.BorderFactory.createEmptyBorder());
            passWordLabel.setEditable(false);
            passWordLabel.setHorizontalAlignment(JTextField.CENTER);
        errorMsg = new JTextField("", 50);
            errorMsg.setBorder(javax.swing.BorderFactory.createEmptyBorder());
            errorMsg.setEditable(false);
            errorMsg.setForeground(Color.red);
            passWordLabel.setHorizontalAlignment(JTextField.CENTER);

            return true; 
    }

}