【问题标题】:cannot finds symbol action listener找不到符号动作侦听器
【发布时间】:2012-11-21 20:22:03
【问题描述】:

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

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

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

【问题讨论】:

标签: java swing import actionlistener jfilechooser


【解决方案1】:
import java.awt.event.ActionListener; // seems to be missing.

【讨论】:

  • import java.awt.event.ActionListener 不包含在 import java.awt.* 中; ??
  • 没有。你需要import java.awt.event.** 不适用于子包。
【解决方案2】:

您必须导入以下类。

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

【讨论】:

  • 谢谢 .. 我虽然 import java.awt.* 已经包含了所有的东西.. 非常感谢
【解决方案3】:

由于您的问题已经得到解答,我想提出一个建议。

使用 Eclipse 或 Netbeans 等 IDE。他们负责导入、格式化、检查异常等,让您的生活更轻松。

【讨论】:

    【解决方案4】:

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

    【讨论】:

      猜你喜欢
      • 2020-08-11
      • 2013-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-26
      • 1970-01-01
      • 2017-09-27
      • 2014-07-08
      相关资源
      最近更新 更多