简体   繁体   中英

Simple Java Program with GUI, what is my error?

I have the code below (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();
            }


    }

sorry if it isn't displaying properly, stackoverflow is glitching when I try to post the code... but anyway...

I'm a beginner in java, trying to produce a program that takes userinput "a path", has the user press a button, and then turns around and indexes that directory, and prints the output to this textarea...

I'm getting the following error:

    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 ================

You are trying to issue a java statement outside of a method. You can have your variable declarations outside of the method, but not statements.

zeButton.addActionListener(this); is within the variable declarations of the DirectoryIndexer class, which is improper. You may only call methods from within a method.

This kind of statement can't be outside methods: zeButton.addActionListener(this);

Try moving it to your constructor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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