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

📄 listdemo.java

📁 Swing组件
💻 JAVA
字号:
//==============================================================
// ListDemo.java - Demonstrate JList list objects
//
// Java学习源代码检索系统 Ver 1.0 20031015 免费正式版
// 版权所有: 中国IT认证实验室(www.ChinaITLab.com)
// 程序制作: ChinaITLab网校教研中心
// 主页地址: www.ChinaITLab.com    中国IT认证实验室
// 论坛地址: bbs.chinaitlab.com  
// 电子邮件: Java@ChinaITLab.com
//==============================================================

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

public class ListDemo extends JFrame {

 private JFrame frame;  // Refers to this JFrame window
 private JLabel label;  // Shows current selection

// Constructor does all the setup work
 public ListDemo() {

  // Select local system look and feel
  try {
   UIManager.setLookAndFeel(
    UIManager.getSystemLookAndFeelClassName());
  } catch (Exception e) { }

  // End program when window closes
  addWindowListener(new WindowAdapter() {
   public void windowClosing(WindowEvent e) {
    System.exit(0);
   }
  });

  frame = this;  // So event handler can find our window

  String[] items = {
   "Sunday", "Monday", "Tuesday", "Wednesday",
   "Thursday", "Friday", "Saturday"
  };
  JList dayList = new JList(items);
  dayList.setSelectionMode(
   ListSelectionModel.SINGLE_SELECTION);
  dayList.setAlignmentX(Component.CENTER_ALIGNMENT);
  JScrollPane listScroller = new JScrollPane(dayList);

  // Respond to a list selection event
  dayList.addListSelectionListener(
   new ListSelectionListener() {
    public void valueChanged(ListSelectionEvent e) {
     JList list = (JList)e.getSource();
     if (!list.isSelectionEmpty()) {
      int i = list.getSelectedIndex();
      String s = (String)list.getModel().getElementAt(i);
      label.setText("Selection: " + s);
     }
    }
   }
  );

  label = new JLabel("Select a day");
  Container content = getContentPane();
  content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
  content.add(label);
  content.add(listScroller);
 }

 public static void main(String[] args) {
  ListDemo app = new ListDemo();
  app.setTitle("List Demonstration");
  app.setSize(320, 240);
  app.show();
 }
}

⌨️ 快捷键说明

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