如何从javaFX中的窗口传递输入?

时间:2016-11-24 06:41:46

标签: java css javafx client-server

在我的聊天信使中,我创建了一个新类ChatBox来包含所有UI内容

import javafx.application.Application;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.scene.text.TextFlow;
import javafx.stage.Stage;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class ChatBox extends Application {
    final ScrollPane sp = new ScrollPane();

    public static void main(String[] args){
        launch(args);
    }

    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);
    }

    @Override
    public void start(Stage stage){
        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);
        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));
        VBox.setVgrow(sp, Priority.ALWAYS);
        VBox.setVgrow(textFlow, Priority.ALWAYS);

        textField.prefWidthProperty().bind(container.widthProperty().subtract(button.prefWidthProperty()));

        // On Enter press
        textField.setOnKeyPressed(e -> {
            if(e.getCode() == KeyCode.ENTER) {
                playSound();
                button.fire();
            }
        });

        button.setOnAction(e -> {
            Text text;
            if(textFlow.getChildren().size()==0){
                text = new Text(textField.getText());
            } else {
                text = new Text("\n" + textField.getText());
            }
            textFlow.getChildren().add(text);
            textField.clear();
            textField.requestFocus();
        });
        VBox vb = new VBox();
        vb.getChildren().addAll(textFlow);
        sp.setVmax(440);
        sp.setPrefSize(400, 300);
        sp.setContent(vb);
        sp.vvalueProperty().bind((ObservableValue<? extends Number>) vb.heightProperty());
        Scene scene = new Scene(container, 400, 300);
        stage.setScene(scene);
        stage.setTitle("jui");
        stage.show();
    }
}

我把我的普通代码客户端和服务器放在两个单独的类中。但是我需要从ChatBox类打开的窗口获取输入并将其传递给我的Client类。

这是我的Client类的发送线程,它通过Server向另一个客户端发送消息。

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 = in.getBytes();
                DatagramPacket sendPack = new DatagramPacket(data, data.length);
                sendPack.setPort(5050);
                sendPack.setAddress(host);
                sock.send(sendPack);
            } catch (Exception e) {
                System.out.println(e);
            }
        }
    }

};

这是我接收来自服务器

的数据的Client类的接受线程
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(friend+" says: "+incoming);
        }
    }
};

我该怎么做?

0 个答案:

没有答案