我创建了一个quizapp,它是一个多客户端服务器应用程序(TCP)。服务器将消息发送到客户端,客户端根据该消息遵循某些指令。这与Swing完美配合。但是我想使用Scene Builder将应用程序实现为JavaFX。
我创建了不同的fxml文件,以便可以拥有不同的场景。
我在Controller类中创建了一个方法(loadUI(String fxmlFile))
,通过按一下按钮就可以交换场景。我已经对其进行了测试,并且可以正常工作。
当我们将消息从服务器发送到客户端(在Controller中)时,消息循环并且场景不会交换。
是否可以在每次调用loadUI时以某种方式实例化控制器类?
这是我的控制器类:
public class Controller implements Runnable {
Socket socket = new Socket("localhost",56565); // 172.20.201.169
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
PrintWriter pw = new PrintWriter(socket.getOutputStream(),true);
Thread thread = new Thread(this);
@FXML
private Button altButton1 = new Button();
@FXML
private Button altButton2 = new Button();
@FXML
private Button altButton3 = new Button();
@FXML
private Button altButton4 = new Button();
@FXML
private Button continueButton = new Button();
List<Button> buttons = new ArrayList<>();
public Controller() throws IOException {
System.out.println("Constructor");
thread.start();
}
public void listOfButtons() {
buttons.add(altButton1);
buttons.add(altButton2);
buttons.add(altButton3);
buttons.add(altButton4);
}
public void handleAnswers(actionEvent event) {
listOfButtons();
Button temp = (Button) event.getsource();
if (event.getsource().equals("Peter")) {
temp.getStyleclass().add("button_right");
} else {
temp.getStyleclass().add("button_wrong");
for (Button button : buttons) {
if (button.getText().equals("Peter")) {
button.getStyleclass().add("button_right");
break;
}
}
}
for (Button button : buttons) {
button.setDisable(true);
button.setOpacity(1);
}
continueButton.setVisible(true);
System.out.println(temp.getText());
}
/**
* Method swaps Scene
*
* @param fxmlFile
* @throws IOException
*/
public void loadUI(String fxmlFile) throws IOException {
Parent root = FXMLLoader.load(getclass().getResource(fxmlFile));
Scene tempScene = new Scene(root);
ObservableList<Window> windows = Stage.getWindows();
for (Window window : windows) {
System.out.println(window);
}
Stage window = (Stage) windows.get(0);
window.setScene(tempScene);
window.show();
}
public void continueToNextQuestion() {
continueButton.setVisible(false);
for (Button button : buttons) {
button.setDisable(false);
button.getStyleclass().remove("button_wrong");
button.getStyleclass().remove("button_right");
}
}
public void continueFromWait() {
try {
loadUI("question.fxml");
} catch (IOException e) {
e.printStackTrace();
}
}
private void showTheMessageFromServer(String message) throws IOException {
if (message.startsWith("Welcome")) {
message = message.substring(message.indexOf(' '));
if (message.contains("1")) {
loadUI("category.fxml");
} else {
loadUI("wait.fxml");
}
} else {
loadUI("question.fxml");
}
}//showTheMessageFromTheServer
@Override
public void run() {
Object obj;
try {
while ((obj = in.readObject()) != null) {
if (obj instanceof Question) {
Question question = (Question) obj;
// showTheQuestion(question);
} else if (obj instanceof String) {
String message = (String) obj;
System.out.println(message);
showTheMessageFromServer(message);
} else if (obj instanceof Integer[]) {
Integer[] points = (Integer[]) obj;
// showThePoints(points);
}
}//while
} catch (IOException e) {
e.printStackTrace();
} catch (ClassnotFoundException e) {
e.printStackTrace();
}
}
}
这是我的主班:
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getclass().getResource("question.fxml"));
primaryStage.setTitle("QuizApp");
primaryStage.setScene(new Scene(root,800,500));
primaryStage.show();
primaryStage.setResizable(false);
}
public static void main(String[] args) {
launch(args);
}
}
输出: 建设者 欢迎:玩家1 建设者 欢迎:玩家2 建设者 欢迎:玩家1 建设者 欢迎:玩家2 建设者 欢迎:玩家1