J2SE JComboBox存储两个值实例

做了这个例子后不得不再一次感叹JAVA的强大,自己继承控件,再自己改写控件,精细的控件调整,直至符合使用要求。
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class Test extends JFrame {
   
     private JButton button;
     private JComboBox comboBox;
       
     public Test() {
      super("JCombox存储两个值 演示");  //调用父类构造函数
      Container contentPane = getContentPane();  //得到容器
      contentPane.setLayout(new FlowLayout());  //设置布局管理器为Flowlayout
        
            button = new JButton("选择");  //初始化按钮
            comboBox=new JComboBox();  //初始化组合框
            Demo d=new Demo("第一项","1");
           
            comboBox.addItem(d);  //增加组合框列表内容
            contentPane.add(comboBox);  //增加组件到容器
      contentPane.add(button);

      button.addActionListener(new ActionListener() {  //按钮事件处理
          public void actionPerformed(ActionEvent e) {
         Demo h=(Demo)comboBox.getSelectedItem();//提取对象
           JOptionPane.showMessageDialog(null,h.toString()+"  "+h.getValue() ); //显示提示信息
         
      }
      });

      this.setSize(200,100);  //设置窗口大小
      this.setVisible(true);  //设置窗口可见
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  //关闭窗口时退出程序
     }

     public static void main(String args[]) {
      new Test();
     }
    }
class Demo
{
    private String name;
    private String value;
   
    public Demo(String n1,String n2)
    {
    this.name=n1;
    this.value=n2;
    }
    public String toString()
    {
     return name;
    }
    public String getValue()
    {
        return value;
    }
    }
网上找的思路,自己查API手册写的

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值