启动 javaFX 程序时出错

Error in launching javaFX program

我的客户端 class 从服务器接收消息并使用 UDP 网络将消息发送到服务器,但我想获取输入并在 [=20= 中显示来自服务器 class 的输入和输出] 命名为 "Chit-chat" 。但我不知道该怎么做。我还尝试在构造函数中传递 launch(Client.class) 但它显示错误。

public class Client extends Application{

Thread send;
Thread accept;
DatagramPacket pack;
DatagramSocket sock;
private String str[];
String name, sname;
int listeningPort;
InetAddress server_ip;
String sender;

public Parent createContent(){
    ScrollPane sp = new ScrollPane();
    TextFlow textFlow = new TextFlow();
    textFlow.setPadding(new Insets(10));
    textFlow.setLineSpacing(10);
    TextField textField = new TextField();
    textField.setPrefSize(50,30);
    Button button = new Button("Send");
    button.setPrefSize(80,30);
    Button button2 = new Button("Start");
    button2.setPrefSize(50,30);
    VBox container = new VBox();
    VBox box = new VBox();
    box.getChildren().addAll(sp,textFlow);
    container.setPadding(new Insets(10));
    container.getChildren().addAll(box, new HBox(textField, button,button2));
    VBox.setVgrow(sp, Priority.ALWAYS);
    VBox.setVgrow(textFlow, Priority.ALWAYS);
    return container;
}

public void playSound() {
    String gongFile =  "C:\Users\HP\IdeaProjects\FirstGUI\src\sample\Really\Small-bell-jingling.wav";
    InputStream in = null;
    try {
        in = new FileInputStream(gongFile);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    AudioStream audioStream = null;
    try {
        audioStream = new AudioStream(in);
    } catch (IOException e) {
        e.printStackTrace();
    }
    AudioPlayer.player.start(audioStream);
}
public void start(Stage stage){
    Parent p=createContent();
    Scene scene = new Scene(p, 400, 300);
    stage.setScene(scene);
    stage.setTitle("Chit-Chat");
    stage.show();
}
public Client(String s[]) throws UnsupportedEncodingException, IOException {
    this.str = s;
    name = str[0];
    listeningPort = Integer.parseInt(str[1]);
    server_ip = InetAddress.getByName(str[2]);
    sname = str[3];
    sock = new DatagramSocket();
    byte[] data = new byte[1024];
    data = String.valueOf(str2).getBytes();
    pack = new DatagramPacket(data, data.length, server_ip, 5050);
    sock.send(pack);
    launch(Client.class);
    send = new Thread() {

        public void run() {
            DatagramSocket sock = null;
            try {
                sock = new DatagramSocket();
            } catch (SocketException ex) {
                Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
            }
            while (true) {
                InetAddress host = server_ip;
                try {
                    Scanner input = new Scanner(System.in);
                    String in = input.nextLine();
                    byte[] data = new byte[1024];
                    data = String.valueOf(str).getBytes();
                    DatagramPacket sendPack = new DatagramPacket(data, data.length);
                    sendPack.setPort(5050);
                    sendPack.setAddress(host);
                    sock.send(sendPack);
                } catch (Exception e) {
                    System.out.println(e);
                }
            }
        }

    };
    send.start();
    accept = new Thread() {

        public void run() {
            try {
                sock = new DatagramSocket(listeningPort);
            } catch (SocketException e) {
                e.printStackTrace();
            }
            while (true) {
                byte[] data = new byte[1000];
                pack = new DatagramPacket(data, data.length);
                try {
                    sock.receive(pack);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                String incoming = null;
                try {
                    incoming = new String(data, "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                System.out.println(incoming);

            }
        }
    };
    accept.start();
}

public static void main(String[] args) throws IOException {
    new Client(args);
}

}

错误:

 Exception in Application constructor
 Exception in thread "main" java.lang.RuntimeException: Unable to construct    Application instance: class sample.Client
at   com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:907)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication5(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NoSuchMethodException: sample.Client.<init>()
at java.lang.Class.getConstructor0(Class.java:3082)
at java.lang.Class.getConstructor(Class.java:1825)
at   com.sun.javafx.application.LauncherImpl.lambda$launchApplication11(LauncherImpl.java:818)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait5(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null3(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater4(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null8(WinApplication.java:191)

调用 launch(args) 而不是 new Client(args) 并将所有代码从构造函数移至 start() 方法。您可以使用 getParameters() method. The Application documentation 解释 JavaFX 应用程序的生命周期来访问命令行参数。

未测试(您的问题中有太多无关紧要的问题 material),但这就是想法:

public class Client extends Application{

    Thread send;
    Thread accept;
    DatagramPacket pack;
    DatagramSocket sock;
    private List<String> str;
    String name, sname;
    int listeningPort;
    InetAddress server_ip;
    String sender;

    public Parent createContent(){
        ScrollPane sp = new ScrollPane();
        TextFlow textFlow = new TextFlow();
        textFlow.setPadding(new Insets(10));
        textFlow.setLineSpacing(10);
        TextField textField = new TextField();
        textField.setPrefSize(50,30);
        Button button = new Button("Send");
        button.setPrefSize(80,30);
        Button button2 = new Button("Start");
        button2.setPrefSize(50,30);
        VBox container = new VBox();
        VBox box = new VBox();
        box.getChildren().addAll(sp,textFlow);
        container.setPadding(new Insets(10));
        container.getChildren().addAll(box, new HBox(textField, button,button2));
        VBox.setVgrow(sp, Priority.ALWAYS);
        VBox.setVgrow(textFlow, Priority.ALWAYS);
        return container;
    }

    public void playSound() {
        String gongFile =  "C:\Users\HP\IdeaProjects\FirstGUI\src\sample\Really\Small-bell-jingling.wav";
        InputStream in = null;
        try {
            in = new FileInputStream(gongFile);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        AudioStream audioStream = null;
        try {
            audioStream = new AudioStream(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
        AudioPlayer.player.start(audioStream);
    }
    public void start(Stage stage) throws Exception {

        str=getParameters().getRaw();

        name = str.get(0);
        listeningPort = Integer.parseInt(str.get(1));
        server_ip = InetAddress.getByName(str.get(2));
        sname = str.get(3);
        sock = new DatagramSocket();
        byte[] data = new byte[1024];
        data = String.valueOf(str2).getBytes();
        pack = new DatagramPacket(data, data.length, server_ip, 5050);
        sock.send(pack);

        send = new Thread() {

            public void run() {
                DatagramSocket sock = null;
                try {
                    sock = new DatagramSocket();
                } catch (SocketException ex) {
                    Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
                }
                while (true) {
                    InetAddress host = server_ip;
                    try {
                        Scanner input = new Scanner(System.in);
                        String in = input.nextLine();
                        byte[] data = new byte[1024];
                        data = String.valueOf(str).getBytes();
                        DatagramPacket sendPack = new DatagramPacket(data, data.length);
                        sendPack.setPort(5050);
                        sendPack.setAddress(host);
                        sock.send(sendPack);
                    } catch (Exception e) {
                        System.out.println(e);
                    }
                }
            }

        };
        send.start();
        accept = new Thread() {

            public void run() {
                try {
                    sock = new DatagramSocket(listeningPort);
                } catch (SocketException e) {
                    e.printStackTrace();
                }
                while (true) {
                    byte[] data = new byte[1000];
                    pack = new DatagramPacket(data, data.length);
                    try {
                        sock.receive(pack);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    String incoming = null;
                    try {
                        incoming = new String(data, "UTF-8");
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                    System.out.println(incoming);

                }
            }
        };

        accept.start();
        Parent p=createContent();
        Scene scene = new Scene(p, 400, 300);
        stage.setScene(scene);
        stage.setTitle("Chit-Chat");
        stage.show();
    }


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

}