java 如何取出JComboBox中的当前值
展开全部
import javax.swing.JComboBox;
import javax.swing.JFrame;
public class Test2 extends JFrame implements Runnable {
private JComboBox box;
public Test2() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
box = new JComboBox();
box.addItem("葡萄");
box.addItem("香蕉");
box.addItem("苹果");
this.add(box);
this.pack();
}
public static void main(String[] args) {
Test2 test = new Test2();
test.setVisible(true);
Thread t = new Thread(test);
t.start();
}
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
String value = (String) box.getSelectedItem();
System.out.println(value);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
展开全部
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();
}
@Override
public void actionPerformed(ActionEvent e) {
int i=jBox.getSelectedIndex()+1;
String s=(String)jBox.getSelectedItem();
System.out.println("你选中的是第"+i+"项"+",内容是:"+s);
// 把i插入你数据库中对应的属性字段
}
}
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();
}
@Override
public void actionPerformed(ActionEvent e) {
int i=jBox.getSelectedIndex()+1;
String s=(String)jBox.getSelectedItem();
System.out.println("你选中的是第"+i+"项"+",内容是:"+s);
// 把i插入你数据库中对应的属性字段
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询