java 取出JComboBox中的当前值

来源:互联网 发布:sql删除数据库命令 编辑:程序博客网 时间:2024/06/04 00:36
import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JComboBox;import javax.swing.JFrame;import javax.swing.JPanel;public class JCom implements ActionListener{JComboBox<String> jBox;public JCom() {jBox=new JComboBox<String>();jBox.addItem("超级用户");jBox.addItem("管理员");jBox.addItem("普通用户");jBox.addActionListener(this);JFrame jf=new JFrame("测试");JPanel jp=new JPanel();jp.add(jBox);jf.add(jp);jf.setBounds(200, 100, 300, 400);jf.setVisible(true);jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}public static void main(String[]args){new JCom();}@Overridepublic void actionPerformed(ActionEvent e) {int i=jBox.getSelectedIndex()+1;String s=(String)jBox.getSelectedItem();System.out.println("你选中的是第"+i+"项"+",内容是:"+s);// 把i插入你数据库中对应的属性字段}}

JComboBox有一个
SelectedItem属性,所以使用getSelectedItem()就可以取到其中的选中值

原创粉丝点击