系统登录界面代码实现--Java

最近在学习java语言,关于Swing编程在该例子实现的非常不错。整理完给大家参考。更加深入学习java。后面主机mainframe的实现会在另外一篇发表,还未完成。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class login extends JPanel {
static final int WIDTH = 300;
static final int HEIGHT = 150;
JFrame loginframe;

public void add(Component c, GridBagConstraints constraints, int x, int y, int w, int h){
    constraints.gridx = x;
    constraints.gridy = y;
    constraints.gridwidth = w;
    constraints.gridheight = h;
    add(c,constraints);
}
login(){
    //创建界面框架
    loginframe = new JFrame("信息管理系统");
    //窗口关闭方法
    loginframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //创建网格布局的对象
    GridBagLayout lay = new GridBagLayout();
    setLayout(lay);
    //添加当前对象到登录界面
    loginframe.add(this, BorderLayout.WEST);
    loginframe.setSize(WIDTH, HEIGHT);
    //实现居中显示的功能
    Toolkit kit = Toolkit.getDefaultToolkit();
    //获取屏幕的大小对象
    Dimension screenSize = kit.getScreenSize();
    int width = screenSize.width;
    int height = screenSize.height;
    int x = (width-WIDTH)/2;
    int y = (height - HEIGHT)/2;
    loginframe.setLocation(x, y);
    JButton ok = new JButton("登录");
    JButton cancel = new JButton("取消");

    JLabel title = new JLabel("信息系统登录窗口");
    JLabel name = new JLabel("用户名");
    JLabel password = new JLabel("密码");
    //创建用户名和密码的输入框
    final JTextField nameinput = new JTextField(15);
    final JTextField passwordinput = new JTextField(15);

    GridBagConstraints constraints = new GridBagConstraints();
    //设置对象constrains的相关属性
    constraints.fill = GridBagConstraints.NONE;
    constraints.anchor = GridBagConstraints.EAST;
    constraints.weightx = 3;
    constraints.weighty = 4;
    //使用网络组布局设置各个控件位置
    add(title, constraints, 0, 0, 4, 1);
    add(name, constraints, 0, 1, 1, 1);
    add(password, constraints, 0, 2, 1, 1);
    add(nameinput ,constraints, 2, 1, 1, 1);
    add(passwordinput, constraints, 2, 2, 1, 1);
    add(ok, constraints, 0, 3, 1, 1);
    add(cancel, constraints, 2, 3, 1, 1);
    loginframe.setResizable(false);
    loginframe.setVisible(true);
    //为“OK”按钮注册事件
    ok.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent Event) {
            //获取用户名和密码的输入框内容
            String nametext = nameinput.getText();
            String passwordtext = passwordinput.getText();
            //String str = new String(passwordtext);
            boolean x = (nametext.equals("Jason"));
            boolean y = (passwordtext.equals("123456"));
            boolean z = (x && y);
            //登录成功
            if(z = true){
            loginframe.dispose();
            mainframe main = new mainframe();
            }
            //登录失败
            else if (z == false){
                nameinput.setText("");
                passwordinput.setText("");
            }
        }
    });
//为“cancel”按钮注册事件
cancel.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent Event){
        loginframe.dispose();
        }
    });
}

}
public class studentlog{
public static void main(String[] args){
login log = new login();
}
}

你可能感兴趣的:(java)