gpt4 book ai didi

java - 带有GUI的简单Java程序,我的错误是什么?

转载 作者:行者123 更新时间:2023-12-02 11:07:11 25 4
gpt4 key购买 nike

我有下面的代码(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的初学者,尝试生成一个程序,该程序采用userinput“路径”,让用户按下按钮,然后转过身并对该目录建立索引,然后将输出打印到此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 ================

最佳答案

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

关于java - 带有GUI的简单Java程序,我的错误是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18666048/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com