2012-12-04 71 views
3

我正在使用JFileChooser做浏览文件类。编译时遇到问题。它一直告诉我比找不到符号actionlistener。下面是我的代码:找不到符号动作监听器

import java.util.*; 
import java.io.*; 
import javax.swing.*; 
import java.awt.*; 
import java.text.*; 
import javax.swing.filechooser.*; 


public class BrowseForFile 
{ 
private JTextField txtFileName; 
private JFrame layout; 

public BrowseForFile() 
{ 
    super(); 
    initialize(); 
} 

    public void initialize() 
    { 
     //empty layout 
     layout = new JFrame(); 
     layout.setTitle("Task Synchronization "); 
     layout.setBounds(100, 100, 800, 600); 
     layout.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     layout.getContentPane().setLayout(null); 

     //set the copyright 
     JLabel lblNewLabel_5 = new JLabel("(c) 2012 All Rights Reserved"); 
     lblNewLabel_5.setForeground(Color.GRAY); 
     lblNewLabel_5.setFont(new Font("Tahoma", Font.PLAIN, 10)); 
     lblNewLabel_5.setHorizontalAlignment(SwingConstants.RIGHT); 
     lblNewLabel_5.setBounds(527, 548, 255, 14); 
     layout.getContentPane().add(lblNewLabel_5); 

     //set the label 
     JLabel lblSendAFile = new JLabel("Select a file to be sent to all nodes"); 
     lblSendAFile.setBounds(404, 400, 378, 14); 
     layout.getContentPane().add(lblSendAFile); 

     //set the textfield 
     txtFileName = new JTextField(); 
     txtFileName.setBounds(404, 425, 277, 20); 
     layout.getContentPane().add(txtFileName); 
     txtFileName.setColumns(10); 

     //set the browse button and let it to choose file after click. 
     JButton btnBrowse = new JButton("Browse"); 
     btnBrowse.setBounds(691, 424, 91, 23); 
     layout.getContentPane().add(btnBrowse); 

     btnBrowse.addActionListener(new ActionListener() 
     { 
      public void actionPerformed(ActionEvent e) 
      { 
       JFileChooser chooser = new JFileChooser(); 
       chooser.setCurrentDirectory(new File(dirName)); 
       chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); 

       FileNameExtensionFilter filter = new FileNameExtensionFilter(".txt only", "txt"); 
       chooser.setFileFilter(filter); 

       try { 
       int code = chooser.showOpenDialog(null); 
       if (code == JFileChooser.APPROVE_OPTION) { 
       File selectedFile = chooser.getSelectedFile(); 
       Scanner input = new Scanner(selectedFile); 
       String f=selectedFile.getName(); 
       txtFileName.setText("File Name is: "+f); 



       } 

       } catch (Exception f) { 
       f.printStackTrace(); 
       } 
      } 
     }); 
    } 







    public static void main(String[] args) 
    { 
     try 
     { 
      BrowseForFile window = new BrowseForFile(); 
      window.layout.setVisible(true); 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 
    } 


} 

这是错误:

BrowseForFile.java:52: error: cannot find symbol 
        btnBrowse.addActionListener(new ActionListener() 
                ^
symbol: class ActionListener 
location: class BrowseForFile 
1 error 

谁能告诉我什么是错误?提前致谢。

+0

你是否用'Notepad'编写了所有代码? –

+0

ya。我在记事本++ – Eric

+0

中编写了一切可能的[Java:如何打印数组? “找不到符号”](http://stackoverflow.com/questions/13057439/java-how-do-you-print-an-array-cannot-find-symbol) – Raedwald

回答

9
import java.awt.event.ActionListener; // seems to be missing. 
+0

import java.awt.event.ActionListener isn' t包含在import java.awt。*中; ?? – Eric

+2

不需要。您需要'import java.awt.event。*'。 '*'不适用于子包。 – Isaac

+0

好的。谢谢..我已经解决了这个问题..非常感谢 – Eric

4

您必须导入以下类别。

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
+0

谢谢..我虽然进口java.awt。*已包含所有的东西..非常感谢 – Eric

1

import java.awt。*表示从该包中导入所有类,但不从子包导入。对于每个子包,你必须把单独的导入语句。例如import java.awt.event,import java.awt.datatransfer。*等

2

由于您的问题已经回答,我想提供一个建议。

使用Eclipse或Netbeans之类的IDE。他们负责进口,格式化,检查异常等,让您的生活变得更加轻松。