gpt4 book ai didi

java - 启动javaFX程序时出错

转载 作者:行者123 更新时间:2023-12-01 09:13:03 24 4
gpt4 key购买 nike

我的客户端类从服务器接收消息并使用 UDP 网络将消息发送到服务器,但我想在名为“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$launchApplication$155(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$launchApplication1$161(LauncherImpl.java:818)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(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$null$148(WinApplication.java:191)

最佳答案

调用 launch(args) 而不是 new Client(args) 并将所有代码从构造函数移动到 start() 方法。您可以使用 getParameters() 访问命令行参数方法。 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);
}

}

关于java - 启动javaFX程序时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40796960/

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