我的客户端类从服务器接收消息,并使用UDP网络将消息发送到服务器,但我想在名为" chit-chat的窗口中输入输入并显示服务器类别的输入和输出"。但是我不知道该怎么做。我还试图通过构造函数传递启动(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
文档说明了Javafx应用程序的生命周期。
未进行测试(在您的问题中有太多无关紧要的材料),但这是:
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);
}
}