Javafx 8 JavaFX while(true)循环冻结应用程序
我有点问题,整晚我都在绞尽脑汁。这个应用程序作为Java.swing应用程序运行得很好,但不幸的是,对于JavaFX和Java8的转换,事情变得有点棘手。对话框当然翻译得不好,但我得到了解决。我的头在while(真实)循环中跳动。它通过循环接收来自服务器的提交和接受消息,但在此之后冻结。我已经试过System.out.println();整个程序的服务器端和客户端,并将问题追溯到该点。有什么想法吗?我最初没有线程,但有同样的问题。我将while(true)循环更改为while(I<1),将消息else if语句移动到它自己的while(true)if语句中,因为它是唯一需要循环多次的线程 客户端文件Javafx 8 JavaFX while(true)循环冻结应用程序,javafx-8,Javafx 8,我有点问题,整晚我都在绞尽脑汁。这个应用程序作为Java.swing应用程序运行得很好,但不幸的是,对于JavaFX和Java8的转换,事情变得有点棘手。对话框当然翻译得不好,但我得到了解决。我的头在while(真实)循环中跳动。它通过循环接收来自服务器的提交和接受消息,但在此之后冻结。我已经试过System.out.println();整个程序的服务器端和客户端,并将问题追溯到该点。有什么想法吗?我最初没有线程,但有同样的问题。我将while(true)循环更改为while(I
package application;
import java.awt.TextArea;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.stage.Stage;
public class Client implements Runnable {
@FXML
public static
TextArea InputTA;
@FXML
public static
TextArea MainTA;
public static BufferedReader clientIn;
public static PrintWriter clientOut;
int i = 1;
static String readLine;
@FXML
public void run(){
/*
* @ALambert
*
* Here we initialize the socket, connect to the server we desire and setup
* our BufferedReader and PrintWriter
*
*/
final int PORT = 8000;
String serverAddress = ServerIPPopup.serverIP;
try {
Socket socket = new Socket(serverAddress, PORT);
System.out.println("Server Connected");
clientIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
System.out.println("Buffered Reader Set");
clientOut = new PrintWriter(socket.getOutputStream(), true);
System.out.println("Print Writer Set");
/*
* @Andrew Lambert
*
* In this section we set our Screen Name as well
* as start relaying messages to the server
*
*/
while (true) {
String readLine = clientIn.readLine();
System.out.println("readLine Set");
if (readLine.startsWith("NAME")) {
ThreadClass.submittedThread st = new ThreadClass.submittedThread();
st.start();
}else if (readLine.startsWith("ACCEPTED")) {
ThreadClass.acceptedThread at = new ThreadClass.acceptedThread();
at.start();
continue;
}else if (readLine.startsWith("MESSAGE")){
ThreadClass.messageThread mt = new ThreadClass.messageThread();
mt.start();
}
}
} catch (UnknownHostException e) {
System.out.println("UnknownHostException");
//e.printStackTrace();
}catch(SocketTimeoutException s){
System.out.println("SocketTimeoutException");
//s.printStackTrace();
} catch (SocketException e) {
System.out.println("SocketException");
// TODO Auto-generated catch block
//e.printStackTrace();
// } catch (InterruptedException e) {
// System.out.println("InterruptedException");
// TODO Auto-generated catch block
//e.printStackTrace();
} catch (IOException e) {
System.out.println("IOException");
// TODO Auto-generated catch block
//e.printStackTrace();
}
/*
* @ALambert
*
*
*/
}
}
服务器文件
package application;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashSet;
public class Server {
public static final int PORT = 8000;
public static HashSet<String> clientNames = new HashSet<String>();
public static HashSet<PrintWriter> writers = new HashSet<PrintWriter>();
public static void main(String[] args) throws Exception {
System.out.println("The chat server is running.");
ServerSocket listener = new ServerSocket(PORT);
try {
while (true) {
new Handler(listener.accept()).start();
System.out.println("The chat server recieved a geust");
}
} finally {
listener.close();
System.out.println("The chat server closed");
}
}
public static class Handler extends Thread {
public String clientName;
public Socket socket;
public BufferedReader serverIn;
public PrintWriter serverOut;
public Handler(Socket socket) {
this.socket = socket;
}
public void run() {
try {
serverIn = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
System.out.println("Buffered Reader Set");
serverOut = new PrintWriter(socket.getOutputStream(), true);
System.out.println("Print Writer Set");
while (true) {
serverOut.println("NAME");
clientName = serverIn.readLine();
if (clientName == null) {
return;
}
synchronized (clientNames) {
if (!clientNames.contains(clientName)) {
clientNames.add(clientName);
System.out.println(clientName + " was added to the list");
break;
}
}
}
serverOut.println("ACCEPTED");
writers.add(serverOut);
System.out.println("Name: " + clientName + " was Accepted");
while (true) {
String clientInput = serverIn.readLine();
if (clientInput == null) {
return;
}
for (PrintWriter writer : writers) {
System.out.println("message " + clientInput + " recieved from " + clientName);
writer.println("MESSAGE " + clientName + ": " + clientInput);
System.out.println("message " + clientInput + " sent to " + clientName);
}
}
} catch (IOException e) {
System.out.println(e);
} finally {
if (clientName != null) {
clientNames.remove(clientName);
}
if (serverOut != null) {
writers.remove(serverOut);
}
try {
socket.close();
} catch (IOException e) {
}
}
}
}
}
静态空隙总管
package application;
import java.io.IOException;
import java.util.Optional;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Dialogs;
import javafx.scene.control.TextInputDialog;
import javafx.stage.Stage;
/*
*
*
*
*
*
*/
public class Main extends Application {
@Override
public void start(Stage stage) throws Exception {
try{
Parent root = FXMLLoader.load(getClass().getResource("IRCFXML.fxml"));
stage.setTitle("CO-OP IRC Project");
stage.setScene(new Scene(root, 450, 450));
stage.show();
} catch (IOException e) {
System.err.println("Error loading IRCFXML.fxml!");
e.printStackTrace();
System.exit(0);
}
}
public static void main(String[] args) throws IOException{
launch(args);
}
}
IRC控制器
package application;
import java.io.IOException;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextInputDialog;
import java.util.Optional;
import javafx.application.Application;
import javafx.stage.Stage;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.net.ConnectException;
import java.net.Socket;
import java.net.UnknownHostException;
import javafx.fxml.LoadException;
public class IRCController{
@FXML
public
TextArea MainTA;
@FXML
public
TextArea InputTA;
@FXML
public
Button reset;
@FXML
public
Button handlerbutton;
@FXML
public
Button submit;
@FXML
public
Button serverButton;
@FXML
public
Button screenNameButton;
@FXML
private void initialize() throws Exception{
InputTA.textProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable,
String oldValue, String newValue) {
}
});
MainTA.textProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable,
String oldValue, String newValue) {
}
});
}
@FXML
public void handleSubmitButtonAction(){
String clientOutString = InputTA.getText();
Client.clientOut.println(clientOutString);
InputTA.setText("");
}
@FXML
public void handleServerButton(){
ServerIPPopup sipp = new ServerIPPopup();
sipp.run();
}
@FXML
public void handleScreenNameButton(){
ScreenNamePopup scpp = new ScreenNamePopup();
scpp.run();
}
}
包应用;
导入java.io.IOException;
导入javafx.beans.value.ChangeListener;
导入javafx.beans.value.observeValue;
导入javafx.fxml.fxml;
导入javafx.scene.control.Button;
导入javafx.scene.control.TextArea;
导入javafx.scene.control.TextInputDialog;
导入java.util.Optional;
导入javafx.application.application;
导入javafx.stage.stage;
导入java.io.BufferedReader;
导入java.io.InputStreamReader;
导入java.io.PrintWriter;
导入java.lang.reflect.InvocationTargetException;
导入java.net.ConnectException;
导入java.net.Socket;
导入java.net.UnknownHostException;
导入javafx.fxml.LoadException;
公共类控制器{
@FXML
平民的
文本区域维护;
@FXML
平民的
文本区域输入a;
@FXML
平民的
按钮复位;
@FXML
平民的
按钮手柄按钮;
@FXML
平民的
按钮提交;
@FXML
平民的
按钮服务器按钮;
@FXML
平民的
按钮屏幕名称按钮;
@FXML
private void initialize()引发异常{
InputA.textProperty().addListener(新的ChangeListener()){
@凌驾
public void已更改(observeValue转储太多,信息太少。while(true)
冻结您的应用程序,因为JavaFX应用程序线程被阻塞。您需要使用线程,以防需要连续执行某些后台任务。StackOverflow不是代码审阅站点,但此代码存在许多失败。应用程序不应实现可运行。应用程序不应是控制器。单独的Tho将关注点转换为单独的文件。应用程序应显示start方法中提供的阶段。类名应紧跟其后。永远不要休眠JavaFX应用程序线程。永远不要忙于等待JavaFX应用程序线程。永远不要在JavaFX线程下修改JavaFX场景图。还有其他问题。感谢您的建议。这就是我们学习的方式n毕竟。我对这一点当然是新手,虽然我是新手,但我并不羞于研究、研究和更多的研究,直到我能找到问题的解决方案。不幸的是,我的研究主要是提出了一些过时的摇摆例子。因此,这是我克服困难的一堵砖墙。我为此感到自豪让这个项目走到正题上。但我并不太骄傲,因为当它来到我身边时,我会寻求帮助或建议。
package application;
import java.io.IOException;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextInputDialog;
import java.util.Optional;
import javafx.application.Application;
import javafx.stage.Stage;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.net.ConnectException;
import java.net.Socket;
import java.net.UnknownHostException;
import javafx.fxml.LoadException;
public class IRCController{
@FXML
public
TextArea MainTA;
@FXML
public
TextArea InputTA;
@FXML
public
Button reset;
@FXML
public
Button handlerbutton;
@FXML
public
Button submit;
@FXML
public
Button serverButton;
@FXML
public
Button screenNameButton;
@FXML
private void initialize() throws Exception{
InputTA.textProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable,
String oldValue, String newValue) {
}
});
MainTA.textProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable,
String oldValue, String newValue) {
}
});
}
@FXML
public void handleSubmitButtonAction(){
String clientOutString = InputTA.getText();
Client.clientOut.println(clientOutString);
InputTA.setText("");
}
@FXML
public void handleServerButton(){
ServerIPPopup sipp = new ServerIPPopup();
sipp.run();
}
@FXML
public void handleScreenNameButton(){
ScreenNamePopup scpp = new ScreenNamePopup();
scpp.run();
}
}
package application;
import java.util.Optional;
import javafx.application.Application;
import javafx.scene.control.TextInputDialog;
import javafx.stage.Stage;
public class ServerIPPopup implements Runnable{
public static String serverIP;
public void run(){
/*
* @ALambert
*
* dialog popup to gather the ip address of the server you wish to connect to.
*
*
*/
TextInputDialog dialog = new TextInputDialog("localhost");
dialog.setTitle("Server IP");
dialog.setHeaderText("Server IP");
dialog.setContentText("Please enter the Server IP");
Optional<String> result = dialog.showAndWait();
if (result.isPresent()){
System.out.println("Client Side-Server IP " + result.get());
serverIP = result.get();
ScreenNamePopup scp = new ScreenNamePopup();
scp.run();
}
}
}
package application;
import java.util.Optional;
import javafx.application.Application;
import javafx.scene.control.TextInputDialog;
import javafx.stage.Stage;
public class ScreenNamePopup implements Runnable{
public static String getName;
public void run(){
/*
* @ALambert
*
* popup dialog box to gather the screen name you wish to use for the chat session
*
*/
TextInputDialog dialogScreenName = new TextInputDialog("Screen Name");
dialogScreenName.setTitle("Screen Name");
dialogScreenName.setHeaderText("Screen Name");
dialogScreenName.setContentText("Choose a screen name:");
Optional<String> resultScreenName = dialogScreenName.showAndWait();
if (resultScreenName.isPresent()){
System.out.println("Client Side-Screen Name: " + resultScreenName.get());
getName = resultScreenName.get();
Client client = new Client();
client.run();
}
}
}