java 获取组件_Java 通过按钮获取JCombobox组件的选值

各位大神,不知道为什么,我在一个窗口中,用了按钮组件和JCombobox组件,点击按钮之后,JCombobox的值,老是第一个,求各位大神求助下。代码如下:String[]actypes={"A","B","C"};jC...

各位大神,不知道为什么,我在一个窗口中,用了按钮组件和JCombobox组件,点击按钮之后,JCombobox的值,老是第一个,求各位大神求助下。

代码如下:

String[] actypes = {"A","B","C"};

jComboBox1 = new javax.swing.JComboBox();

jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(actypes));

jButton1 = new javax.swing.JButton();

jButton1.addActionListener(

new SelectPDFButtonAction( this.jComboBox1.getSelectedItem().toString() )

);

public class SelectPDFButtonAction implements ActionListener

{

private int jcombox;

SelectPDFButtonAction( int jcombox )

{

this.jcombox = jcombox;

}

public void actionPerformed( ActionEvent e )

{

JFrame choosefile = new JFrame("请选择文件");

JFileChooser filechooser = new JFileChooser();

filechooser.setDialogTitle("请选择文件");

//不显示所有文件

filechooser.setAcceptAllFileFilterUsed(false);

//过滤除pdf外的其他文件

filechooser.setFileFilter(new PDFFilter());

//已经选择文件

if( filechooser.showOpenDialog(choosefile)==0 )

{

String filepath = filechooser.getSelectedFile().getPath();

System.out.println( filepath );

if( this.jcombox==0 )

System.out.println( "A" );

if( this.jcombox==1 )

System.out.println( "B" );

if( this.jcombox==2)

System.out.println( "C" );

}

}

//pdf过滤器

private class PDFFilter extends FileFilter

{

public boolean accept(File file)

{

if( file.isDirectory() )

return true;

String fileName = file.getName();

int index = fileName.indexOf(".");

if( (index>0) && (index

{

String extension = fileName.substring(index + 1).toLowerCase();

if( extension.equals("pdf") )

{

return true;

}

}

return false;

}

public String getDescription()

{

return "PDF文件";

}

}

}

不知道为什么,结果永远是A,求各位大神帮忙啊!

展开

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值