java中如何获取下拉列表中的选项 10
就是用户在下拉列表中选了一项,然后在控制台显示刚才用户选择了哪一项,书上只写了一部分代码,请问还应该怎么弄?importjava.awt.*;importjava.uti...
就是用户在下拉列表中选了一项,然后在控制台显示刚才用户选择了哪一项,书上只写了一部分代码,请问还应该怎么弄?
import java.awt.*;
import java.util.Locale;
import javax.swing.*;
import javax.swing.event.*;
public class JF extends JFrame{
JComboBox jc=new JComboBox(new mcb());
JLabel jl=new JLabel("可选的选项有:");
public JF(){
setSize(300,300);
setVisible(true);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
Container cp=getContentPane();
cp.setLayout(new FlowLayout());
cp.add(jl);
cp.add(jc);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new JF();
}
class mcb extends AbstractListModel implements ComboBoxModel{
String selecteditem=null;
String[] itemname={"选项A","选项B","选项C","选项D","选项E"};
public void setSelectedItem(Object item){
selecteditem=(String)item;
}
@Override
public int getSize() {
// TODO Auto-generated method stub
return itemname.length;
}
@Override
public Object getElementAt(int index) {
// TODO Auto-generated method stub
return itemname[index];
}
@Override
public Object getSelectedItem() {
// TODO Auto-generated method stub
return selecteditem;
}
public int getIndex(){
for(int i=0;i<itemname.length;i++){
if(itemname[i].equals(getSelectedItem())){
return 1;
}
}
return 0;
}
}
}
我本想用System.out.print输出返回的selecteditem,但那些方法里不让我加这句啊,怎么解决 展开
import java.awt.*;
import java.util.Locale;
import javax.swing.*;
import javax.swing.event.*;
public class JF extends JFrame{
JComboBox jc=new JComboBox(new mcb());
JLabel jl=new JLabel("可选的选项有:");
public JF(){
setSize(300,300);
setVisible(true);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
Container cp=getContentPane();
cp.setLayout(new FlowLayout());
cp.add(jl);
cp.add(jc);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new JF();
}
class mcb extends AbstractListModel implements ComboBoxModel{
String selecteditem=null;
String[] itemname={"选项A","选项B","选项C","选项D","选项E"};
public void setSelectedItem(Object item){
selecteditem=(String)item;
}
@Override
public int getSize() {
// TODO Auto-generated method stub
return itemname.length;
}
@Override
public Object getElementAt(int index) {
// TODO Auto-generated method stub
return itemname[index];
}
@Override
public Object getSelectedItem() {
// TODO Auto-generated method stub
return selecteditem;
}
public int getIndex(){
for(int i=0;i<itemname.length;i++){
if(itemname[i].equals(getSelectedItem())){
return 1;
}
}
return 0;
}
}
}
我本想用System.out.print输出返回的selecteditem,但那些方法里不让我加这句啊,怎么解决 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询