| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1729 人关注过本帖
标题:两段代码有点问题,不知道怎么改.....
只看楼主 加入收藏
渐渐鱼
Rank: 1
等 级:新手上路
帖 子:80
专家分:0
注 册:2018-5-11
结帖率:90%
收藏
已结贴  问题点数:20 回复次数:1 
两段代码有点问题,不知道怎么改.....
程序代码:
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函数里,也会报错....(???)
小伙伴来看一下^_^
搜索更多相关主题的帖子: application java javafx base Source 
2018-09-16 17:04
wlrjgzs
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:26
帖 子:212
专家分:1566
注 册:2017-4-10
收藏
得分:20 
你这个应该使用了第三方库,没办法给你分析
2018-09-18 11:48
快速回复:两段代码有点问题,不知道怎么改.....
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.046334 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved