繁体   English   中英

ActionListener的语法错误

[英]syntax error with ActionListener

这是我的小应用程序代码的一部分,我确实在第50和85行出现语法错误,任何人都可以帮我解决这个问题并与您核对。此应用程序仅做一件事,请单击一个按钮,它需要您选择txt文件,该应用将读取并更改一个单词而不是另一个单词,并且假定使用相同的名称保存它:

 public void initListeners() {
    chooseFile.addActionListener(e)-> {
    JFileChooser fs = new JFileChooser(new File("C:\\"));
        fs.setDialogTitle("Choose document to change");
        fs.setFileFilter(new FileTypeFilter(".txt", " New Text Document"));
        int result = fs.showOpenDialog(null);
        if (result == JFileChooser.APPROVE_OPTION) {
            BufferedReader br;
            String fulltext = " ";
            try {
                File fi = fs.getSelectedFile();
                br = new BufferedReader(new FileReader(fi.getPath()));
                String line;
                int i = 0;
                while ((line = br.readLine()) != null) {
                    fulltext += line + "\r\n";
                }
                br.close();
                fulltext = fulltext.replace("tekst", "cs101");
            } catch (FileNotFoundException e) {
                JOptionPane.showMessageDialog(null, e.getMessage());
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(null, ex.getMessage());

            }
            PrintWriter upis;
            try {
                upis = new PrintWriter("C:\\Users\\Maksimovic\\Desktop\\tekstzaobradu.txt");
                upis.append(fulltext);
                upis.close();

            } catch (FileNotFoundException ex) {
                JOptionPane.showMessageDialog(null, ex.getMessage());
            }
        }
    };
}

更改:

.addActionListener(ActionEvent e)-> {

.addActionListener(e -> {

使用lambda快捷方式时,您不需要或应该具有参数的类型。 至于第85行,您将需要更好地设置代码格式,以便我们理解。

Edit 85需要一个addActionListener括号来结束addActionListener方法调用。

public void initListeners(){
    chooseFile.addActionListener(e -> {
        JFileChooser fs=new JFileChooser(new File("C:\\"));
        fs.setDialogTitle("Choose document to change");
        fs.setFileFilter(new FileTypeFilter(".txt"," New Text Document"));
        int result=fs.showOpenDialog(null);
        if(result==JFileChooser.APPROVE_OPTION){
            BufferedReader br;
            String fulltext=" ";
            try {
                File fi=fs.getSelectedFile();
                br=new BufferedReader(new FileReader(fi.getPath()));
                String line;

                int i=0;
                while((line=br.readLine())!=null){
                    fulltext+=line+"\r\n";
                }
                br.close();
                fulltext=fulltext.replace("text","cs101");
            } catch (FileNotFoundException e) {
                JOptionPane.showMessageDialog(null, e.getMessage() );
            }catch(IOException ex){
                JOptionPane.showMessageDialog(null, ex.getMessage() );
            }
            PrintWriter input;
            try {
                upis=new PrintWriter(new FileWriter(fi.getPath));
                upis.append(fulltext);
                upis.close();
            } catch (FileNotFoundException ex) {
                JOptionPane.showMessageDialog(null, ex.getMessage() );
            }
        }
    });
}

旁注:如果这是我的项目,则我会将大多数代码放在自己的方法中,以简化并简化调试。

例如,

public void initListeners(){
    chooseFile.addActionListener(e -> chooseFileActionPerformed());    
}

还应考虑使用AbstractActions代替ActionListeners。 这可以帮助您从视图中分离程序逻辑。

附带问题:您的文件输入和输出代码应在Swing事件线程之外并在其自己的后台工作线程中完成,例如SwingWorker提供的代码。 这应该使您的GUI更具响应性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM