做了这个例子后不得不再一次感叹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手册写的