📄 emaillisting.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class EmailListing
extends JFrame
implements ActionListener, ListSelectionListener {
private DefaultListModel data = new DefaultListModel();
private JList list = new JList(data);
private JTextField name = new JTextField();
private JTextField email = new JTextField();
private JButton add = new JButton("Add");
private JButton del = new JButton("Delete");
private class WindowCloser
extends WindowAdapter {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
}
public EmailListing() {
/* defines the basic layout of the GUI components */
super("Email List");
JPanel buttons = new JPanel(new FlowLayout());
buttons.add(add);
buttons.add(del);
JPanel input = new JPanel(new GridLayout(2, 2));
input.add(new JLabel("Name: "));
input.add(name);
input.add(new JLabel("Email: "));
input.add(email);
JScrollPane scrollList = new JScrollPane(list);
getContentPane().add("North", new JPanelBox(input, "Address"));
getContentPane().add("Center", new JPanelBox(scrollList, "List"));
getContentPane().add("South", buttons);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.addListSelectionListener(this);
add.addActionListener(this);
del.addActionListener(this);
email.addActionListener(this);
addWindowListener(new WindowCloser());
validate();
pack();
setVisible(true);
}
public void handleDelete() {
/* handles deletion of an address selected in the list */
int tempIndex=list.getSelectedIndex() ;
if (tempIndex >= 0) {
data.remove(tempIndex);
}
}
public void handleAdd() {
/* handles the addition of a new address */
if (!name.equals("")) {
data.addElement(new Address(name.getText(), email.getText()));
name.setText("");
email.setText("");
name.requestFocus();
}
}
public void actionPerformed(ActionEvent ae) {
/* react to action events by calling appropriate handlers */
if (ae.getSource() == del) {
handleDelete();
}
else if ( (ae.getSource() == add) || (ae.getSource() == email)) {
handleAdd();
}
}
public void valueChanged(ListSelectionEvent lse) {
/* displays a 'full' address corresponding to the selected name */
if (!lse.getValueIsAdjusting()) {
Address address = (Address) data.get(list.getSelectedIndex());
name.setText(address.name);
email.setText(address.email);
}
}
public static void main(String args[]) {
EmailListing el = new EmailListing();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -