📄 getresult.java
字号:
package frank.simpleLibrary;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import javax.swing.*;
import frank.simpleLibrary.Book;
public class GetResult extends JFrame {
private JTextArea display;
private JButton exitButton;
private JPanel buttonPanel;
private ObjectInputStream input;
private FileInputStream fileInput;
private File fn;
private String tit, auth;
double prc_low, prc_high;
//set up GUI and execute search
public GetResult(String title, String author, double price_low, double price_high, File f) {
super("查询结果");
tit = title;
auth = author;
prc_low = price_low;
prc_high = price_high;
fn = f;
Container c = getContentPane();
//set up exit button
buttonPanel = new JPanel();
exitButton = new JButton("关闭");
exitButton.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(input != null)
closeFile();
GetResult.this.dispose();
}
}
);
buttonPanel.add(exitButton);
//set up display area
display = new JTextArea();
display.setEditable(false);
JScrollPane scroller = new JScrollPane(display);
//attach components to pane
c.add(scroller, BorderLayout.CENTER);
c.add(buttonPanel, BorderLayout.SOUTH);
//register window listener
addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent event) {
if(input != null)
closeFile();
GetResult.this.dispose();
}
}
);
pack();
setSize(600,250);
setVisible(true);
//show result;
searchResult();
}
private void openFile() {
try {
//close file from previous operation
if(input != null)
input.close();
fileInput = new FileInputStream(fn);
input = new ObjectInputStream(fileInput);
}
//process exceptions
catch(IOException ioE) {
JOptionPane.showMessageDialog(this, "打开文件错误", "错误", JOptionPane.ERROR_MESSAGE);
}
}
private void closeFile() {
try {
input.close();
}
//process exceptions
catch(IOException ioE) {
JOptionPane.showMessageDialog(this, "关闭文件错误", "错误", JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
}
private void searchResult() {
Book b;
DecimalFormat twoDigits = new DecimalFormat("0.00");
openFile();
boolean found = false;
//search books
try {
display.setText("查询条件:\n" +
"书名包含:" + tit +
"\t作者包含:" + auth +
"\t价格范围:" + twoDigits.format(prc_low) +
" 至 " + twoDigits.format(prc_high) + "\n\n");
while(true) {
//read a book record
b = (Book)input.readObject();
if(satisfied(b)){
display.append(b.getTitle() + "\t\t\t"
+ b.getAuthor() + "\t\t\t"
+ twoDigits.format(b.getPrice()) + "\n");
found = true;
}
}
}
//if end of file reached
catch(EOFException eofE) {
if(!found)
display.append("没有满足条件的图书!");
closeFile();
}
//process ClassNotFound exceptions
catch(ClassNotFoundException cnfE) {
JOptionPane.showMessageDialog(this, "读取文件时出错!", "错误", JOptionPane.ERROR_MESSAGE);
}
//process io exceptions
catch(IOException ioE) {
JOptionPane.showMessageDialog(this, "读取文件时出错!", "错误", JOptionPane.ERROR_MESSAGE);
}
} // end of searchResult
//decide if a book record satisfy the inquiry conditions
private boolean satisfied(Book b) {
boolean title_yes = false, author_yes = false, price_yes = false;
if(b.getTitle().indexOf(tit) >= 0 || tit.equals("")) title_yes = true;
if(b.getAuthor().indexOf(auth) >= 0 || auth.equals("")) author_yes = true;
if( (b.getPrice() >= prc_low && b.getPrice() <= prc_high)
|| (prc_low == 0 && prc_high == 0)) price_yes = true;
return title_yes && author_yes && price_yes;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -