⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 s17.htm

📁 Java2Swingt界面设计
💻 HTM
📖 第 1 页 / 共 2 页
字号:
              <br>              &quot;item[3]&quot;, &quot;item[4]&quot;, &quot;item[5]&quot;,<br>              &quot;item[6]&quot;, &quot;item[7]&quot;, <br>              &quot;item[8]&quot;, &quot;item[9]&quot; };</p>            <p> JList list = new JList(items);</p>            <p> list.setPrototypeCellValue(&quot;MMMMMMM&quot;);</p>            <p> controlPanel = new ControlPanel(list);<br>              controlPanel.update();</p>            <p> listPanel.setBorder(BorderFactory.createEtchedBorder());<br>              listPanel.add(new JScrollPane(list));</p>            <p> contentPane.add(controlPanel, BorderLayout.NORTH);<br>              contentPane.add(listPanel, BorderLayout.CENTER);</p>            <p> list.addListSelectionListener(<br>              new ListSelectionListener() {<br>              public void valueChanged(ListSelectionEvent e) {<br>              controlPanel.update();<br>              }<br>              });<br>              }<br>              }<br>              class ControlPanel extends JPanel {<br>              private JComboBox mode = new JComboBox();<br>              private JButton clear = new JButton(&quot;clear selection&quot;);</p>            <p> private String single = &quot;SINGLE_SELECTION&quot;,<br>              singleInterval = &quot;SINGLE_INTERVAL_SELECTION&quot;,<br>              multipleInterval = &quot;MULTIPLE_INTERVAL_SELECTION&quot;;</p>            <p> private JLabel leadLabel = new JLabel(),<br>              anchorLabel = new JLabel(),<br>              minLabel = new JLabel(),<br>              maxLabel = new JLabel(),<br>              selIndicesLabel = new JLabel();</p>            <p> private JList list;</p>            <p> public ControlPanel(JList l) {<br>              JPanel top = new JPanel(), <br>              mid = new JPanel(),<br>              bottom = new JPanel();</p>            <p> this.list = l;</p>            <p> setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); <br>              setBorder(BorderFactory.createEtchedBorder());</p>            <p> top.add(mode);<br>              top.add(clear);</p>            <p> mid.add(new JLabel(&quot;Lead:&quot;)); mid.add(leadLabel);<br>              mid.add(new JLabel(&quot;Anchor:&quot;)); mid.add(anchorLabel);<br>              mid.add(new JLabel(&quot;Minimum:&quot;)); mid.add(minLabel);<br>              mid.add(new JLabel(&quot;Maximum:&quot;)); mid.add(maxLabel);</p>            <p> add(top);<br>              add(mid);<br>              add(bottom);</p>            <p> mode.addItem(single);<br>              mode.addItem(singleInterval);<br>              mode.addItem(multipleInterval);<br>              initializeSelectionMode();</p>            <p> bottom.add(new JLabel(&quot;Selected Indices:&quot;));<br>              bottom.add(selIndicesLabel);</p>            <p> mode.addItemListener(new ItemListener() {<br>              public void itemStateChanged(ItemEvent e) {<br>              if(e.getStateChange() == ItemEvent.SELECTED)<br>              setSelectionMode((String)e.getItem());<br>              }<br>              });<br>              clear.addActionListener(new ActionListener() {<br>              public void actionPerformed(ActionEvent e) {<br>              list.clearSelection();<br>              }<br>              });<br>              }<br>              public void update() {<br>              int lead = list.getLeadSelectionIndex(),<br>              min = list.getMinSelectionIndex(),<br>              max = list.getMaxSelectionIndex(),<br>              anchor = list.getAnchorSelectionIndex();</p>            <p> leadLabel.setText(Integer.toString(lead) + &quot; / &quot;);<br>              anchorLabel.setText(Integer.toString(anchor) + &quot; / &quot;);<br>              minLabel.setText(Integer.toString(min) + &quot; / &quot;);<br>              maxLabel.setText(Integer.toString(max) + &quot; / &quot;);</p>            <p> int[] selected = list.getSelectedIndices();<br>              String s = new String();</p>            <p> for(int i = 0; i &lt; selected.length; ++i) {<br>              s += Integer.toString(selected[i]);</p>            <p> if(i &lt; selected.length-1)<br>              s += &quot;,&quot;;<br>              }<br>              selIndicesLabel.setText(s);<br>              validate();<br>              }<br>              private void initializeSelectionMode() {<br>              int m = list.getSelectionMode();</p>            <p> switch(m) {<br>              case ListSelectionModel.SINGLE_SELECTION:<br>              mode.setSelectedItem(single);<br>              break;<br>              case ListSelectionModel.SINGLE_INTERVAL_SELECTION:<br>              mode.setSelectedItem(singleInterval);<br>              break;<br>              case ListSelectionModel.MULTIPLE_INTERVAL_SELECTION:<br>              mode.setSelectedItem(multipleInterval);<br>              break;<br>              }<br>              }<br>              private void setSelectionMode(String s) {<br>              if(s.equals(&quot;SINGLE_SELECTION&quot;)) {<br>              list.setSelectionMode(<br>              ListSelectionModel.SINGLE_SELECTION);<br>              }<br>              else if(s.equals(&quot;SINGLE_INTERVAL_SELECTION&quot;)) {<br>              list.setSelectionMode(<br>              ListSelectionModel.SINGLE_INTERVAL_SELECTION);<br>              }<br>              else if(s.equals(&quot;MULTIPLE_INTERVAL_SELECTION&quot;)) {<br>              list.setSelectionMode(<br>              ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);<br>              }<br>              }<br>              }</p>            <hr size="1" noshade>            <p align="center"><b>例17-6 处理列表数据事件</b></p>            <hr noshade size="1">            import java.awt.*;<br>            import java.awt.event.*;<br>            import javax.swing.*;<br>            import javax.swing.event.*;            <p>public class Test extends JApplet {<br>              private JList list = new JList();</p>            <p> private String[] items = { <br>              &quot;item one&quot;, &quot;item two&quot;, &quot;item three&quot;,               <br>              &quot;item four&quot;, &quot;item five&quot;, &quot;item six&quot;,<br>              &quot;item seven&quot;, &quot;item eight&quot;, <br>              &quot;item nine&quot;, &quot;item ten&quot; <br>              };</p>            <p> public void init() {<br>              Container contentPane = getContentPane();</p>            <p> JPanel controlPanel = new ControlPanel(this, list);</p>            <p> contentPane.add(controlPanel, BorderLayout.NORTH);<br>              contentPane.add(new JScrollPane(list), <br>              BorderLayout.CENTER);<br>              populateList();<br>              }<br>              public void populateList() {<br>              final DefaultListModel model = new DefaultListModel();</p>            <p> for(int i=0; i &lt; items.length; ++i)<br>              model.addElement(items[i]);</p>            <p> list.setModel(model);</p>            <p> if(list.isShowing())<br>              list.revalidate();</p>            <p> model.addListDataListener(new ListDataListener() {<br>              public void contentsChanged(ListDataEvent e) {<br>              showStatus(&quot;contents changed&quot;);<br>              }<br>              public void intervalRemoved(ListDataEvent e) {<br>              Object[] message = new Object[] {<br>              &quot;Removed item at index &quot; + e.getIndex0(),<br>              &quot; &quot;,<br>              &quot;There are now &quot; + model.getSize() + &quot; items&quot;<br>              };<br>              JOptionPane.showMessageDialog(Test.this,<br>              message,<br>              &quot;Items Removed&quot;, // title<br>              JOptionPane.INFORMATION_MESSAGE); // type<br>              }<br>              public void intervalAdded(ListDataEvent e) {<br>              showStatus(&quot;interval added&quot;);<br>              }<br>              });<br>              }<br>              }<br>              class ControlPanel extends JPanel {<br>              JButton remove = new JButton(&quot;remove selected items&quot;);<br>              JButton repopulate = new JButton(&quot;repopulate&quot;);</p>            <p> public ControlPanel(final Test applet, final JList list) {<br>              add(remove);<br>              add(repopulate);</p>            <p> remove.addActionListener(new ActionListener() {<br>              public void actionPerformed(ActionEvent e) {<br>              int[] selected = list.getSelectedIndices();<br>              DefaultListModel model =<br>              (DefaultListModel)list.getModel();</p>            <p> for(int i=0; i &lt; selected.length; ++i) {<br>              model.removeElementAt(selected[i] - i);<br>              }<br>              }<br>              });<br>              repopulate.addActionListener(new ActionListener() {<br>              public void actionPerformed(ActionEvent e) {<br>              applet.populateList();<br>              }<br>              });<br>              }<br>              }</p>            <hr size="1" noshade>            <p>&nbsp;</p>            <p>&nbsp;</p>            <p align="center"><b>例17-7 处理鼠标双击和三击</b></p>            <hr noshade size="1">            import java.awt.*;<br>            import java.awt.event.*;<br>            import javax.swing.*;<br>            import javax.swing.event.*;            <p>public class Test extends JApplet {<br>              public void init() {<br>              Container contentPane = getContentPane();</p>            <p> String[] items = { &quot;item one&quot;, &quot;item two&quot;,               &quot;item three&quot;, <br>              &quot;item four&quot;, &quot;item five&quot;, &quot;item six&quot;,<br>              &quot;item seven&quot;, &quot;item eight&quot;, <br>              &quot;item nine&quot;, &quot;item ten&quot; };</p>            <p> JList list = new JList(items);</p>            <p> contentPane.setLayout(new FlowLayout());<br>              contentPane.add(new JScrollPane(list));</p>            <p> list.addMouseListener(new MouseAdapter() {<br>              public void mouseClicked(MouseEvent e) {<br>              JList theList = (JList)e.getSource();<br>              ListModel model = theList.getModel();</p>            <p> int index = theList.locationToIndex(e.getPoint());<br>              String itemString = <br>              (String)model.getElementAt(index);</p>            <p> String s = new String(&quot; for &quot; + <br>              model.getElementAt(index));</p>            <p> switch(e.getClickCount()) {<br>              case 1:<br>              showStatus(&quot;Single Click&quot; + s);<br>              break;<br>              case 2:<br>              showStatus(&quot;Double Click&quot; + s);<br>              break;<br>              case 3:<br>              showStatus(&quot;Triple Click&quot; + s);<br>              break;<br>              }<br>              }<br>              });<br>              }<br>              }</p>            <hr size="1" noshade>            <p> 17.3.3 JList类总结</p>            <p>&nbsp;</p>            <p> 17.3.4 AWT兼容</p>            <p>&nbsp;</p>            <p> 17.4 本章回顾</p>            <p>&nbsp;</p>            <p>[<a href="index.html" target="_self">目录</a>][<a href="s16.htm">上一页</a>][<a href="s18.htm">下一页</a>](飒龙收藏/2002.5.18)             </p>            </td>          </tr>        </tbody>      </table>    </td>  </tr></tbody></table><script language="javascript">bottomprint()</script></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -