初识I/O,利用WindowsBuilder组件画一个用户登录界面,并将登录名和密码存入文件中
一、文件写入
package com.junel.ioTest;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class testFW {
private static FileWriter fileWriter;
private static BufferedWriter bw;
private static File file = new File("/home/junel/junel/Eclipse/test/test.docx");
public static void creatFile() {
if (!file.getParentFile().exists()) {
try {
file.getParentFile().mkdirs();
file.createNewFile();
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
public static void fileWriter(String s) {
try {
fileWriter = new FileWriter(file, true);
bw = new BufferedWriter(fileWriter);
bw.write(s);
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
} finally {
try {
bw.close();
fileWriter.close();
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
}
二、界面和监听事件
package com.junel.ioTest;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Font;
import javax.swing.JTextArea;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
public class testFrame extends JFrame {
private JPanel contentPane;
private JTextField userName;
private JTextField userPassword;
/**
* Launch the application.
*/
public static void main(String[] args) {
testFW tf = new testFW();
tf.creatFile();
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
testFrame frame = new testFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public testFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 602, 436);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("用户名:");
lblNewLabel.setFont(new Font("Dialog", Font.BOLD, 20));
lblNewLabel.setBounds(126, 111, 122, 29);
contentPane.add(lblNewLabel);
JLabel label = new JLabel("密 码:");
label.setFont(new Font("Dialog", Font.BOLD, 20));
label.setBounds(126, 187, 122, 29);
contentPane.add(label);
JButton addInfo = new JButton("添加");
addInfo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
boolean flag = false;
StringBuilder s = new StringBuilder();
String userName1 = userName.getText();
String userPassword1 = userPassword.getText();
if (userName1 == null || userName1.equals("") || userPassword1.equals("") || userPassword1 == null) {
JOptionPane.showMessageDialog(null, "请输入完整", "警告", JOptionPane.PLAIN_MESSAGE);
} else {
s.append(userName1);
s.append(",").append(userPassword1);
s.append("\n");
String ss = s.toString();
testFW.fileWriter(ss);
flag = true;
}
if (flag) {
JOptionPane.showMessageDialog(null, "添加成功", "成功", JOptionPane.PLAIN_MESSAGE);
userName.setText(null);
userPassword.setText(null);
flag = false;
}
}
});
addInfo.setFont(new Font("Dialog", Font.BOLD, 22));
addInfo.setBounds(202, 260, 150, 46);
contentPane.add(addInfo);
userName = new JTextField();
userName.setBounds(241, 113, 193, 29);
contentPane.add(userName);
userName.setColumns(10);
userPassword = new JTextField();
userPassword.setColumns(10);
userPassword.setBounds(241, 187, 193, 29);
contentPane.add(userPassword);
}
}