![](/img/trans.png)
[英]Exception in thread “main” java.lang.NoSuchMethodException:
[英]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)
我已经在网上和stackoverflow上进行了搜索,但是没有找到可以帮助我了解自己在做什么的东西。 在我的代码下面。 预先感谢您提供任何建议。
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);
}
}
应用程序的问题StoreNumberPane
,即使在旧实例完成自身实例化之前,您仍试图从StoreNumberPane
创建应用程序类的新实例。
整个应用程序中应该只有一个应用程序类实例。 因此,创建新实例将无济于事。 JavaFX won't throw an error for it, but it is not the instance you are looking for
现在,您只需要在StoreNumberPane
一个arraylist,就可以选择很多方法。
ArrayList
为静态,现在可以从任何地方轻松访问 你的电话变成
btSort.setOnAction(e -> snPane.sort(arrayList));
您的方法声明变为
public void sort(List list) {
Collections.sort(list);
}
Though I am still not sure, why are you using the
which extends
Pane Though I am still not sure, why are you using the
StoreNumberPane . It doesn't do anything that needs Pane to be extended.
. 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);
}
}
您从类应用程序扩展。 这样做总是需要声明main函数。
public static void main(String args[]){
launch (args);
}
看看这个简单的oracle示例... helloworld
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.