📄 j_combobox.java.bak
字号:
// ////////////////////////////////////////////////////////
//
// J_ComboBox.java
//
// Created by Jun-Hai Yong, on XX xx, xxxx (Date)
// ////////////////////////////////////////////////////////
// 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 JApplet
{
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 JLabel m_label =new JLabel( );
private Icon m_icons[] =new ImageIcon[2];
public void init( )
{
Image theImage[] = {getImage(getCodeBase( ), m_items[0]),
getImage(getCodeBase( ), m_items[1]) };
m_icons[0]= new ImageIcon(theImage[0]);
m_icons[1]= new ImageIcon(theImage[1]);
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 );
m_label.setIcon( m_icons[ 0 ] );
} // End of method: init
} // End of class J_ComboBox
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -