两段代码有点问题,不知道怎么改.....
程序代码:
package application; import import import import import import import javafx.application.Application; import javafx.event.ActionEvent; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.TextField; import javafx.scene.layout.VBox; import javafx.stage.Stage; public class ChatClient extends Application { Socket socket; PrintWriter out; BufferedReader in; Button b1; TextField tf2; TextField tf1; public void start(Stage stage) throws UnknownHostException, IOException { //图形界面 stage.setTitle("ChatClient"); stage.setWidth(600); stage.setHeight(600); Scene scene = new Scene(new Group()); tf1 = new TextField(); tf1.setEditable(false); //设文本框为只读 tf1.setMinSize(500, 300); tf2 = new TextField(); tf2.setMinSize(500, 100); b1 = new Button("send"); b1.setMinSize(50, 25); b1.setTranslateX(450); VBox vbox = new VBox(); vbox.getChildren().add(tf1); vbox.getChildren().add(tf2); vbox.getChildren().add(b1); ((Group) scene.getRoot()).getChildren().add(vbox); stage.setScene(scene); stage.show(); new GetMes().start(); } class GetMes extends Thread { public GetMes() throws UnknownHostException, IOException { //逻辑组成 socket = new Socket("172.19.212.212",5200); //向服务器发送信息 out = new PrintWriter(socket.getOutputStream()); //读取服务器发送的信息 in = new BufferedReader(new InputStreamReader(socket.getInputStream())); //监听 b1.setOnAction((ActionEvent e)-> { String mes = tf2.getText(); out.println("num1:"+mes); out.flush(); tf2.setText(""); } ); } public void run() { while(this.isAlive()) { try { String inMeg = in.readLine(); if(inMeg!=null) { tf1.appendText(inMeg+"\n"); } } catch (IOException e) { e.printStackTrace(); } } } } public static void main(String []args) { launch(args); } }
new GetMes().start();
socket = new Socket("172.19.212.212",5200);
这两句报错,
运行结果:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.base/java.lang.reflect.Method.invoke(Unknown Source)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(Unknown Source)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Source)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.base/java.lang.reflect.Method.invoke(Unknown Source)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(Unknown Source)
at java.base/java.lang.Thread.run(Unknown Source)
Caused by: Connection refused: connect
at java.base/(Native Method)
at java.base/(Unknown Source)
at java.base/(Unknown Source)
at java.base/(Unknown Source)
at java.base/(Unknown Source)
at java.base/(Unknown Source)
at java.base/(Unknown Source)
at java.base/(Unknown Source)
at java.base/(Unknown Source)
at java.base/(Unknown Source)
at java.base/(Unknown Source)
at application.ChatClient$GetMes.<init>(ChatClient.java:64)
at application.ChatClient.start(ChatClient.java:56)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(Unknown Source)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(Unknown Source)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(Unknown Source)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(Unknown Source)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(Unknown Source)
... 1 more
Exception running application application.ChatClient
线程的start()方法如果放在main函数里,也会报错....(???)
小伙伴来看一下^_^