-1

我有下面的代码(java)

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.io.File;

    public class DirectoryIndexer extends JFrame implements ActionListener, MouseListener {

            JPanel OutputPane001 = new JPanel();
            JTextArea OutputTextArea001 = new JTextArea();

            JPanel zeButtonPane = new JPanel();
            JButton zeButton = new JButton("Index This Mess!");
            zeButton.addActionListener(this);

            JPanel InputPane001 = new JPanel();
            JTextField InputTextField001 = new JTextField();

            String[] DirList;
            String DirListConcat = "";


            public DirectoryIndexer() {
                super();
                setSize(750, 550);
                setTitle("920's Second Attempt At A Java Program With A GUI");
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                BorderLayout OutputPaneLayout = new BorderLayout();
                OutputPane001.setLayout(OutputPaneLayout);
                OutputTextArea001.setSize(550,435);
                OutputPane001.add(OutputTextArea001);
                OutputPane001.setSize(600,485);
                add(OutputPane001, BorderLayout.WEST);

                BorderLayout zeButtonPaneLayout = new BorderLayout();
                zeButtonPane.setLayout(zeButtonPaneLayout);
                zeButtonPane.add(zeButton);
                zeButtonPane.setSize(120,485);
                add(zeButtonPane, BorderLayout.EAST);

                BorderLayout InputPaneLayout = new BorderLayout();
                InputPane001.setLayout(InputPaneLayout);
                InputPane001.add(InputTextField001);
                InputPane001.setSize(720,20);
                add(InputPane001, BorderLayout.SOUTH);

                setVisible(true);
            }

            public void actionPerformed(ActionEvent event) {
                super();
                string DirToIndex = InputTextField001.getText();
                String files;
                File folder = new File(DirToIndex);
                File[] listOfFiles = folder.listFiles(); 

                for (int i = 0; i < listOfFiles.length; i++) 
                {        
                    if (listOfFiles[i].isFile()) 
                    {
                        files = listOfFiles[i].getName();
                        DirList[i] = files;
                        DirListConcat = DirListConcat + files;
                    }
                }

                OutputTextArea001.setText(DirListConcat);           

            }

            public static void main(String[] arguments) {
                DirectoryIndexer MainFrame = new DirectoryIndexer();
            }


    }

抱歉,如果它没有正确显示,当我尝试发布代码时,stackoverflow 会出现故障......但无论如何......

我是java的初学者,试图生成一个程序,该程序接受用户输入“路径”,让用户按下一个按钮,然后转身并索引该目录,并将输出打印到这个文本区域......

我收到以下错误:

    cmd /c cd "C:\Users\Charles\custom_java_dir\charles\GUI2" && "javac" "DirectoryIndexer.java"
    Process started >>>
    DirectoryIndexer.java:13: error: <identifier> expected
            zeButton.addActionListener(this);
                                      ^
    DirectoryIndexer.java:13: error: illegal start of type
            zeButton.addActionListener(this);
                                       ^
    2 errors
    <<< Process finished. (Exit code 1)
    cmd /c java DirectoryIndexer
    Process started >>>
    <<< Process finished. (Exit code 0)
    ================ READY ================
4

3 回答 3

3

您正试图在方法之外发出 java 语句。您可以在方法之外声明变量,但不能在语句之外声明。

于 2013-09-06T20:26:50.553 回答
2

zeButton.addActionListener(this);DirectoryIndexer类的变量声明中,这是不恰当的。您只能从方法中调用方法。

于 2013-09-06T20:27:18.363 回答
1

这种声明不能是外部方法:zeButton.addActionListener(this);

尝试将其移至您的构造函数。

于 2013-09-06T20:28:07.017 回答