用java创建窗口

发布网友 发布时间:2022-04-24 15:29

我来回答

2个回答

热心网友 时间:2023-10-19 09:08

我试一下硬盘文件存储吧,首先在C盘根目录下创建个login.swing的文件,在里面写上 tom##123&&lydia##123 ,这个为了方便测试,自己试下吧,我也是没学多久,如果有太2的地方,请联系我...谢谢...;

import java.awt.*;
import javax.swing.*;
import java.io.*;
public class LoginTest implements ActionListener{
private JFrame jf ;
private JLabel l1,l2 ;
private JTextField tf1 ;
private JPasswordField tf2;
private JPanel northPanel,centerPanel ;
private JButton b1,b2 ;
private File file = new File("c:/login.swing");

public LoginTest() {
jf = new JFrame("My First WindowTest") ;
northPanel = new JPanel(new GridLayout(2,2,10,10)) ;
l1 = new JLabel("用户名:") ;
tf1 = new JTextField() ;
l2 = new JLabel("密 码:") ;
tf2 = new JPasswordField() ;
northPanel.add(l1);
northPanel.add(tf1);
northPanel.add(l2);
northPanel.add(tf2);

centerPanel = new JPanel();
b1 = new JButton("login");
b2 = new JButton("exit");
centerPanel.add(b1);
centerPanel.add(b2);

b1.addActionListener(this);
b2.addActionListener(this);

jf.add(northPanel);
jf.add(centerPanel,"South");
jf.setSize(200,130);

Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
jf.setLocation(size.width / 2 - jf.getWidth() / 2, size.height / 2 - jf.getHeight() / 2);

jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}

public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(b1)) {
String username = tf1.getText() ;
String password = String.valueOf(tf2.getPassword());
BufferedReader br = null ;
try {
FileReader fr = new FileReader(file);
br = new BufferedReader(fr);
String line = "",str = "" ;
while((line = br.readLine()) != null) {
str += line ;
}
String[] users = str.split("&&");
for(String user : users) {
String[] userInfo = user.split("##");
if(userInfo[0].equals(username) && userInfo[1].equals(password)) {
JOptionPane.showMessageDialog(null, "登录成功!") ;
return ;
}
}
JOptionPane.showMessageDialog(null, "用户名或密码错误!") ;
return ;
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} else {
System.exit(0);
}
}

public static void main(String args[]) {
new LoginTest();
}
}

热心网友 时间:2023-10-19 09:08

我这有一个。给我邮箱,我发给你,你参考下。追问邮箱是:43349@qq.com

追答好久没用了,发给你的,是以前我上课做的。

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com