java如何“获取jcombobox里的值”并转化为字符串?
1个回答
展开全部
获取jcombobox里的值,并将它转化为字符串的程序如下:
import java.awt.event.ItemEvent;import java.awt.event.ItemListener;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class EEE extends JFrame implements ItemListener{ JComboBox A;
JPanel pane;
String a[] = {"动物","植物","真菌","病毒","原生动物"};
DefaultComboBoxModel category = new DefaultComboBoxModel(a);
EEE(){
pane = new JPanel();
A = new JComboBox(category);
A.addItemListener(this);
pane.add(A);
add(pane);
setBounds(400, 200, 400, 300);
setResizable(false);
setVisible(true);
}
public void itemStateChanged(ItemEvent e){
if(e.getSource()==A){
if(e.getStateChange()==ItemEvent.SELECTED){
String s=(String)A.getSelectedItem();
System.out.println(s);
//JOptionPane.showMessageDialog(null, s);
}
}
}
public static void main(String[] args) {
new EEE();
}
}
import java.awt.event.ItemEvent;import java.awt.event.ItemListener;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class EEE extends JFrame implements ItemListener{ JComboBox A;
JPanel pane;
String a[] = {"动物","植物","真菌","病毒","原生动物"};
DefaultComboBoxModel category = new DefaultComboBoxModel(a);
EEE(){
pane = new JPanel();
A = new JComboBox(category);
A.addItemListener(this);
pane.add(A);
add(pane);
setBounds(400, 200, 400, 300);
setResizable(false);
setVisible(true);
}
public void itemStateChanged(ItemEvent e){
if(e.getSource()==A){
if(e.getStateChange()==ItemEvent.SELECTED){
String s=(String)A.getSelectedItem();
System.out.println(s);
//JOptionPane.showMessageDialog(null, s);
}
}
}
public static void main(String[] args) {
new EEE();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询