[英]Simple Java Program with GUI, what is my error?
I have the code below (java) 我有下面的代码(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... 抱歉,如果显示不正确,当我尝试发布代码时,stackoverflow会出现故障...但是无论如何...
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... 我是Java的初学者,尝试生成一个程序,该程序采用userinput“路径”,让用户按下按钮,然后转过身并对该目录建立索引,然后将输出打印到此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. 您正在尝试在方法之外发出Java语句。 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. 在DirectoryIndexer
类的变量声明中,这是不正确的。 You may only call methods from within a method. 您只能从方法内部调用方法。
This kind of statement can't be outside methods: zeButton.addActionListener(this);
这种声明不能在方法之外: zeButton.addActionListener(this);
Try moving it to your constructor. 尝试将其移至构造函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.