怎么实现java简单的弹出用户登录窗口
展开全部
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class ATMDengLu {
JFrame jframe = null;
JLabel jlabl = null;
JLabel jlab2 = null;
JLabel jlab3 = null;
JButton jbu1 = null;
JButton jbu2 = null;
int myZhangHu = 0;
JTextField jtext1 = null;
JPasswordField jtext2 = null;
String[] person = new String[2];
String[] mimi = new String[2];
ATMDengLu()
{
myDenglu();
}
private void myDenglu()
{
jframe = new JFrame("ATM取款机");
jlabl = new JLabel("请先登录");
jlab2 = new JLabel("账号");
jlab3 = new JLabel("密码");
jbu1 = new JButton("确定");
jbu2 = new JButton("取消");
jtext1 = new JTextField();
jtext2 = new JPasswordField();
jframe.setLayout(null);
jframe.add(jlabl);
jlabl.setBounds(110,20,60,30);
jframe.add(jlab2);
jlab2.setBounds(60,50,40,25);
jframe.add(jlab3);
jlab3.setBounds(60,90,40,25);
jframe.add(jbu1);
jbu1.setBounds(80,120,70,32);
jframe.add(jbu2);
jbu2.setBounds(160,120,70,32);
jframe.add(jtext1);
jtext1.setBounds(90,50,120,25);
jframe.add(jtext2);
jtext2.setBounds(90,90,120,25);
jframe.setBounds(500,400,300,200);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setVisible(true);
myEvent();
}
private void myEvent()
{
jbu1.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
String zhanghao = jtext1.getText();
String mima = new String(jtext2.getPassword());
String string = null;
int num = 0;
int math = 0;
BufferedReader buff = null;
try {
buff = new BufferedReader(new FileReader(new File("demo.txt")));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
while((string=buff.readLine())!=null)
{
person = string.split("/");
if(person[0].equals(zhanghao))
{
myZhangHu = Integer.parseInt(person[1]);
// System.out.println(person[0]+"::"+person[1]+"::"+person[2]);
break;
}
}
}
catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
BufferedReader buff1 = null;
try {
buff1 = new BufferedReader(new FileReader(new File("Mima.txt")));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
while((string=buff1.readLine())!=null)
{
mimi = string.split("/");
if(mimi[1].equals(myZhangHu))
{
// System.out.println(person[0]+"::"+person[1]+"::"+person[2]);
break;
}
}
}
catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(person[0].equals(zhanghao)&&mimi[0].equals(mima))
{ num = 1;
jframe.dispose();
java.awt.EventQueue.invokeLater(new Runnable() {
public void run()
{
new ATMGong(myZhangHu).setVisible(true);
}
}
);
}
try {
buff1.close();
buff.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if (num == 0)
{
errDialog("账号或密码错误,请重新输入");
}
}
}
);
jbu2.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
System.exit(0);
}
});
}
public void errDialog(String str)
{
final JDialog jd = new JDialog(jframe,"错误", true);
JLabel jlabe = new JLabel(str);
JButton jbutton = new JButton("确定");
jlabe.add(jbutton);
jbutton.setBounds(110,90,60,40);
jd.add(jlabe);
jlabe.setBounds(50,30,140,28);
jd.setBounds(555,425,200,110);
jd.setVisible(true);
jd.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e3)
{
jd.dispose();
}
});
}
}
//在你的运行目录下面需要有两个文件Mima.txt 和Demo.txt
/*mima.txt文件内容格式需要:
haha/2
haha/3
haha/4
111/1
Demo.txt文件内容格式:
lisheng/1
zhangsan/2
zhaoping/3
wangbiao/4
两个文件是对应的 斜线后的数字对应一组账号密码
*/
import java.io.*;
import javax.swing.*;
public class ATMDengLu {
JFrame jframe = null;
JLabel jlabl = null;
JLabel jlab2 = null;
JLabel jlab3 = null;
JButton jbu1 = null;
JButton jbu2 = null;
int myZhangHu = 0;
JTextField jtext1 = null;
JPasswordField jtext2 = null;
String[] person = new String[2];
String[] mimi = new String[2];
ATMDengLu()
{
myDenglu();
}
private void myDenglu()
{
jframe = new JFrame("ATM取款机");
jlabl = new JLabel("请先登录");
jlab2 = new JLabel("账号");
jlab3 = new JLabel("密码");
jbu1 = new JButton("确定");
jbu2 = new JButton("取消");
jtext1 = new JTextField();
jtext2 = new JPasswordField();
jframe.setLayout(null);
jframe.add(jlabl);
jlabl.setBounds(110,20,60,30);
jframe.add(jlab2);
jlab2.setBounds(60,50,40,25);
jframe.add(jlab3);
jlab3.setBounds(60,90,40,25);
jframe.add(jbu1);
jbu1.setBounds(80,120,70,32);
jframe.add(jbu2);
jbu2.setBounds(160,120,70,32);
jframe.add(jtext1);
jtext1.setBounds(90,50,120,25);
jframe.add(jtext2);
jtext2.setBounds(90,90,120,25);
jframe.setBounds(500,400,300,200);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setVisible(true);
myEvent();
}
private void myEvent()
{
jbu1.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
String zhanghao = jtext1.getText();
String mima = new String(jtext2.getPassword());
String string = null;
int num = 0;
int math = 0;
BufferedReader buff = null;
try {
buff = new BufferedReader(new FileReader(new File("demo.txt")));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
while((string=buff.readLine())!=null)
{
person = string.split("/");
if(person[0].equals(zhanghao))
{
myZhangHu = Integer.parseInt(person[1]);
// System.out.println(person[0]+"::"+person[1]+"::"+person[2]);
break;
}
}
}
catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
BufferedReader buff1 = null;
try {
buff1 = new BufferedReader(new FileReader(new File("Mima.txt")));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
while((string=buff1.readLine())!=null)
{
mimi = string.split("/");
if(mimi[1].equals(myZhangHu))
{
// System.out.println(person[0]+"::"+person[1]+"::"+person[2]);
break;
}
}
}
catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(person[0].equals(zhanghao)&&mimi[0].equals(mima))
{ num = 1;
jframe.dispose();
java.awt.EventQueue.invokeLater(new Runnable() {
public void run()
{
new ATMGong(myZhangHu).setVisible(true);
}
}
);
}
try {
buff1.close();
buff.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if (num == 0)
{
errDialog("账号或密码错误,请重新输入");
}
}
}
);
jbu2.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
System.exit(0);
}
});
}
public void errDialog(String str)
{
final JDialog jd = new JDialog(jframe,"错误", true);
JLabel jlabe = new JLabel(str);
JButton jbutton = new JButton("确定");
jlabe.add(jbutton);
jbutton.setBounds(110,90,60,40);
jd.add(jlabe);
jlabe.setBounds(50,30,140,28);
jd.setBounds(555,425,200,110);
jd.setVisible(true);
jd.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e3)
{
jd.dispose();
}
});
}
}
//在你的运行目录下面需要有两个文件Mima.txt 和Demo.txt
/*mima.txt文件内容格式需要:
haha/2
haha/3
haha/4
111/1
Demo.txt文件内容格式:
lisheng/1
zhangsan/2
zhaoping/3
wangbiao/4
两个文件是对应的 斜线后的数字对应一组账号密码
*/
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |