当前位置: 代码迷 >> J2SE >> 一个关于JFileChooser的有关问题
  详细解决方案

一个关于JFileChooser的有关问题

热度:98   发布时间:2016-04-24 13:57:40.0
一个关于JFileChooser的问题
今天试着写了个Swing,但是JFileChooser始终都报一个错...大虾们帮忙看看..

import   java.awt.Component;
import   java.awt.FlowLayout;
import   java.awt.event.ActionEvent;
import   java.awt.event.ActionListener;
import   java.awt.event.KeyEvent;
import   java.io.File;

import   javax.swing.JButton;
import   javax.swing.JFileChooser;
import   javax.swing.JPanel;
import   javax.swing.JTextField;

public   class   CreateUI{

static   String   displayTxt   =   " ";
JTextField   tf   =   null;
public   Component   createCom(){
JPanel   panel   =   new   JPanel();
tf   =   new   JTextField();
tf.setColumns(20);
JButton   Fchoose   =   new   JButton( "Choose   Directory ");
Fchoose.setMnemonic(KeyEvent.VK_D);
ActionListener   chooser   =   new   ActionListener(){

public   void   actionPerformed(ActionEvent   arg0)   {

String   txt   =   tf.getText();
JFileChooser   jfc   =   null;
if(txt   !=   null   &&   txt.length()!=0){
File   file   =   new   File(txt);
if(!file.exists()){
txt   =   " ";
}
}else{
txt   =   " ";
}
if(txt.length()   ==0){
try{
jfc   =   new   JFileChooser( "c:\\ ");
}catch(Exception   e){
System.out.println( "这儿报错了..... ");
e.printStackTrace();
}
}else{
jfc   =   new   JFileChooser(txt);
}
jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int   rtnVal   =   jfc.showOpenDialog(null);
if(rtnVal   ==   JFileChooser.APPROVE_OPTION){
tf.setText(jfc.getSelectedFile().getAbsolutePath());
}

}

};
Fchoose.addActionListener(chooser);
JButton   jb1   =   new   JButton( "Convert   HTM   To   txt ");
jb1.setMnemonic(KeyEvent.VK_H);
jb1.addActionListener(new   ActionListener(){
public   void   actionPerformed(ActionEvent   e){
boolean   flag   =   true;
String   txt   =   " ";
if(tf.getText()   !=   null   &&   tf.getText().length()!=0){
txt   =     new   String(tf.getText());
File   file   =   new   File(txt);
if(!file.exists()   ||   !file.isDirectory()){
flag   =   false;
}
}
if(flag){
HtmConvert   convert   =   new   HtmConvert();
displayTxt   =   convert.convert(txt);
}
}


});
JButton   jb2   =   new   JButton( "Conver   ansi   to   UTF-8 ");
jb2.setMnemonic(KeyEvent.VK_A);
ActionListener   ac1   =   new   ActionListener(){

public   void   actionPerformed(ActionEvent   arg0)   {
boolean   flag   =   true;
String   txt   =   " ";
if(tf.getText()   !=   null   &&   tf.getText().length()!=0){
txt   =     new   String(tf.getText());
File   file   =   new   File(txt);