使用JFileChooser打开文件

时间:2011-11-27 03:32:55

标签: java file swing jfilechooser

作为一个小小的项目,我认为制作文本编辑器会很酷。我目前只是打开文件了。这是我打开文件的代码(eActionEvent,打开是JMenuItem):

else if (e.getSource() == open) {
        JFileChooser choice = new JFileChooser();
        int option = choice.showOpenDialog(this);
        if (option == JFileChooser.APPROVE_OPTION) {
            try{
                Scanner scan = new Scanner(new FileReader((open).getSelectedFile().getPath()));
            }
        }

    }

试块给我带来了麻烦。 Eclipse称getSelectedFile()未定义类型JMenuItem。对于MenuItem来说,它似乎也未定义。有没有其他方法可以解决这个问题,还是另一种方法可以解决这个问题?

3 个答案:

答案 0 :(得分:9)

一旦返回,您需要在JFileChooser上致电getSelectedFile(),因此请将代码更改为:

choice.getSelectedFile()

答案 1 :(得分:3)

  private void selectfileActionPerformed(java.awt.event.ActionEvent evt) {                                           

    JFileChooser jFileChooser=new JFileChooser();
   StringBuffer buffer;
    buffer = new StringBuffer();
   int result= jFileChooser.showOpenDialog(this);
    if(result==JFileChooser.APPROVE_OPTION)
    {
        try {
            FileReader reader;
            reader = null;
            JOptionPane.showMessageDialog(this,"hii user clicked open sysytem");
            File file=jFileChooser.getSelectedFile();
            reader=new FileReader(file);
            int i=1;
            while(i!=-1)
            {
                i=reader.read();
                char ch=(char) i;
                buffer.append(ch);

            }

            notepad.setText(buffer.toString());
        } catch (FileNotFoundException ex) {
            Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}                                          

答案 2 :(得分:0)

import java.awt.EventQueue;

public class FileChooser extends JFrame
{
    private JPanel contentPane;
    String filename;
   // main
    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                try
                {
                    FileChooser frame = new FileChooser();
                    frame.setVisible(true);
                } catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
        });
    }

    public FileChooser()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        // button to selct file
        JButton btnNewButton = new JButton("Select file");
        btnNewButton.setBounds(10, 2, 89, 23);
        contentPane.add(btnNewButton);
        // area to display file content
        final JTextArea textArea = new JTextArea();
        textArea.setBounds(10, 36, 414, 215);
        contentPane.add(textArea);
         // save button
        final JButton btnSave = new JButton("Save");
        btnSave.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent arg0)
            {

                try
                {
                    FileWriter writer = new FileWriter(filename.replace(".",
                            "_out."));
                    BufferedWriter bwr = new BufferedWriter(writer);

                    bwr.write(textArea.getText());

                    bwr.close();
                    writer.close();
                    System.out.println(textArea.getText());
                } catch (Exception e)
                {
                    System.out.println("Error");
                }
            }
        });
        btnSave.setBounds(283, 2, 89, 23);
        contentPane.add(btnSave);
      // listen to button clicks
        btnNewButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                JFileChooser fileChooser = new JFileChooser();
                int returnValue = fileChooser.showOpenDialog(null);
                if (returnValue == JFileChooser.APPROVE_OPTION)
                {
                    File selectedFile = fileChooser.getSelectedFile();
                    filename = selectedFile.getAbsolutePath();

                    try
                    {
                        FileReader reader = new FileReader(filename);
                        BufferedReader br = new BufferedReader(reader);
                        textArea.read(br, null);
                        br.close();
                        System.out.println(textArea.getText());

                    } catch (Exception e)
                    {
                        System.out.println("Error");
                    }enter code here

                }

            }
    enter code here
        });

    }
}