📄 addressbookgui.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
/**
* Displays the information in an address book.
*
* @author LT
* @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) {
try {
String file = fileNameTextField.getText();
BufferedReader input =
new BufferedReader(new FileReader(file));
/* Removing all entries in the address book if it has any. */
addressBook.removeAllEntries();
/* Clearing the status area. */
statusTextArea.removeAll();
/* Read the first line in the specified file. */
String line = input.readLine();
/* The information of the person in the address book. */
String name = "",
address = "",
telephone = "";
/* Load the data of the specified file into the empty address book. */
while (line != null) {
StringTokenizer tokenizer =
new StringTokenizer(line, DELIM);
if( tokenizer.countTokens() != 3 ) {
/* A line does not have the expected number of tokens. */
statusTextArea.setText( "Invalid input data in the file "+"\""+file+"\".");
} else {
/* Load the data of the current line into the now-empty address book. */
name = tokenizer.nextToken();
address = tokenizer.nextToken();
telephone = tokenizer.nextToken();
AddressBookEntry addressBookEntry =
new AddressBookEntry(name,address,telephone);
addressBook.addEntry( addressBookEntry );
}
/* Read next line in the specified file. */
line = input.readLine();
}
/* Close the file. */
input.close();
/* Display the names of the people in the names list. */
namesList.setListData( addressBook.getNames() );
/* Update the label that displays the number of people in the address book. */
numberOfEntriesLabel.setText( addressBook.getNumberOfEntries()+"entries" );
/* Clears nameTextField, addressTextField, and telephoneTextField. */
nameTextField.removeAll();
addressTextField.removeAll();
telephoneTextField.removeAll();
} catch(FileNotFoundException fnfn) {
/* The specified file does not exist. */
statusTextArea.setText( "The file does not exist." );
} catch (IOException ioe) {
/* An error occurs while reading data from the specified file. */
statusTextArea.setText( ioe.toString() );
}
}
}
/**
* 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) {
if( addressBook.getNumberOfEntries() == 0 ) {
/* The address book is empty. */
statusTextArea.setText( "The address book is empty." );
} else if( namesList.isSelectionEmpty() ) {
/* The user has not selected a name. */
statusTextArea.setText( "Please selected a name." );
} else if ( ! namesList.getValueIsAdjusting()
&& namesList.getSelectedIndex() != -1 ) {
/* The name of the selected person in the names list. */
String name = (String) namesList.getSelectedValue();
/* An object of {@link AddressBookEntry} that contains
* information of the specified person.
*/
AddressBookEntry addrBookEntry = addressBook.getEntry( name );
/* Display the contact information for the selected name. */
nameTextField.setText( addrBookEntry.getName() );
addressTextField.setText( addrBookEntry.getAddress() );
telephoneTextField.setText( addrBookEntry.getTelephone() );
/* Display a status message for the action. */
statusTextArea.setText( name + " has been displayed." );
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -