close

第一個檔
檔名為:ComboBoxFrame.java

import java.awt.FlowLayout;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.Icon;
import javax.swing.ImageIcon;

public class ComboBoxFrame extends JFrame

{

   private JComboBox imagesJComboBox;

   private JLabel label;

   private String names[] = {"Girl1.jpg","Girl2.jpg","Girl3.jpg","Girl4.jpg"};

   private Icon icons[] =

      { new ImageIcon( getClass().getResource( names[ 0 ] ) ),
        new ImageIcon( getClass().getResource( names[ 1 ] ) ),
        new ImageIcon( getClass().getResource( names[ 2 ] ) ),
        new ImageIcon( getClass().getResource( names[ 3 ] ) ) };

      public ComboBoxFrame()

      {

         super( "Testing JconboBox");

         setLayout( new FlowLayout() );

         imagesJComboBox = new JComboBox( names );

         imagesJComboBox.setMaximumRowCount( 3 );

         imagesJComboBox.addItemListener
           
            (

               new ItemListener()

               {

               public void itemStateChanged( ItemEvent event )

               {

                  if ( event.getStateChange() == ItemEvent.SELECTED )

                     label.setIcon ( icons[imagesJComboBox.getSelectedIndex() ] );
               }

            }

         );

         add( imagesJComboBox );

         label = new JLabel( icons [0] );

         add( label );

   }

}


---------------------------------------------------------------------------------------
第二個檔
檔名為:ComboBoxTest.java

import javax.swing.JFrame;

public class ComboBoxTest

{

   public static void main ( String args[] )

   {

      ComboBoxFrame comboBoxFrame = new ComboBoxFrame();
      comboBoxFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
      comboBoxFrame.setSize(300, 100);
      comboBoxFrame.setVisible(true);

   }

}

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

    萬花筒

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