程序说明:编写testJComboBox.java来探讨JComboBox与数组的结合,在j2sdk1.4.2_09编译并运行成功!
代码如下:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
public class testJComboBox extends JFrame
{
private String[] countries = { "中国1", "俄罗斯2", "美国3",
"意大利4", "澳大利5", "南非6", "巴西7", "西班牙8", "女儿国9"};
private JLabel itemsJLabel;
private JComboBox selectCountryJComboBox;
private JButton calculateJButton;
private JButton icalculateJButton;
//testJComboBox就是上面的类名;createUserInterface()就是下面看到的createUserInterface方法
public testJComboBox()
{
createUserInterface();
}
//createUserInterface方法,用以布置界面
public void createUserInterface()
{
// get content pane and set layout to null
Container contentPane = getContentPane();
contentPane.setLayout( null );
// set up itemsJLabel
itemsJLabel = new JLabel();
itemsJLabel.setText( "点什么是什么" );
itemsJLabel.setBounds( 16, 48, 104, 21 );
contentPane.add( itemsJLabel );
//set up selectCountryJComboBox
Arrays.sort( countries ); //对数组countries 进行排序,不过这里效果不对啊,???
selectCountryJComboBox = new JComboBox( countries );
selectCountryJComboBox.setBounds( 16, 100, 104, 21 );
selectCountryJComboBox.setMaximumRowCount( 3 );
selectCountryJComboBox.setSelectedIndex( 0 );
contentPane.add( selectCountryJComboBox );
// set up calculateJButton
calculateJButton = new JButton();
calculateJButton.setText( "获取名称" );
calculateJButton.setBounds( 204, 48, 126, 24 );
contentPane.add( calculateJButton );
//如果要添加事件处理,就要在这边声明
calculateJButton.addActionListener(//这是鼠标事件,所以用addActionListener
//如果是键盘事件就要用addKeyListener
new ActionListener() // anonymous inner class,
{
// event handler called when calculateJButton is pressed
public void actionPerformed( ActionEvent event )//
{
calculateJButtonActionPerformed( event );
//calculateJButtonActionPerformed方法用响应鼠标单击事件,看下面
}
} // end anonymous inner class
); // end call to addActionListener
// set up icalculateJButton呵呵呵呵额额
icalculateJButton= new JButton();
icalculateJButton.setText( "获取索引" );
icalculateJButton.setBounds( 204, 100, 126, 24 );
contentPane.add(icalculateJButton);
//如果要添加事件处理,就要在这边声明
icalculateJButton.addActionListener(//这是鼠标事件,所以用addActionListener
//如果是键盘事件就要用addKeyListener
new ActionListener() // anonymous inner class,
{
public void actionPerformed( ActionEvent event )//
{
icalculateJButtonActionPerformed( event );
}
}
);
// set properties of application window
setTitle( "testJComboBox" ); // set title bar text
setSize( 354, 200 ); // set window size
setVisible( true ); // display window
} // end method createUserInterface
//获取名称
private void calculateJButtonActionPerformed( ActionEvent event )
{
itemsJLabel.setText(( String ) selectCountryJComboBox.getItemAt(selectCountryJComboBox.getSelectedIndex()));
}
//获取索引
private void icalculateJButtonActionPerformed( ActionEvent event )
{ int i= selectCountryJComboBox.getSelectedIndex();
//itemsJLabel.setText((char)i);
icalculateJButton.setText("索引是"+i);
//奇怪icalculateJButton.setText(i);就不行啊,什么原因啊???
}
//下面是main()方法
// main method
public static void main( String[] args )
{
testJComboBox application = new testJComboBox();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
} // end method main
} // end class testJComboBox