📄 inquiry.java
字号:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//import frank.simpleLibrary.Book;
import frank.simpleLibrary.GUI_Inquiry;
import frank.simpleLibrary.GetResult;
public class Inquiry extends JFrame {
//private ObjectInputStream input;
private GUI_Inquiry ui;
private JButton openButton, inquireButton;
private File fn;
//set up GUI
public Inquiry() {
super("图书查询");
ui = new GUI_Inquiry(4); // 4 text fields;
getContentPane().add(ui, BorderLayout.CENTER);
//openButton set up
openButton = ui.getDoTask1Button();
openButton.setText("打开文件...");
openButton.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
openFile();
}
}
);
//inquireButton set up
inquireButton = ui.getDoTask2Button();
inquireButton.setText("查询");
inquireButton.setEnabled(false);
inquireButton.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
searchBook();
}
}
);
//window set up
addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
);
setSize(400, 150);
setVisible(true);
} //end CreateLibrary constructor
//user choose filename
private void openFile() {
//file dialog
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
int result = fc.showSaveDialog(this);
//cancel
if(result == JFileChooser.CANCEL_OPTION)
return;
//get filename
fn = fc.getSelectedFile();
//deal with invalid filename
if(fn == null || fn.getName().equals("")) {
JOptionPane.showMessageDialog(this, "无效的文件名", "无效的文件名", JOptionPane.ERROR_MESSAGE);
return;
}
inquireButton.setEnabled(true);
}//end of openFile()
public void searchBook() {
String tit, auth;
double prc_low, prc_high;
String fieldValues[] = ui.getFieldValues();
try {
tit = fieldValues[GUI_Inquiry.TITLE];
auth = fieldValues[GUI_Inquiry.AUTHOR];
prc_low = fieldValues[GUI_Inquiry.PRICE_LOW].equals("")?
0 : Double.parseDouble(fieldValues[GUI_Inquiry.PRICE_LOW]);
prc_high = fieldValues[GUI_Inquiry.PRICE_HIGH].equals("")?
0 : Double.parseDouble(fieldValues[GUI_Inquiry.PRICE_HIGH]);
//call GetResult
new GetResult(tit, auth, prc_low, prc_high, fn);
}
//handle format exceptions
catch(NumberFormatException fmE) {
JOptionPane.showMessageDialog(this, "错误的数据类型", "错误", JOptionPane.ERROR_MESSAGE);
}
} //end addRecord
//execute app;
public static void main(String[] args) {
new Inquiry();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -