当前位置: 代码迷 >> J2SE >> 请问怎么获得JComboBox的内容
  详细解决方案

请问怎么获得JComboBox的内容

热度:213   发布时间:2016-04-24 16:09:06.0
请教如何获得JComboBox的内容
如题

------解决方案--------------------
up
------解决方案--------------------
getSelectedItem()
------解决方案--------------------
给你个例子代码,你看看
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;

public class JComboBoxTest
{
public static void main(String[] args)
{
JFrame frame=new JFrame( "JComboBox Demo ");
final JComboBox comboBox=new JComboBox();
comboBox.addItem( "item1 ");
comboBox.addItem( "item2 ");
comboBox.addItem( "item3 ");
comboBox.addItem( "item4 ");
Container c=frame.getContentPane();
c.setLayout(new BorderLayout());
c.add(comboBox,BorderLayout.NORTH);
JButton b=new JButton( "OK ");
c.add(b,BorderLayout.SOUTH);
b.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e)
{
int index=comboBox.getSelectedIndex();
//comboBox.removeItemAt(index);
System.out.println(comboBox.getItemAt(index));
}
});
frame.setSize(200,200);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
------解决方案--------------------
String str= (String) jComboBox1.getSelectedItem();