线程 "main" java.lang.NoSuchMethodException 中的异常:在我添加 ArrayList 之后

Exception in thread "main" java.lang.NoSuchMethodException: after I add ArrayList

我正在编写程序,将数字从 TextField 移动到 TextArea 以对它们进行排序、混洗或反转。所有输入的数字都必须保存在 ArrayList 中。

首先,我创建了带有按钮标签等的 javafx 阶段。然后我为按钮添加了侦听器,并将文本从 TextField 移动到 TextArea。在我分配到 ArrayList 中存储数字之前,我 运行 成功地完成了我的程序几次。然后我收到异常消息

Exception in Application constructor
Exception in thread "main" java.lang.NoSuchMethodException: StoreNumbers.main([Ljava.lang.String;)
    at java.lang.Class.getMethod(Class.java:1778)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:119)

我在网络和 Whosebug 上进行了搜索,但没有找到可以帮助我理解我做错了什么的东西。在我的代码下面。感谢您提前提供任何建议。

 import javafx.application.Application;
 import javafx.geometry.Pos;
 import javafx.scene.Scene;
 import javafx.scene.control.*;
 import javafx.scene.input.KeyCode;
 import javafx.scene.layout.BorderPane;
 import javafx.scene.layout.FlowPane;
 import javafx.scene.layout.HBox;
 import javafx.scene.layout.Pane;
 import javafx.stage.Stage;
 import java.util.*;

 public class StoreNumbers extends Application {
    private TextField textField = new TextField();
    private TextArea textArea = new TextArea();
    StoreNumberPane snPane = new StoreNumberPane();
    List<String> arrayList = new ArrayList<>();
    public void start(Stage primaryStage) {

    Button btSort = new Button("Sort");
    Button btShuffle = new Button("Shuffle");
    Button btReverse = new Button("Reverse");
    HBox hBox = new HBox(10);
    hBox.getChildren().addAll(btSort, btShuffle, btReverse);
    hBox.setAlignment(Pos.CENTER);

    btSort.setOnAction(e -> snPane.sort());
    btShuffle.setOnAction(e -> snPane.shuffle());
    btReverse.setOnAction(e -> snPane.reverse());

    Label label = new Label("Enter a number: ");

    textField.setPromptText("Enter integer");
    FlowPane flowPane = new FlowPane();

    flowPane.getChildren().addAll(label, textField);

    ScrollPane scrollPane = new ScrollPane(textArea);

    BorderPane pane = new BorderPane();
    pane.setCenter(textArea);
    pane.setBottom(hBox);
    pane.setTop(flowPane);

    pane.setOnKeyPressed(e -> {
        if (e.getCode() == KeyCode.ENTER) {
            obtainData();
            textField.clear();
        }
    });

    Scene scene = new Scene(pane, 400, 400);
    primaryStage.setTitle("Exercise20_20");
    primaryStage.setScene(scene);
    primaryStage.show();

    snPane.requestFocus();
}

    public void displayArray(){

    }

    private void obtainData(){

        String userInput = textField.getText();
        if(userInput != null)

        arrayList.add(userInput);

        textArea.setText(String.format("%s", arrayList));
    }



}

class StoreNumberPane extends Pane{

StoreNumbers storeNumbers = new StoreNumbers();




public void sort(){
    Collections.sort(storeNumbers.arrayList);

}

public void shuffle(){
    Collections.shuffle(storeNumbers.arrayList);
}

public void reverse(){
    Collections.reverse(storeNumbers.arrayList);
}
}

您从 class 应用程序扩展。通过这样做,您始终需要声明 main 函数。

public static void main(String args[]){
    launch (args);
}

看看这个简单的 oracle 示例...helloworld

您的应用程序的问题是您试图从 StoreNumberPane 再次创建应用程序 class 的新实例,甚至在旧实例完成自身实例化之前。

应该只有一个应用程序 class instance int eh 整个应用程序。因此,创建一个新实例无济于事。 JavaFX won't throw an error for it, but it is not the instance you are looking for

现在,您只需要 StoreNumberPane 中的数组列表,您有很多选择。

  • ArrayList 设为静态,现在可以从任何地方轻松访问
  • 创建一个 returns class 当前实例的方法,并使用它来访问列表
  • 最后也是最好的方法是传递要排序、随机播放或反转的 lsit

你的电话变成了,

btSort.setOnAction(e -> snPane.sort(arrayList));

你的方法声明变成,

public void sort(List list) {
    Collections.sort(list);
}

Though I am still not sure, why are you using theStoreNumberPanewhich extendsPane. It doesn't do anything that needs Pane to be extended.

一个完整的字符串排序示例:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class StoreNumbers extends Application {
    private TextField textField = new TextField();
    private TextArea textArea = new TextArea();
    StoreNumberPane snPane = new StoreNumberPane();
    public List<String> arrayList = new ArrayList<>();

    public void start(Stage primaryStage) {

        Button btSort = new Button("Sort");
        Button btShuffle = new Button("Shuffle");
        Button btReverse = new Button("Reverse");
        HBox hBox = new HBox(10);
        hBox.getChildren().addAll(btSort, btShuffle, btReverse);
        hBox.setAlignment(Pos.CENTER);

        btSort.setOnAction(e -> { snPane.sort(arrayList); refreshData(); });
        btShuffle.setOnAction(e -> { snPane.shuffle(arrayList); refreshData(); });
        btReverse.setOnAction(e -> { snPane.reverse(arrayList); refreshData(); });

        Label label = new Label("Enter a number: ");

        textField.setPromptText("Enter String");
        textArea.setEditable(false);
        FlowPane flowPane = new FlowPane();

        flowPane.getChildren().addAll(label, textField);

        ScrollPane scrollPane = new ScrollPane(textArea);

        BorderPane pane = new BorderPane();
        pane.setCenter(textArea);
        pane.setBottom(hBox);
        pane.setTop(flowPane);

        pane.setOnKeyPressed(e -> {
            if (e.getCode() == KeyCode.ENTER) {
                obtainData();
                textField.clear();
            }
        });

        Scene scene = new Scene(pane, 400, 400);
        primaryStage.setTitle("Exercise20_20");
        primaryStage.setScene(scene);
        primaryStage.show();

        snPane.requestFocus();
    }

    public void displayArray() {

    }

    private void obtainData() {
        String userInput = textField.getText();
        if (userInput != null)
            arrayList.add(userInput);
        textArea.setText(String.format("%s", arrayList));
    }

    private void refreshData() {
        textArea.clear();
        textArea.setText(String.format("%s", arrayList));
    }
    public static void main(String[] args) {
        launch(args);
    }

}

class StoreNumberPane extends Pane {

    public void sort(List<String> list) {
        Collections.sort(list);
    }

    public void shuffle(List<String> list) {
        Collections.shuffle(list);
    }

    public void reverse(List<String> list) {
        Collections.reverse(list);
    }

}