设计一个登录窗口,点击“登录”按钮时,将用户输入的信息写入到磁盘中的某个路径下文件中
程序员文章站
2022-06-24 12:36:25
综合利用IO流和GUI图形用户界面知识,设计一个登录窗口,当用户在文本框中输入用户名和密码信息,点击“登录”按钮时,将用户输入的信息写入到磁盘中的某个路径下的user.txt文件中。当用户点击“显示”按钮时,将user.txt中的信息读取后放在一个文本区中显示出来。package com.test;import java.awt.BorderLayout;import java.awt.Container;import java.awt.Font;import java.awt.Image;i...
综合利用IO流和GUI图形用户界面知识,设计一个登录窗口,当用户在文本框中输入用户名和密码信息,点击“登录”按钮时,将用户输入的信息写入到磁盘中的某个路径下的user.txt文件中。当用户点击“显示”按钮时,将user.txt中的信息读取后放在一个文本区中显示出来。
package com.test;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class Exap6_4 extends JFrame {
// 窗口参数设置
public void home() {
Container c = this.getContentPane();
JTextField account = new JTextField();
JPasswordField password = new JPasswordField();
JButton record = new JButton("登录");
JButton register = new JButton("显示");
JTextField show = new JTextField("显示账号密码");
this.setTitle("窗口");
int width = Toolkit.getDefaultToolkit().getScreenSize().width;
int height = Toolkit.getDefaultToolkit().getScreenSize().height;
this.setBounds(width / 2 - 200, height / 2 - 220, 650, 650);
c.setLayout(new BorderLayout());
this.setResizable(false);
this.setVisible(true);
this.validate();
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel jpanel = new JPanel();
jpanel.setLayout(null);
JLabel Bank = new JLabel("银行");
Bank.setFont(new Font("宋体", Font.PLAIN, 30));
jpanel.add(Bank);
Bank.setBounds(280, 10, 100, 30);
c.add(jpanel, "North");
ImageIcon ii = new ImageIcon("D:\\Example6\\src\\com\\test\\1.jpg");
ii.setImage(ii.getImage().getScaledInstance(150, 150, Image.SCALE_DEFAULT));
JLabel a3 = new JLabel(ii);
a3.setBounds(240, 50, 150, 150);
c.add(a3);
JLabel a1 = new JLabel("账号:");
a1.setFont(new Font("宋体", Font.PLAIN, 20));
a1.setBounds(180, 280, 50, 20);
JLabel a2 = new JLabel("密码:");
a2.setFont(new Font("宋体", Font.PLAIN, 20));
a2.setBounds(180, 360, 50, 20);
jpanel.add(a1);
jpanel.add(a2);
account.setBounds(280, 275, 150, 30);
account.setFont(new Font("宋体", Font.PLAIN, 18));
password.setBounds(280, 355, 150, 30);
password.setFont(new Font("宋体", Font.PLAIN, 18));
jpanel.add(account);
jpanel.add(password);
c.add(jpanel, "Center");
jpanel.add(register);
register.setBounds(15, 550, 200, 40);
register.setFont(new Font("宋体", Font.PLAIN, 15));
jpanel.add(show);
show.setBounds(190, 430, 300, 75);
show.setFont(new Font("宋体", Font.PLAIN, 15));
c.add(jpanel);
jpanel.add(record);
record.setBounds(400, 550, 200, 40);
record.setFont(new Font("宋体", Font.PLAIN, 20));
c.add(jpanel);
record.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String a1 = account.getText()+"<-show password->"+password.getText();
try {
xieru(a1);
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
register.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
show.setText(duqu());
}
});
}
public void xieru(String ac) throws IOException {
File file=new File("D:\\Example6\\src\\com\\test\\user.txt");
if(!file.exists()){
file.createNewFile();
}
FileWriter wr=new FileWriter(file);
try{
wr.write(ac);
wr.flush();
}catch(IOException e){
e.printStackTrace();
}
if(wr!=null){
wr.close();
}
}
public String duqu () {
String result = "";
File file = new File("D:\\Example6\\src\\com\\test\\user.txt");
try {
InputStreamReader reader = new InputStreamReader(new FileInputStream(file),"gbk");
BufferedReader br = new BufferedReader(reader);
String s = null;
while((s=br.readLine())!=null){
result = result + s;
}
} catch (UnsupportedEncodingException | FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
public static void main(String[] args) {
Exap6_4 fuck = new Exap6_4();
fuck.home();
}
}
一个小代码,只供参考
本文地址:https://blog.csdn.net/qq_52581566/article/details/110834341
下一篇: python numpy格式化打印的实例