0

我正在尝试制作一个简单的 JFileChooser,但到目前为止我所做的不会编译。编译器告诉我它在第 66 行和第 80 行找不到符号“类文件”:

JFileChooserExample.java:66:找不到符号符号:类文件位置:类 JFileChooserExample.ListenerClass 文件文件 = fc.getSelectedFile(); ^ JFileChooserExample.java:80:找不到符号符号:类文件位置:类 JFileChooserExample.ListenerClass 文件文件 = fc.getSelectedFile(); ^ 2 个错误

import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;
import java.awt.event.*;

public class JFileChooserExample extends JFrame
{       
static private final String newline = "\n";
private JButton openButton, saveButton;
private JTextArea log;
private JFileChooser fc;    
private ImageIcon openIcon, saveIcon;

public JFileChooserExample()                                                                      // constructor
{                          
  log = new JTextArea(5,20);                                                                              // create the log first, because the 
  log.setMargin(new Insets(5,5,5,5));                                                                 // action listeners need to refer to it
  log.setEditable(false);
  JScrollPane logScrollPane = new JScrollPane(log);

  fc = new JFileChooser();                                                                            // create a file chooser

  //fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);                               // Uncomment one of these lines to try a different
   //fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);                         // file selection mode. (FILES_ONLY) is default 

    openButton = new JButton(openIcon = new ImageIcon("image\\Open16.gif"));              // create the Open button                                                                                                  
   saveButton = new JButton(saveIcon = new ImageIcon("image\\Save16.gif"));           // create the Save button;

  JPanel buttonPanel = new JPanel();                                                  // put the buttons in a separate panel
  buttonPanel.add(openButton);
  buttonPanel.add(saveButton);

  add(buttonPanel, BorderLayout.NORTH);                                                         // add the buttons and the log to this panel
  add(logScrollPane, BorderLayout.CENTER);

   ListenerClass listener = new ListenerClass();
   openButton.addActionListener(listener);
   saveButton.addActionListener(listener);
}

public static void main(String[] args)
{       
    JFileChooserExample frame = new JFileChooserExample();     // new frame object 
    frame.setTitle("JFileChooser Example");                    // set frame title
    frame.pack();                                                             // sizes the frame so components fit frame  
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      // ends program on frame closing
    frame.setLocationRelativeTo(null);                                // centre frame
    frame.setVisible(true);                                    // make frame visible
}

private class ListenerClass implements ActionListener
{   
    public void actionPerformed(ActionEvent e)
    {
        if (e.getSource() == openButton)                                                                  // handle Open button action
       {
         int returnVal = fc.showOpenDialog(JFileChooserExample.this);

         if (returnVal == JFileChooser.APPROVE_OPTION) 
        {
            File file = fc.getSelectedFile();
                 log.append("Opening: " + file.getName() + "." + newline);                   // this is where a real application 
         }                                                                                                   // would open the file
        else 
        {
            log.append("Open command cancelled by user." + newline);
         }
         log.setCaretPosition(log.getDocument().getLength());
         } 
        else if (e.getSource() == saveButton)                                                        // handle Save button action
        {
         int returnVal = fc.showSaveDialog(FileChooserDemo.this);
         if (returnVal == JFileChooser.APPROVE_OPTION) 
             {
            File file = fc.getSelectedFile();
            log.append("Saving: " + file.getName() + "." + newline);                         // this is where a real application 
         }                                                                                                   // would open the file
             else 
             {
            log.append("Save command cancelled by user." + newline);
         }
         log.setCaretPosition(log.getDocument().getLength());
      }
    }  
}
}
4

1 回答 1

1

您需要导入 java.io.File

您可能还必须更改FileChooserDemo.thisJFileChooserExample.this(仅在第二个上方几行File

于 2013-04-05T20:26:17.373 回答