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

📄 addressbookgui.java

📁 iCarnegie SSD3 exam3
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
import java.util.zip.DataFormatException;

/**
 * Displays the information in an address book.
 * 
 * @author Fernando Irigoyen Farias
 * @version 1.0.0
 */
public class AddressBookGUI extends JPanel {

	/* Delimiter */
	private final String DELIM = "_";

	/* Window width in pixels */
	static private int WIDTH = 450;

	/* Window height in pixels */
	static private int HEIGHT = 300;

	private JButton openButton;

	private JButton displayButton;

	private JLabel numberOfEntriesLabel;

	private JTextField fileNameTextField;

	private JTextField nameTextField;

	private JTextField addressTextField;

	private JTextField telephoneTextField;

	private JList namesList;

	private JTextArea statusTextArea;

	private AddressBook addressBook;

	/**
	 * Creates a window.
	 * 
	 * @param args
	 *            not used.
	 */
	public static void main(String[] args) {

		JFrame frame = new JFrame("Address Book");

		frame.setContentPane(new AddressBookGUI());
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(WIDTH, HEIGHT);
		frame.setVisible(true);
	}

	/**
	 * Creates a graphical user interface.
	 */
	public AddressBookGUI() {

		setBackground(Color.white);

		// Create the components
		openButton = new JButton("Open File");
		displayButton = new JButton("Display");
		numberOfEntriesLabel = new JLabel("0 entries");
		fileNameTextField = new JTextField("", 17);
		nameTextField = new JTextField("", 17);
		addressTextField = new JTextField("", 17);
		telephoneTextField = new JTextField("", 17);
		namesList = new JList();
		statusTextArea = new JTextArea(4, 40);

		// Customize the components
		namesList.setVisibleRowCount(8);
		namesList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		namesList.setFixedCellHeight(16);
		namesList.setFixedCellWidth(130);
		numberOfEntriesLabel.setHorizontalAlignment(JLabel.CENTER);
		nameTextField.setEditable(false);
		addressTextField.setEditable(false);
		telephoneTextField.setEditable(false);
		statusTextArea.setEditable(false);

		// Register the listeners for the buttons
		openButton.addActionListener(new OpenButtonListener());
		displayButton.addActionListener(new DisplayButtonListener());

		// North panel
		JPanel northPanel = new JPanel();

		northPanel.add(new JLabel("File name: "));
		northPanel.add(fileNameTextField);
		northPanel.add(openButton);

		// West panel
		JPanel westPanel = new JPanel(new BorderLayout());
		westPanel.add(numberOfEntriesLabel, BorderLayout.CENTER);
		westPanel.add(new JScrollPane(namesList), BorderLayout.NORTH);
		westPanel.add(displayButton, BorderLayout.SOUTH);

		// Center panel
		JPanel centerPanel = new JPanel();
		JPanel infoPanel = new JPanel(new BorderLayout());
		GridLayout layout = new GridLayout(3, 1);
		JPanel labelsPanel = new JPanel(layout);
		JPanel valuesPanel = new JPanel(layout);

		layout.setVgap(10);
		labelsPanel.add(new JLabel("Name: "));
		labelsPanel.add(new JLabel("Address: "));
		labelsPanel.add(new JLabel("Telephone: "));
		valuesPanel.add(nameTextField);
		valuesPanel.add(addressTextField);
		valuesPanel.add(telephoneTextField);
		infoPanel.add(labelsPanel, BorderLayout.WEST);
		infoPanel.add(valuesPanel, BorderLayout.CENTER);
		centerPanel.add(infoPanel);

		// Arrange panels in window
		setLayout(new BorderLayout());
		add(northPanel, BorderLayout.NORTH);
		add(westPanel, BorderLayout.WEST);
		add(centerPanel, BorderLayout.CENTER);
		add(statusTextArea, BorderLayout.SOUTH);

		addressBook = new AddressBook();
	}

	/**
	 * This inner class handles <code>openButton</code> events.
	 */
	class OpenButtonListener implements ActionListener {

		/**
		 * Loads address book data from the specified file and displays the
		 * names in a list. Also displays, beneath the list, the number2 of
		 * names in the address book.
		 * 
		 * @param event
		 *            the event object.
		 */
		public void actionPerformed(ActionEvent event) {
			
			StringTokenizer tokenizer;
			String datos;
			
			namesList.removeAll();
			statusTextArea.setText("");			
			nameTextField.setText("");
			telephoneTextField.setText("");
			addressTextField.setText("");
			
			try {
				BufferedReader archivo = new BufferedReader(new FileReader(
						fileNameTextField.getText()));

				while ((datos = archivo.readLine()) != null) {
					
					tokenizer = new StringTokenizer(datos, "_");
					
					if (tokenizer.countTokens() != 3) {
						throw new DataFormatException();
					}
					
					addressBook.addEntry(new AddressBookEntry(tokenizer
							.nextToken(), tokenizer.nextToken(), tokenizer
							.nextToken()));
				}

			} catch (FileNotFoundException fnfe) {
				statusTextArea.setText("El archivo no existe");
			} catch (IOException io) {
				statusTextArea.setText("Error al leer el archivo");
			} catch (DataFormatException dfe) {
				statusTextArea.setText("Error de formato");
			}

			namesList.setListData(addressBook.getNames());
			numberOfEntriesLabel.setText("" + addressBook.getNumberOfEntries());
		}
	}

	/**
	 * This inner class handles <code>displayButton</code> events.
	 */
	class DisplayButtonListener implements ActionListener {

		/**
		 * Displays the address book entry for the selected person.
		 * 
		 * @param event
		 *            the event object.
		 */
		public void actionPerformed(ActionEvent event) {
			
			String nombre;

			if (addressBook.getNumberOfEntries() == 0) {
				statusTextArea.setText("La libreta de direcciones esta vacia");
			} else if 
					((nombre = (String) namesList.getSelectedValue()) == null) {
				statusTextArea.setText("No ha seleccionado un nombre");
			} else {
				AddressBookEntry addressBookEntry = addressBook
						.getEntry(nombre);

				statusTextArea.setText("Se ha mostrado: " + nombre);
				addressTextField.setText(addressBookEntry.getAddress());
				nameTextField.setText(addressBookEntry.getName());
				telephoneTextField.setText(addressBookEntry.getTelephone());
			}
		}
	}
}

⌨️ 快捷键说明

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