用户登录界面代码

[quote]注:这只是个登录测试参考,是练习知识点用的,
具体功能不完善;用户登录名:lianzhou 密码:admin[/quote]

package org.tarena.day03;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

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

public class UserLogin implements ActionListener {

JButton ok;

JButton back;

JTextField jtf1;

JTextField jpwd;

JFrame jf = new JFrame("用户登录");
public void creatFrame() {

// 添加组建;
JPanel jp = new JPanel(new GridLayout(3, 2));
JLabel jl1 = new JLabel("用户名");
jtf1 = new JTextField(15);
JLabel jl2 = new JLabel("密码");
jpwd = new JTextField(15);
ok = new JButton("确定");
ok.addActionListener(this);
back = new JButton("重置");
back.addActionListener(this);
jp.add(jl1);
jp.add(jtf1);
jp.add(jl2);
jp.add(jpwd);
jp.add(ok);
jp.add(back);

jf.add(jp);

// 把窗口的四个边空出来;
jf.add(BorderLayout.NORTH, new JPanel());
jf.add(BorderLayout.SOUTH, new JPanel());
jf.add(BorderLayout.EAST, new JPanel());
jf.add(BorderLayout.WEST, new JPanel());
// jf.pack();// 可自动调试窗口大小
jf.setSize(250, 150);
jf.setLocation(500, 500);
jf.setVisible(true);
jf.setResizable(false);
}

JFrame nf;

// 创建一个新的窗口;
public void newFrame(boolean flag) {
nf = new JFrame("用户界面");
JLabel jla = new JLabel();
if (flag) {
jla.setText("登录成功");
nf.add(jla);
} else {
// jla.setText("登录失败");
// nf.add(jla);
}
nf.setSize(300, 400);
nf.setLocation(400, 400);
nf.setVisible(true);
nf.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
nf.addWindowListener(new WindowListener() {

public void windowActivated(WindowEvent e) {
}

public void windowClosed(WindowEvent e) {
}

public void windowClosing(WindowEvent e) {
int i = JOptionPane.showConfirmDialog(null, "是否真的要退出界面?","用户界面",
JOptionPane.YES_NO_CANCEL_OPTION);
if(i==0){
System.exit(i);
}//根据i的返回值来判断是否退出;
}

public void windowDeactivated(WindowEvent e) {
}

public void windowDeiconified(WindowEvent e) {
}

public void windowIconified(WindowEvent e) {
}

public void windowOpened(WindowEvent e) {
}
});
}

public void actionPerformed(ActionEvent e) {
if (e.getSource() == ok) {
if ("lianzhou".equals(jtf1.getText())
&& "admin".equals(jpwd.getText())) {
jf.dispose();//隐藏登录窗口
newFrame(true);
} else {
jtf1.setText("登录失败!");
jpwd.setText("");
}
} else if (e.getSource() == back) {
jtf1.setText("");
jpwd.setText("");
}
}

public static void main(String[] args) {
UserLogin user = new UserLogin();
user.creatFrame();
}

}

你可能感兴趣的:(javacore)