📄 j_combobox.java
字号:
// ////////////////////////////////////////////////////////
//
// J_ComboBox.java
//
// ////////////////////////////////////////////////////////
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 + -