各位大神,不知道为什么,我在一个窗口中,用了按钮组件和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,求各位大神帮忙啊!
下面是Form类/*
* SelectPDF.java
*
* Created on __DATE__, __TIME__
*/package forms;import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.filechooser.FileFilter;
import java.io.File;/**
*
* @author __USER__
*/
public class SelectPDF extends javax.swing.JFrame { /** Creates new form SelectPDF */
public SelectPDF() {
try
{
if( System.getProperty("os.name").toUpperCase().indexOf("WINDOWS") != -1 )
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
initComponents();
} /** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
//GEN-BEGIN:initComponents
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() { String[] actypes = {"A","B","C"};
jComboBox1 = new javax.swing.JComboBox();
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(actypes));
jButton1 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); //jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] {
// "Item 1", "Item 2", "Item 3", "Item 4" })); jButton1.setText("\u8bf7\u9009\u62e9PDF\u6587\u4ef6"); jLabel1.setText("\u8bf7\u9009\u62e9\u673a\u578b\uff1a"); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
getContentPane());
getContentPane().setLayout(layout);
layout
.setHorizontalGroup(layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout
.createSequentialGroup()
.addGroup(
layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout
.createSequentialGroup()
.addGap(
60,
60,
60)
.addComponent(
jLabel1)
.addGap(
30,
30,
30)
.addComponent(
jComboBox1,
javax.swing.GroupLayout.PREFERRED_SIZE,
97,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(
layout
.createSequentialGroup()
.addGap(
117,
117,
117)
.addComponent(
jButton1,
javax.swing.GroupLayout.PREFERRED_SIZE,
165,
javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(118, Short.MAX_VALUE)));
layout
.setVerticalGroup(layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout
.createSequentialGroup()
.addGap(83, 83, 83)
.addGroup(
layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(
jComboBox1,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(53, 53, 53).addComponent(
jButton1).addContainerGap(116,
Short.MAX_VALUE))); //button添加监听事件
jButton1.addActionListener(
new SelectPDFButtonAction( this.jComboBox1.getSelectedIndex() )
);
pack();
}// </editor-fold>
//GEN-END:initComponents /**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new SelectPDF().setVisible(true);
}
});
} //GEN-BEGIN:variables
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JComboBox jComboBox1;
private javax.swing.JLabel jLabel1;
private int jcomboboxselect;
// End of variables declaration//GEN-END:variables
}
public SelectPDF() {
try
{
if( System.getProperty("os.name").toUpperCase().indexOf("WINDOWS") != -1 )
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
initComponents();
}
jComboBox1 = new javax.swing.JComboBox();
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(actypes));
jButton1 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("\u8bf7\u9009\u62e9PDF\u6587\u4ef6"); jLabel1.setText("\u8bf7\u9009\u62e9\u673a\u578b\uff1a");
//button添加监听事件
jButton1.addActionListener(
new SelectPDFButtonAction( this.jComboBox1.getSelectedIndex() )
);
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.filechooser.FileFilter;/**
*
* @author __USER__
*/
public class SelectPDF extends javax.swing.JFrame { /** Creates new form SelectPDF */
public SelectPDF() {
try {
if (System.getProperty("os.name").toUpperCase().indexOf("WINDOWS") != -1) {
UIManager
.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
} catch (Exception ex) {
ex.printStackTrace();
}
initComponents();
} /**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
// GEN-BEGIN:initComponents
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() { String[] actypes = { "A", "B", "C" };
jComboBox1 = new javax.swing.JComboBox();
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(actypes));
jButton1 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); // jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[]
// {
// "Item 1", "Item 2", "Item 3", "Item 4" })); jButton1.setText("\u8bf7\u9009\u62e9PDF\u6587\u4ef6"); jLabel1.setText("\u8bf7\u9009\u62e9\u673a\u578b\uff1a"); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(layout
.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout.createSequentialGroup()
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout.createSequentialGroup()
.addGap(60, 60,
60)
.addComponent(
jLabel1)
.addGap(30, 30,
30)
.addComponent(
jComboBox1,
javax.swing.GroupLayout.PREFERRED_SIZE,
97,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(
layout.createSequentialGroup()
.addGap(117,
117,
117)
.addComponent(
jButton1,
javax.swing.GroupLayout.PREFERRED_SIZE,
165,
javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(118, Short.MAX_VALUE)));
layout.setVerticalGroup(layout
.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout.createSequentialGroup()
.addGap(83, 83, 83)
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(
jComboBox1,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(53, 53, 53).addComponent(jButton1)
.addContainerGap(116, Short.MAX_VALUE))); // button添加监听事件
System.out.println("======"+this.jComboBox1.getSelectedIndex());
System.out.println("======----"+this.jComboBox1.getSelectedItem());
jButton1.addActionListener(new SelectPDFButtonAction(this.jComboBox1
)); pack(); }// </editor-fold>
// GEN-END:initComponents /**
* @param args
* the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new SelectPDF().setVisible(true);
}
});
} // GEN-BEGIN:variables
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JComboBox jComboBox1;
private javax.swing.JLabel jLabel1;
private int jcomboboxselect;
// End of variables declaration//GEN-END:variables}class SelectPDFButtonAction implements ActionListener {
private int jcombox;
JComboBox combox; SelectPDFButtonAction(int jcombox) {
this.jcombox = jcombox;
}
SelectPDFButtonAction(JComboBox combox) {
this.combox = combox;
} 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);
System.out.println("------"+this.combox.getSelectedIndex());
int i = this.combox.getSelectedIndex();
if (this.jcombox == 0)
System.out.println("A");
if (this.jcombox == 1)
System.out.println("B");
if (this.jcombox == 2)
System.out.println("C");
if (i == 0)
System.out.println("A");
if (i == 1)
System.out.println("B");
if (i == 2)
System.out.println("C");
} }
}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文件";
}
}
给你调试了下,你可以试试。