java ListTest

第一個檔名:ListFrame.java

import java.awt.FlowLayout;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.ListSelectionModel;

public class ListFrame extends JFrame

{

   private JList colorJList;

   private final String colorNames[] = { "Black", "Blue" , "Pink", "Orange",
                                         "Red", "White", "Yellow" } ;

   private final Color colors[] = { Color.BLACK, Color.BLUE, Color.PINK, Color.ORANGE,
                                    Color.RED, Color.WHITE, Color.YELLOW };

   public ListFrame()

   {

      super("List Test");

      setLayout( new FlowLayout() );

      colorJList = new JList( colorNames );

      colorJList.setVisibleRowCount( 5 );

      colorJList.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );

      add( new JScrollPane( colorJList ) );

      colorJList.addListSelectionListener(

         new ListSelectionListener()

         {

            public void valueChanged( ListSelectionEvent event )

            {

               getContentPane().setBackground( colors[ colorJList.getSelectedIndex() ] );

            }

         }

      );

   }

}

------------------------------------------------------------------------------------
第二個檔名:ListTest.java

import javax.swing.JFrame;

public class ListTest

{

   public static void main ( String args[] )

   {

      ListFrame listFrame = new ListFrame();

      listFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

      listFrame.setSize( 350, 150 );

      listFrame.setVisible( true );

   }

}

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 a25561970 的頭像
    a25561970

    萬花筒

    a25561970 發表在 痞客邦 留言(0) 人氣()