2016-11-25 41 views
0

我的客戶端類接收來自服務器的消息,並使用UDP網絡向服務器發送消息,但我想在名爲「Chit-chat」的窗口中輸入並顯示來自服務器類的輸入和輸出。但我不知道該怎麼做。我也嘗試在構造函數中傳遞啓動(Client.class),但它顯示錯誤。啓動javaFX程序出錯

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) 
+0

我不知道JavaFX的,所以這是不是回答的評論,但'引起:java.lang.NoSuchMethodException:sample.Client。 ()'表示框架正在尋找一個零參數(即默認)構造函數來調用,而你沒有提供它。 –

+2

這個例外很清楚。 _'com.sun.javafx.application.LauncherImpl'_正在尋找'Client()'而沒有找到它。 –

+0

我在啓動時添加了一個字符串參數(Client.class,「我的客戶端」),但仍然沒有變化 – IAmBlake

回答

1

呼叫launch(args)而不是new Client(args)和構造將所有的代碼到start()方法。您可以使用getParameters()方法訪問命令行參數。 Application documentation解釋了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); 
    } 

} 
+0

它現在的作品...非常感謝:D – IAmBlake

+1

@IAmBlake請標記答案是正確的。您現在已經提出了13個問題,並沒有將單個答案標記爲正確 - 如果您繼續這樣做,您不可能獲得更多答案。 –

+0

好吧,我新來這裏感謝這個信息 – IAmBlake

相關問題