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

📄 listtest.java

📁 java 课件与资料 java 课件与资料 java 课件与资料 java 课件与资料
💻 JAVA
字号:
// Fig. 13.14: ListTest.java
// Selecting colors from a JList.
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;

public class ListTest extends JFrame {
   private JList colorList;
   private Container container;
 
   private final String colorNames[] = { "Black", "Blue", "Cyan", 
      "Dark Gray", "Gray", "Green", "Light Gray", "Magenta",
      "Orange", "Pink", "Red", "White", "Yellow" };

   private final Color colors[] = { Color.black, Color.blue, Color.cyan, 
      Color.darkGray, Color.gray, Color.green, Color.lightGray, 
      Color.magenta, Color.orange, Color.pink, Color.red, Color.white, 
      Color.yellow };

   // set up GUI
   public ListTest()
   {
      super( "List Test" );

      // get content pane and set its layout
      container = getContentPane();
      container.setLayout( new FlowLayout() );

      // create a list with items in colorNames array
      colorList = new JList( colorNames );
      colorList.setVisibleRowCount( 5 );
      
      // do not allow multiple selections
      colorList.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );

      // add a JScrollPane containing JList to content pane
      container.add( new JScrollPane( colorList ) );
      colorList.addListSelectionListener(

         new ListSelectionListener() {  // anonymous inner class 

            // handle list selection events
            public void valueChanged( ListSelectionEvent event )
            {
               container.setBackground( 
                  colors[ colorList.getSelectedIndex() ] );
            }

         } // end anonymous inner class

      ); // end call to addListSelectionListener

      setSize( 350, 150 );
      setVisible( true );

   } // end ListTest constructor

   public static void main( String args[] )
   { 
      ListTest application = new ListTest();
      application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
   }

} // end class ListTest

⌨️ 快捷键说明

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