Java-项目-003-图书管理系统(界面利用swing,存储利用txt-2020-6-25
目录
零、最终效果
一、MainActivity.java
/*
*1.图书信息管理程序
*2.图形用户界面
*3.数据以文件形式进行保存,可通过文件存入数据,使用文件txt进行存储;
*4.具有基本的信息维护功能,包括信息的增、删、改、查、文件导入操作
*5.通过BufferedReader、FileReader、FileWriter、FileInputStream、InputStreamReader进行文件操作
*6.通过StringBuffer对字符串进行操作
*7.通过正则表达式获取需要的字符串, Book (书名=[ name], 简介=[introduce])
*
*/
public class MainActivity {
public static void main(String[] args) {
LoginActivity w1 = new LoginActivity();
}
}
二、LoginActivity.java
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class LoginActivity extends JFrame implements ActionListener {
static LoginActivity w1;
JLabel jL1, jL2, jLname;
JButton jB1, jB2;
JTextField jF1;
JPasswordField jP1;
public LoginActivity() {
super("图书信息管理程序_登录界面");
jLname = new JLabel("");
jLname.setFont(new Font("宋体", Font.BOLD, 20));
jLname.setBounds(5, 5, 200, 30);
jL1 = new JLabel("用户名称:");
jL1.setFont(new Font("宋体", Font.BOLD, 32));
jL1.setBounds(60, 40, 200, 30);
jL2 = new JLabel("登录密码:");
jL2.setFont(new Font("宋体", Font.BOLD, 32));
jL2.setBounds(60, 120, 200, 30);
jF1 = new JTextField("", 20);
jF1.setBounds(250, 40, 200, 30);
jP1 = new JPasswordField("", 20);
jP1.setBounds(250, 120, 200, 30);
jB1 = new JButton("登录");
jB1.setBounds(60, 170, 60, 30);
jB1.addActionListener(this);
jB2 = new JButton("退出");
jB2.setBounds(250, 170, 60, 30);
jB2.addActionListener(this);
Container winContainer = this.getContentPane();
winContainer.setLayout(null);
winContainer.add(jLname);
winContainer.add(jL1);
winContainer.add(jL2);
winContainer.add(jF1);
winContainer.add(jP1);
winContainer.add(jB1);
winContainer.add(jB2);
this.setSize(1100, 280);
this.setLocation(500, 500);
this.setVisible(true);
}
public static void main(String[] args) {
LoginActivity w1 = new LoginActivity();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == jB1) {
if (jF1.getText().toString() != null && jF1.getText().toString().length() > 0) {
if(new String(jP1.getPassword()).equals(""))
{
JOptionPane.showMessageDialog(w1, "登录密码不为空", "提示", 2);
}
else {
if (jF1.getText().equals("cloud") && new String(jP1.getPassword()).equals("1")) // ※
{
JOptionPane.showMessageDialog(w1, "登录成功", "提示", 1);
jL1.setText("");
jL2.setText("");
jF1.setText("");
this.setVisible(false);
IndexActivity w2 = new IndexActivity();
} else {
JOptionPane.showMessageDialog(w1, "登录失败", "提示", 2);
}
}
} else {
JOptionPane.showMessageDialog(w1, "用户名称不为空", "提示", 2);
}
} else if (e.getSource() == jB2)
System.exit(0);
}
}
三、IndexActivity.java
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class IndexActivity extends JFrame implements ActionListener {
JLabel jL1, jL2, jLname, jL3;
JButton jB1, jB2, jB3, jB4, jB5, jB6, SignOut;
JTextField jF1;
JPasswordField jP1;
ImageIcon background;
JPanel myPanel;
public IndexActivity() {
super("图书信息管理程序_操作主界面");
jLname = new JLabel("用户名称:cloud");
jLname.setFont(new Font("宋体", Font.BOLD, 16));
jLname.setBounds(950, 5, 200, 30);
jL1 = new JLabel("操作:");
jL1.setFont(new Font("宋体", Font.BOLD, 20));
jL1.setBounds(20, 40, 200, 30);
jB1 = new JButton("添加图书信息");
jB1.setBounds(100, 40, 150, 30);
jB1.addActionListener(this);
jB2 = new JButton("删除图书信息");
jB2.setBounds(100, 85, 150, 30);
jB2.addActionListener(this);
jB3 = new JButton("修改图书信息");
jB3.setBounds(100, 130, 150, 30);
jB3.addActionListener(this);
jB4 = new JButton("查询图书信息");
jB4.setBounds(100, 175, 150, 30);
jB4.addActionListener(this);
SignOut = new JButton("退出登录");
SignOut.setBounds(820, 5, 110, 30);
SignOut.addActionListener(this);
background = new ImageIcon("e:\\\\图片/图标/cloud.png"); // 创建一个背景图片
jL3 = new JLabel(background); // 把背景图片添加到标签里
jL3.setBounds(950, 35, background.getIconWidth(), background.getIconHeight()); // 把标签设置为和图片等高等宽
myPanel = (JPanel) this.getContentPane(); // 把我的面板设置为内容面板
myPanel.setOpaque(false); // 把我的面板设置为不可视
myPanel.setLayout(new FlowLayout()); // 把我的面板设置为流动布局
this.getLayeredPane().setLayout(null); // 把分层面板的布局置空
this.getLayeredPane().add(jL3, new Integer(Integer.MIN_VALUE));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container winContainer = this.getContentPane();
winContainer.setLayout(null);
winContainer.add(jLname);
winContainer.add(jL1);
winContainer.add(jB1);
winContainer.add(jB2);
winContainer.add(jB3);
winContainer.add(jB4);
winContainer.add(SignOut);
this.setSize(1100, 280);
this.setLocation(500, 500);
this.setVisible(true);
}
public static void main(String[] args) {
IndexActivity w2 = new IndexActivity();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == jB1) {
this.setVisible(false);
// 添加信息
AddInfoActivity w3 = new AddInfoActivity();
}
if (e.getSource() == jB2) {
this.setVisible(false);
// 删除信息
DeleteInfoActivity w4 = new DeleteInfoActivity();
}
if (e.getSource() == jB3) {
this.setVisible(false);
// 修改信息
ReviseInfoActivity w5 = new ReviseInfoActivity();
}
if (e.getSource() == jB4) {
this.setVisible(false);
// 查询信息
SearchInfoActivity w6 = new SearchInfoActivity();
}
if (e.getSource() == SignOut) {
this.setVisible(false);
// 登录界面
LoginActivity w1 = new LoginActivity();
}
}
}
四、AddInfoActivity.java
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
public class AddInfoActivity extends JFrame implements ActionListener {
static IndexActivity w2;
JLabel jL1, jL2, jLname;
JButton jB1, jB2;
JTextField jF1, jF2;
JMenuItem openMenuItem;
JMenu fileMenu;
JMenuBar mbar;// 定义菜单
JFileChooser jFc;
File file;
FileReader fileread;
BufferedReader bufread;
String readStr = "";
JScrollPane scrollPane;
JTextArea textArea;
public AddInfoActivity() {
super("图书信息管理程序_添加图书信息");
jLname = new JLabel("用户名称:cloud");
jLname.setFont(new Font("宋体", Font.BOLD, 16));
jLname.setBounds(950, 5, 200, 30);
jL1 = new JLabel("添加的书名:");
jL1.setFont(new Font("宋体", Font.BOLD, 20));
jL1.setBounds(30, 60, 200, 30);
jL2 = new JLabel("添加的简介:");
jL2.setFont(new Font("宋体", Font.BOLD, 20));
jL2.setBounds(30, 120, 200, 30);
jF1 = new JTextField("", 20);
jF1.setBounds(170, 60, 200, 30);
jF2 = new JTextField("", 20);
jF2.setBounds(170, 120, 200, 30);
jB1 = new JButton("添加");
jB1.setBounds(60, 170, 60, 30);
jB1.addActionListener(this);
jB2 = new JButton("返回");
jB2.setBounds(250, 170, 60, 30);
jB2.addActionListener(this);
jFc = new JFileChooser();
jFc.setCurrentDirectory(new File("e:/桌面/考试"));
jFc.setDialogTitle("请选择文件");
openMenuItem = new JMenuItem("导入");
openMenuItem.addActionListener(this);
fileMenu = new JMenu("文件");
mbar = new JMenuBar();
fileMenu.add(openMenuItem);
mbar.add(fileMenu);
textArea = new JTextArea();
textArea.setLineWrap(true); // 自动换行
textArea.setFont(new Font("sssssssss", Font.PLAIN, 18)); // 设置字体
// 创建滚动面板, 指定滚动显示的视图组件(textArea), 垂直滚动条一直显示, 水平滚动条从不显示
scrollPane = new JScrollPane(textArea, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setBounds(400, 50, 660, 130);
this.setJMenuBar(mbar);
Container winContainer = this.getContentPane();
winContainer.setLayout(null);
winContainer.add(jLname);
winContainer.add(jL1);
winContainer.add(jL2);
winContainer.add(jF1);
winContainer.add(jF2);
winContainer.add(jB1);
winContainer.add(jB2);
winContainer.add(scrollPane);
this.setSize(1100, 280);
this.setLocation(500, 500);
this.setVisible(true);
try {
SQLTxt.searchAllTxtFile();
textArea.setText(SQLTxt.getreadStr());
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
AddInfoActivity w3 = new AddInfoActivity();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == jB1) {
if (jF1.getText().toString() != null && jF1.getText().toString().length() > 0) {
if (jF2.getText().toString() != null && jF2.getText().toString().length() > 0) {
Book book = new Book(jF1.getText().toString(), jF2.getText().toString());
try {
SQLTxt.writeSQL(book.toString());
SQLTxt.searchAllTxtFile();
textArea.setText(SQLTxt.getreadStr());
JOptionPane.showMessageDialog(w2, "添加成功", "提示", 1);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
JOptionPane.showMessageDialog(w2, "添加失败", "提示", 2);
}
} else {
JOptionPane.showMessageDialog(w2, "简介不为空", "提示", 1);
}
} else {
JOptionPane.showMessageDialog(w2, "书名不为空", "提示", 1);
}
} else if (e.getSource() == jB2) {
this.setVisible(false);
w2 = new IndexActivity();
} else
readStr = "";
if (e.getSource() == openMenuItem) {
int returnVal = jFc.showOpenDialog(this);
if (returnVal == 0) {
file = jFc.getSelectedFile();
try {
SQLTxt.readTxtFile(file.getPath());
//System.out.println(SQLTxt.getreadStr());
try {
SQLTxt.writeTxtFile(SQLTxt.getreadStr());
SQLTxt.searchAllTxtFile();
textArea.setText(SQLTxt.getreadStr());
JOptionPane.showMessageDialog(w2, "添加成功", "提示", 1);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} else {
//JOptionPane.showMessageDialog(w2, "你没有选择文件", "提示", 1);
}
}
}
}
五、DeleteInfoActivity.java
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
public class DeleteInfoActivity extends JFrame implements ActionListener {
static IndexActivity w2;
JLabel jL1, jL2, jLname;
JButton jB1, jB2, jB3;
JTextField jF1, jF2;
JScrollPane scrollPane;
JTextArea textArea;
public DeleteInfoActivity() {
super("图书信息管理程序_删除图书信息");
jLname = new JLabel("用户名称:cloud");
jLname.setFont(new Font("宋体", Font.BOLD, 16));
jLname.setBounds(950, 5, 200, 30);
jL1 = new JLabel("删除的书名:");
jL1.setFont(new Font("宋体", Font.BOLD, 20));
jL1.setBounds(30, 60, 200, 30);
jL2 = new JLabel("删除的简介:");
jL2.setFont(new Font("宋体", Font.BOLD, 20));
jL2.setBounds(30, 120, 200, 30);
jF1 = new JTextField("", 20);
jF1.setBounds(170, 60, 200, 30);
jF2 = new JTextField("", 20);
jF2.setBounds(170, 120, 200, 30);
jB1 = new JButton("删除");
jB1.setBounds(60, 170, 60, 30);
jB1.addActionListener(this);
jB2 = new JButton("返回");
jB2.setBounds(280, 170, 60, 30);
jB2.addActionListener(this);
jB3 = new JButton("删除全部");
jB3.setBounds(150, 170, 90, 30);
jB3.addActionListener(this);
textArea = new JTextArea();
textArea.setLineWrap(true); // 自动换行
textArea.setFont(new Font("sssssssss", Font.PLAIN, 18)); // 设置字体
// 创建滚动面板, 指定滚动显示的视图组件(textArea), 垂直滚动条一直显示, 水平滚动条从不显示
scrollPane = new JScrollPane(textArea, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setBounds(400, 50, 660, 160);
Container winContainer = this.getContentPane();
winContainer.setLayout(null);
winContainer.add(jLname);
winContainer.add(jL1);
winContainer.add(jL2);
winContainer.add(jF1);
winContainer.add(jF2);
winContainer.add(jB1);
winContainer.add(jB2);
winContainer.add(jB3);
winContainer.add(scrollPane);
this.setSize(1100, 280);
this.setLocation(500, 500);
this.setVisible(true);
try {
SQLTxt.searchAllTxtFile();
textArea.setText(SQLTxt.getreadStr());
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
DeleteInfoActivity w4 = new DeleteInfoActivity();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == jB1) {
if (jF1.getText().toString() != null && jF1.getText().toString().length() > 0) {
if (jF2.getText().toString() != null && jF2.getText().toString().length() > 0) {
Book book = new Book(jF1.getText().toString(), jF2.getText().toString());
SQLTxt.deleteTxtByStr(book.toString());
try {
SQLTxt.searchAllTxtFile();
textArea.setText(SQLTxt.getreadStr());
JOptionPane.showMessageDialog(w2, "删除成功", "提示", 1);
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} else {
JOptionPane.showMessageDialog(w2, "删除的简介不为空", "提示", 1);
}
} else {
JOptionPane.showMessageDialog(w2, "删除的书名不为空", "提示", 1);
}
} else if (e.getSource() == jB2) {
this.setVisible(false);
w2 = new IndexActivity();
} else if (e.getSource() == jB3) {
try {
SQLTxt.deleteAllTxtFile();
try {
SQLTxt.searchAllTxtFile();
textArea.setText(SQLTxt.getreadStr());
JOptionPane.showMessageDialog(w2, "删除成功", "提示", 1);
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
六、ReviseInfoActivity.java
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
public class ReviseInfoActivity extends JFrame implements ActionListener {
static IndexActivity w2;
JLabel jL1, jL2, jL3, jLname;
JButton jB1, jB2;
JTextField jF1, jF2, jF3, jF4, jF;
JScrollPane scrollPane;
JTextArea textArea;
public ReviseInfoActivity() {
super("图书信息管理程序_修改图书信息");
jLname = new JLabel("用户名称:cloud");
jLname.setFont(new Font("宋体", Font.BOLD, 16));
jLname.setBounds(950, 5, 200, 30);
jL1 = new JLabel("修改前的信息:");
jL1.setFont(new Font("宋体", Font.BOLD, 20));
jL1.setBounds(20, 15, 200, 30);
jL2 = new JLabel("修改后的信息:");
jL2.setFont(new Font("宋体", Font.BOLD, 20));
jL2.setBounds(20, 90, 200, 30);
jF1 = new JTextField("", 20);
jF1.setBounds(160, 15, 200, 30);
jF2 = new JTextField("", 20);
jF2.setBounds(160, 90, 200, 30);
jF3 = new JTextField("", 20);
jF3.setBounds(160, 50, 200, 30);
jF4 = new JTextField("", 20);
jF4.setBounds(160, 125, 200, 30);
jF = new JTextField("", 20);
jB1 = new JButton("修改");
jB1.setBounds(60, 180, 60, 30);
jB1.addActionListener(this);
jB2 = new JButton("返回");
jB2.setBounds(250, 180, 60, 30);
jF1.addFocusListener(new JTextFieldHintListener(jF1, "书名"));
jF2.addFocusListener(new JTextFieldHintListener(jF2, "书名"));
jF3.addFocusListener(new JTextFieldHintListener(jF3, "简介"));
jF4.addFocusListener(new JTextFieldHintListener(jF4, "简介"));
jB2.addActionListener(this);
textArea = new JTextArea();
textArea.setLineWrap(true); // 自动换行
textArea.setFont(new Font("sssssssss", Font.PLAIN, 18)); // 设置字体
// 创建滚动面板, 指定滚动显示的视图组件(textArea), 垂直滚动条一直显示, 水平滚动条从不显示
scrollPane = new JScrollPane(textArea, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setBounds(400, 50, 660, 160);
Container winContainer = this.getContentPane();
winContainer.setLayout(null);
winContainer.add(jLname);
winContainer.add(jL1);
winContainer.add(jL2);
winContainer.add(jF1);
winContainer.add(jF2);
winContainer.add(jF3);
winContainer.add(jF4);
winContainer.add(jF);
winContainer.add(jB1);
winContainer.add(jB2);
winContainer.add(scrollPane);
this.setSize(1100, 280);
this.setLocation(500, 500);
this.setVisible(true);
jF.requestFocus();
try {
SQLTxt.searchAllTxtFile();
textArea.setText(SQLTxt.getreadStr());
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
ReviseInfoActivity w5 = new ReviseInfoActivity();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == jB1) {
Book oldbook = new Book(jF1.getText().toString(), jF3.getText().toString());
Book replacebook = new Book(jF2.getText().toString(), jF4.getText().toString());
SQLTxt.replaceTxtByStr(oldbook.toString(), replacebook.toString());
try {
SQLTxt.searchAllTxtFile();
textArea.setText(SQLTxt.getreadStr());
JOptionPane.showMessageDialog(w2, "修改成功", "提示", 1);
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} else if (e.getSource() == jB2) {
this.setVisible(false);
w2 = new IndexActivity();
}
}
}
七、SearchInfoActivity.java
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
public class SearchInfoActivity extends JFrame implements ActionListener {
static IndexActivity w2;
JLabel jL1, jL2, jLname;
JButton jB1, jB2;
JTextField jF1, jF;
JScrollPane scrollPane;
JTextArea textArea;
public SearchInfoActivity() {
super("图书信息管理程序_查询图书信息");
jLname = new JLabel("用户名称:cloud");
jLname.setFont(new Font("宋体", Font.BOLD, 16));
jLname.setBounds(950, 5, 200, 30);
jL1 = new JLabel("信息:");
jL1.setFont(new Font("宋体", Font.BOLD, 30));
jL1.setBounds(20, 60, 200, 30);
jF1 = new JTextField("", 20);
jF1.setBounds(110, 60, 280, 30);
jF1.setFont(new Font("宋体", Font.BOLD, 15));
jF1.addFocusListener(new JTextFieldHintListener(jF1, "书名/简介(输入search.all查询全部)"));
jF = new JTextField("", 20);
jB1 = new JButton("查询");
jB1.setBounds(60, 170, 60, 30);
jB1.addActionListener(this);
jB2 = new JButton("返回");
jB2.setBounds(250, 170, 60, 30);
jB2.addActionListener(this);
// 创建文本区域组件
textArea = new JTextArea();
textArea.setLineWrap(true); // 自动换行
textArea.setFont(new Font("sssssssss", Font.PLAIN, 18)); // 设置字体
// 创建滚动面板, 指定滚动显示的视图组件(textArea), 垂直滚动条一直显示, 水平滚动条从不显示
scrollPane = new JScrollPane(textArea, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setBounds(400, 50, 660, 160);
Container winContainer = this.getContentPane();
winContainer.setLayout(null);
winContainer.add(jLname);
winContainer.add(jL1);
winContainer.add(jF1);
winContainer.add(jF);
winContainer.add(jB1);
winContainer.add(jB2);
winContainer.add(scrollPane);
this.setSize(1100, 280);
this.setLocation(500, 500);
this.setVisible(true);
jF.requestFocus();
try {
SQLTxt.searchAllTxtFile();
textArea.setText(SQLTxt.getreadStr());
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
SearchInfoActivity w6 = new SearchInfoActivity();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == jB1) {
if (jF1.getText().toString() != null && jF1.getText().toString().length() > 0) {
if (jF1.getText().toString().equals("search.all")) {
try {
SQLTxt.searchAllTxtFile();
textArea.setText(SQLTxt.getreadStr());
JOptionPane.showMessageDialog(w2, "查询成功", "提示", 1);
// System.out.println(SQLTxt.getreadStr());
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(w2, "查询失败", "提示", 1);
e1.printStackTrace();
}
} else {
try {
SQLTxt.searchTxtFile(jF1.getText().toString());
textArea.setText(SQLTxt.getreadStr());
JOptionPane.showMessageDialog(w2, "查询成功", "提示", 1);
// System.out.println(SQLTxt.getreadStr());
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(w2, "查询失败", "提示", 1);
e1.printStackTrace();
}
}
} else {
JOptionPane.showMessageDialog(w2, "输入不为空", "提示", 1);
}
} else if (e.getSource() == jB2) {
this.setVisible(false);
w2 = new IndexActivity();
}
}
}
八、Book.java
import java.io.Serializable;
public class Book implements Serializable{
String name;
String introduce;
public Book(String name, String introduce) {
super();
this.name = name;
this.introduce = introduce;
}
public String toString() {
return "Book (书名=[" + name + "], 简介=[" + introduce + "])";
}
}
九、SQLTxt.java
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class SQLTxt {
public static BufferedReader bufread;
// 存储的文件路径和名称
private static String path = "e:/桌面/考试/BookInfo.txt";
private static File filename = new File(path);
private static String readStr = "";
/**
* 1.添加内容.
*
* @param addStr 添加内容
*/
public static void writeSQL(String addStr) throws IOException
{
FileWriter out;
out = new FileWriter(path, true);
out.write(addStr);
// 换行
out.write("\r\n");
// 继续追加
// 刷新IO内存流
out.flush();
// 关闭
out.close();
}
/**
* 2.删除内容.
*
* @param deleteStr 删除内容
*
*/
public static void deleteTxtByStr(String deleteStr) {
String temp = "";
try {
File file = new File(path);
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
StringBuffer buf = new StringBuffer();
//System.out.println(deleteStr);
// 保存该行前面的内容
for (int j = 1; (temp = br.readLine()) != null && !temp.equals(deleteStr); j++) {
buf = buf.append(temp);
buf = buf.append(System.getProperty("line.separator"));
}
// 保存该行后面的内容
while ((temp = br.readLine()) != null) {
buf = buf.append(temp);
buf = buf.append(System.getProperty("line.separator"));
}
br.close();
FileOutputStream fos = new FileOutputStream(file);
PrintWriter pw = new PrintWriter(fos);
pw.write(buf.toString().toCharArray());
pw.flush();
pw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 3.将文件中指定内容的第一行替换为其它内容.
*
* @param oldStr 查找内容
* @param replaceStr 替换内容
*/
public static void replaceTxtByStr(String oldStr, String replaceStr) {
String temp = "";
try {
File file = new File(path);
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
StringBuffer buf = new StringBuffer();
// 保存该行前面的内容
for (int j = 1; (temp = br.readLine()) != null && !temp.equals(oldStr); j++) {
buf = buf.append(temp);
buf = buf.append(System.getProperty("line.separator"));
}
// 将内容插入
buf = buf.append(replaceStr);
// 保存该行后面的内容
while ((temp = br.readLine()) != null) {
buf = buf.append(System.getProperty("line.separator"));
buf = buf.append(temp);
}
buf=buf.append("\n");
br.close();
FileOutputStream fos = new FileOutputStream(file);
PrintWriter pw = new PrintWriter(fos);
pw.write(buf.toString().toCharArray());
pw.flush();
pw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 4.查找文本文件.
*
* @throws UnsupportedEncodingException
*
*/
public static void searchTxtFile(String searchStr) throws UnsupportedEncodingException {
String read;
FileReader fileread;
readStr = "";
String math = "";
String rmath = "";
// String m="";
try {
fileread = new FileReader(filename);
// InputStreamReader isr = new InputStreamReader(new FileInputStream(filename),
// "UTF-8");
bufread = new BufferedReader(fileread);
try {
while ((read = bufread.readLine()) != null) {
System.out.println(read);
math = "";
String regex = "\\[(.*?)]";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(read);
while (matcher.find()) {
// System.out.println(matcher.group());
math = math + matcher.group();
}
int result1 = math.indexOf(searchStr);
// System.out.println(math+"-"+searchStr);
if (result1 != -1) {
Pattern pattern1 = Pattern.compile("(?<=\\()[^\\)]+");
Matcher matcher1 = pattern1.matcher(read);
while (matcher1.find()) {
// System.out.println(matcher1.group());
rmath = matcher1.group();
}
readStr = readStr + rmath + "\n";
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 5.获取文本文件.
*
* @throws UnsupportedEncodingException
* @param file 打开的文件
*/
public static void readTxtFile(String filepath) throws UnsupportedEncodingException {
String read;
FileReader fileread;
readStr = "";
File file = new File(filepath);
try {
fileread = new FileReader(file);
bufread = new BufferedReader(fileread);
try {
while ((read = bufread.readLine()) != null) {
System.out.println(read);
readStr = readStr + read + "\n";
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 6.添加到文件.
*
* @param addStr 添加内容
*/
public static void writeTxtFile(String addStr) throws IOException
{
FileWriter out;
out = new FileWriter(path, true);
// out.write('\n');
out.write(addStr);
out.flush();
// 关闭
out.close();
}
/**
* 7.获取全部文本.
*
* @throws UnsupportedEncodingException
* @param file 打开的文件
*/
public static void searchAllTxtFile() throws UnsupportedEncodingException {
String read;
FileReader fileread;
readStr = "";
String rmath = "";
try {
fileread = new FileReader(filename);
bufread = new BufferedReader(fileread);
try {
while ((read = bufread.readLine()) != null) {
// System.out.println(read);
Pattern pattern1 = Pattern.compile("(?<=\\()[^\\)]+");
Matcher matcher1 = pattern1.matcher(read);
while (matcher1.find()) {
// System.out.println(matcher1.group());
rmath = matcher1.group();
}
readStr = readStr + rmath + "\n";
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 8.删除全部文本.
*
* @throws UnsupportedEncodingException
*/
public static void deleteAllTxtFile() throws UnsupportedEncodingException {
String temp = "";
try {
File file = new File(path);
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
StringBuffer buf = new StringBuffer();
buf = buf.append("");
br.close();
FileOutputStream fos = new FileOutputStream(file);
PrintWriter pw = new PrintWriter(fos);
pw.write(buf.toString().toCharArray());
pw.flush();
pw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static String getreadStr() {
return readStr;
}
}
十、JTextFieldHintListener.java
import java.awt.Color;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JTextField;
public class JTextFieldHintListener implements FocusListener {
private String hintText;
private JTextField textField;
public JTextFieldHintListener(JTextField jTextField,String hintText) {
this.textField = jTextField;
this.hintText = hintText;
jTextField.setText(hintText); //默认直接显示
jTextField.setForeground(Color.GRAY);
}
@Override
public void focusGained(FocusEvent e) {
//获取焦点时,清空提示内容
String temp = textField.getText();
if(temp.equals(hintText)) {
textField.setText("");
textField.setForeground(Color.BLACK);
}
}
@Override
public void focusLost(FocusEvent e) {
//失去焦点时,没有输入内容,显示提示内容
String temp = textField.getText();
if(temp.equals("")) {
textField.setForeground(Color.GRAY);
textField.setText(hintText);
}
}
}