我正在尝试创建一个简单的JFileChooser,但到目前为止我所做的不会编译。编译器告诉我它在第66行和第66行中找不到符号'class File'。 80:
JFileChooserExample.java:66:找不到符号 符号:类文件 location:类JFileChooserExample.ListenerClass 文件file = fc.getSelectedFile(); ^ JFileChooserExample.java:80:找不到符号 符号:类文件 location:类JFileChooserExample.ListenerClass 文件file = 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());
}
}
}
}
答案 0 :(得分:1)
您需要导入java.io.File
您可能还需要将FileChooserDemo.this
更改为JFileChooserExample.this
(仅比第二个File
高几行)