-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path程序9_2.java
43 lines (41 loc) · 1.37 KB
/
程序9_2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class 程序9_2 {
JFrame frame = new JFrame("JList Demo");
JList list;
DefaultListModel listModel;
JPanel panel;
JTextField tf;
JButton button;
public static void main(String[] args) {
程序9_2 ld = new 程序9_2();
ld.go();
}
public void go() {
listModel = new DefaultListModel();
listModel.addElement("one");
listModel.addElement("two");
listModel.addElement("three");
listModel.addElement("four");
list = new JList<>(listModel);
// 将列表放入滚动窗格JSrollPane中
JScrollPane jsp = new JScrollPane(list,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
Container cp = frame.getContentPane();
cp.add(jsp);
tf = new JTextField(15);
button = new JButton("add new item");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
listModel.addElement(tf.getText()); // 添加新的可选项
}
});
panel = new JPanel();
panel.add(tf);
panel.add(button);
cp.add(panel,BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}