JComboBox与数组、数据库的结合--实例一

 程序说明:编写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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值