⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 createlibrary.java

📁 编写一个简单的图书馆图书查询界面
💻 JAVA
字号:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import frank.simpleLibrary.*;

public class CreateLibrary extends JFrame {
	private ObjectOutputStream output;
	private GUI ui;
	private JButton openButton, enterButton;
	
	//set up GUI
	public CreateLibrary() {
		super("Creating a book list:");
		
		ui = new GUI(3); // 3 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();
				}
			}
		);
		//enterButton set up
		enterButton = ui.getDoTask2Button();
		enterButton.setText("确定");
		enterButton.setEnabled(false);
		enterButton.addActionListener(
			new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					addRecord();
				}
			}
		);
		//window set up
		addWindowListener(
			new WindowAdapter() {
				public void windowClosing(WindowEvent e) {
					if(output != null)
						closeFile();
					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
		File fn = fc.getSelectedFile();
		
		//deal with invalid filename
		if(fn == null || fn.getName().equals(""))
			JOptionPane.showMessageDialog(this, "无效的文件名", "无效的文件名", JOptionPane.ERROR_MESSAGE);
		else {
			//open file
			try {
				output = new ObjectOutputStream(new FileOutputStream(fn));
				openButton.setEnabled(false);
				enterButton.setEnabled(true);
			}
			
			//process exceptions
			catch(IOException ioE) {
				JOptionPane.showMessageDialog(this, "打开文件时发生错误", "错误", JOptionPane.ERROR_MESSAGE);
			}
		}
	}//end of openFile()
	
	//close file and terminate app
	private void closeFile() {
		//close file
		try {
			output.close();
			System.exit(0);
		}
		//process exceptions
		catch(IOException ioE) {
			JOptionPane.showMessageDialog(this, "关闭文件时发生错误", "错误", JOptionPane.ERROR_MESSAGE);
			System.exit(1);
		}
	}
	
	public void addRecord() {
		Book b;
		String fieldValues[] = ui.getFieldValues();
		try {
			b = new Book(fieldValues[GUI.TITLE], fieldValues[GUI.AUTHOR], Double.parseDouble(fieldValues[GUI.PRICE]));
			
			//output record, flush buffer
			output.writeObject(b);
			output.flush();
			//clear text fields
			ui.clearFields();
		}
		//handle format exceptions
		catch(NumberFormatException fmE) {
			JOptionPane.showMessageDialog(this, "错误的数据类型", "错误", JOptionPane.ERROR_MESSAGE);
		}
		//handle file output exceptions
		catch(IOException ioE) {
			closeFile();
		}
	} //end addRecord
	
	//execute app;
	public static void main(String[] args) {
		new CreateLibrary();
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -