📄 simplelist2.java
字号:
// SimpleList2.java// A variation on SimpleList. This version modifies the selecion model used.//package jswing.ch07;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;public class SimpleList2 extends JPanel { String label[] = { "Zero","One","Two","Three","Four","Five","Six", "Seven","Eight","Nine","Ten","Eleven" }; JList list; public SimpleList2() { setLayout(new BorderLayout()); list = new JList(label); JButton button = new JButton("Print"); JScrollPane pane = new JScrollPane(list); DefaultListSelectionModel m = new DefaultListSelectionModel(); m.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); m.setLeadAnchorNotificationEnabled(false); list.setSelectionModel(m); list.addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { System.out.println(e.toString()); } }); button.addActionListener(new PrintListener()); add(pane, BorderLayout.NORTH); add(button, BorderLayout.SOUTH); } public static void main(String s[]) { JFrame frame = new JFrame("List Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setContentPane(new SimpleList2()); frame.pack(); frame.setVisible(true); } // An inner class to respond to clicks on the Print button class PrintListener implements ActionListener { public void actionPerformed(ActionEvent e) { int selected[] = list.getSelectedIndices(); System.out.println("Selected Elements: "); for (int i=0; i < selected.length; i++) { String element = (String)list.getModel().getElementAt(selected[i]); System.out.println(" " + element); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -