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);
}
}
}