2013-04-05 36 views
0

我試圖做一個簡單的JFileChooser,但我做了什麼至今不會編譯。編譯器告訴我,它不能在線路66 & 80找到符號「類文件」:JFileChooser中不會編譯

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()); 
     } 
    } 
} 
} 

回答

1

您需要進口的java.io.File

您可能還需要改變(第二File上面只有幾行)FileChooserDemo.thisJFileChooserExample.this