📄 phonelistgui.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
/**
* Displays the information in an phone list.
*
* @author author name
* @version 1.0.0
*/
public class PhoneListGUI extends JPanel {
/* Delimiters */
private final String DELIM = "_";
private final String PHONE_DELIM = ":";
private static BufferedReader stdIn =
new BufferedReader(new InputStreamReader(System.in));
/* 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 fixedTextField;
private JTextField mobileTextField;
private JList namesList;
private JTextArea statusTextArea;
private PhoneList phoneList;
/**
* Creates a window.
*
* @param args not used.
* @throws IOException
* @throws FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException, IOException {
JFrame frame = new JFrame("Phone List");
frame.setContentPane(new PhoneListGUI());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(WIDTH, HEIGHT);
frame.setVisible(true);
}
/**
* Creates a graphical user interface.
*/
public PhoneListGUI() {
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);
fixedTextField = new JTextField("", 17);
mobileTextField = 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(true);
fixedTextField.setEditable(true);
mobileTextField.setEditable(true);
statusTextArea.setEditable(true);
// 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("Fixed Phone: "));
labelsPanel.add(new JLabel("Mobile Phone: "));
valuesPanel.add(nameTextField);
valuesPanel.add(fixedTextField);
valuesPanel.add(mobileTextField);
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);
phoneList = new PhoneList();
}
/**
* This inner class handles <code>openButton</code> events.
*/
class OpenButtonListener implements ActionListener {
/**
* Loads phone list data from the specified file and displays
* the names in a list.
*
* @param event the event object.
*/
public void actionPerformed(ActionEvent event) {
/* PLACE YOUR CODE HERE */
numberOfEntriesLabel.getText();
}
}
/**
* This inner class handles <code>displayButton</code> events.
*/
class DisplayButtonListener implements ActionListener {
/**
* Displays the phone entry for the selected person.
*
* @param event the event object.
*/
public void actionPerformed(ActionEvent event) {
/* PLACE YOUR CODE HERE */;
statusTextArea.setText("");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -