我有以下代码(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的初学者,尝试生成一个程序,用户输入“路径”,用户按下按钮,然后转身并索引该目录,并将输出打印到此textarea ...
我收到以下错误:
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 ================
答案 0 :(得分:3)
您正尝试在方法之外发出java语句。您可以在方法之外使用变量声明,但不能使用语句。
答案 1 :(得分:2)
zeButton.addActionListener(this);
在DirectoryIndexer
类的变量声明中,这是不合适的。您只能从方法中调用方法。
答案 2 :(得分:1)
这种陈述不能是外部方法:zeButton.addActionListener(this);
尝试将其移至构造函数。