1. 程式人生 > >java 取出JComboBox中的當前值

java 取出JComboBox中的當前值

               
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()就可以取到其中的選中值

           

再分享一下我老師大神的人工智慧教程吧。零基礎!通俗易懂!風趣幽默!還帶黃段子!希望你也加入到我們人工智慧的隊伍中來!https://blog.csdn.net/jiangjunshow