请叫各位大虾一下,我想在JComboBox中对对应的每一条在添加一个隐藏的值

smallcat96 2003-10-16 10:55:23
问题是对现有的JComboBoxr的如何继承什么我才能每条在添加一个隐藏的值,
这个值还能在运行时获取。
请各位指叫一二。最好中有实例。
...全文
67 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
LoveRose 2003-10-17
  • 打赏
  • 举报
回复
写错了
是((Item)com.getSelectedItem()).getValue();
LoveRose 2003-10-17
  • 打赏
  • 举报
回复
((Item)com.getSelectedItem()).value
就取得了选定的值
LoveRose 2003-10-16
  • 打赏
  • 举报
回复
import javax.swing.*;
import java.lang.*;
import java.awt.*;

public class TestFrame extends JFrame
{
public TestFrame()
{
super();
}
public static void main(String[] args)
{
JComboBox com = new JComboBox();
Item item1 = new Item();
Item item2 = new Item("Second");
Item item3 = new Item("Third","3");
item1.setText("First");
item1.setValue("1");
item2.setValue("2");
com.addItem(item1);
com.addItem(item2);
com.addItem(item3);
com.setSize(100,20);
TestFrame f = new TestFrame();
f.getContentPane().setLayout(new FlowLayout());
f.setBounds(100,100,400,300);
f.getContentPane().add(com);
f.show();
}
}

class Item extends Object
{
private String text;
private String value;

public Item()
{
this.text = null;
this.value = null;
}

public Item(String text)
{
this.text = text;
this.value = null;
}

public Item(String text,String value)
{
this.text = text;
this.value = value;
}

public void setText(String text)
{
this.text = text;
}

public String getText()
{
return this.text;
}

public void setValue(String value)
{
this.value = value;
}

public String getValue()
{
return this.value;
}

public String toString()
{
return this.text;
}
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧