簡體   English   中英

ShowopenDialog在actionlistener下不起作用

[英]ShowopenDialog not working under actionlistener

因此,我一直試圖制作一個打開文本文件的文件選擇器,然后將內容粘貼到我已定義為textArea的JtextArea中。 但是我不能讓showOpenDialog在有參數(this)時不給出錯誤,我進行了研究,答案是填寫(null),這確實使filechooser起作用,但是當我嘗試打印它的內容時也只是返回null。 我正在使用Eclipse程序,因此使用自動填充的代碼。 我對Java相當陌生,不知道出了什么問題。 如果這不是在此處發布內容的方式,我真的很抱歉。

JButton btnNewButton = new JButton("Bladeren");
btnNewButton.addActionListener(
    new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JFileChooser fileChooser = new JFileChooser();
            fileChooser.setCurrentDirectory(new File(System.getProperty("user.home")));
            int result = fileChooser.showOpenDialog(this);
            if (result == JFileChooser.APPROVE_OPTION) {
                File selectedFile = fileChooser.getSelectedFile();
                System.out.println("Selected file: " + selectedFile.getAbsolutePath());
                String content = readFile(selectedFile, StandardCharsets.UTF_8);
                System.out.println(content);
                textArea.setText(content);
            }

        }

        private String readFile(File selectedFile, Charset utf8) {
            // TODO Auto-generated method stub
            return null;
        }
    }
);

您可以查看API ,該參數必須來自Component類型。 那么在您的示例中this意味着什么? 您的課程從哪個其他類/接口extendsimplements

您沒有粘貼readFile方法的代碼,但問為什么它返回null? 這樣我們就無法為您提供幫助,因此請發布代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM