Java 通过按钮获取JCombobox组件的选值
各位大神,不知道为什么,我在一个窗口中,用了按钮组件和JCombobox组件,点击按钮之后,JCombobox的值,老是第一个,求各位大神求助下。代码如下:String[...
各位大神,不知道为什么,我在一个窗口中,用了按钮组件和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<(fileName.length()-1)) )
{
String extension = fileName.substring(index + 1).toLowerCase();
if( extension.equals("pdf") )
{
return true;
}
}
return false;
}
public String getDescription()
{
return "PDF文件";
}
}
}
不知道为什么,结果永远是A,求各位大神帮忙啊! 展开
代码如下:
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<(fileName.length()-1)) )
{
String extension = fileName.substring(index + 1).toLowerCase();
if( extension.equals("pdf") )
{
return true;
}
}
return false;
}
public String getDescription()
{
return "PDF文件";
}
}
}
不知道为什么,结果永远是A,求各位大神帮忙啊! 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询