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

📄 j_combobox.java.bak

📁 一个十分好的java基础学习的课件
💻 BAK
字号:
// ////////////////////////////////////////////////////////
// 
// J_ComboBox.java
// 
// Created by Jun-Hai Yong,    on XX xx, xxxx (Date)
// ////////////////////////////////////////////////////////
// Readme:
//      Example of JComboBox and JList.
// ////////////////////////////////////////////////////////
// Using this example, please explicitly refer to the book:
//     Jun-Hai Yong. Programming in Java. 
//     Beijing: Tsinghua University Press, 2004.
// 使用本例子,请注明引用:
//     雍俊海. Java 程序设计. 北京: 清华大学出版社, 2004.
// Please note that the author and publisher make no 
// warranty of any kind on the examples provided.
// ////////////////////////////////////////////////////////

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

public class J_ComboBox extends JFrame
{
    private String       m_items[] = { "snow.gif", "flag.gif" };
    private JComboBox m_comboBox= new JComboBox( m_items );
    private JList         m_list       = new JList( m_items );
    private Icon           m_icons[] = {new ImageIcon(m_items[0]), new ImageIcon(m_items[1])};
    private JLabel       m_label      = new JLabel( m_icons[ 0 ] );
    
    public J_ComboBox( )
    {
        super( "Example of JComboBox  and JList" );
        Container container = getContentPane( );
        container.setLayout( new FlowLayout( ) ); 
        
        container.add( m_comboBox );
        m_comboBox.addItemListener(new ItemListener( ) // Register event handler
            {// anonymous inner class to handle JComboBox events
                public void itemStateChanged( ItemEvent event )
                {
                    if ( event.getStateChange( ) == ItemEvent.SELECTED )
                    {
                        m_list.setSelectedIndex( m_comboBox.getSelectedIndex( ) );
                        m_label.setIcon( m_icons[ m_comboBox.getSelectedIndex( ) ] );
                    }
                } // End of method: itemStateChanged
            } // End of anonymous inner class (ItemListener)
        ); // End of invoking addItemListener

        container.add( m_list );
        m_list.setSelectedIndex(0);
        m_list.addListSelectionListener(new ListSelectionListener( ) // Register event handler
            {// anonymous inner class to handle events
                public void valueChanged(ListSelectionEvent e )
                {
                    int i= m_list.getAnchorSelectionIndex( );
                    if ( 0<= i && i<=1 )
                        m_comboBox.setSelectedIndex( i );
                } // End of method: valueChanged
            } // End of anonymous inner class (ListSelectionListener)
        ); // End of invoking addListSelectionListener
        
        container.add( m_label );
        setSize( 220, 110 );
        setVisible( true );
    } // End of constructor: J_ComboBox( )
    
    public static void main( String args[] )
    { 
        J_ComboBox app = new J_ComboBox( );
        app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
    } // End of method: main

} // End of class: J_ComboBox

⌨️ 快捷键说明

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