执行期间JavaFX窗口是否冻结?

时间:2016-05-21 02:29:05

标签: java javafx

出于某种原因,这段代码无法正常运行。当我运行它时,会出现一个空白窗口,4秒左右后冻结,我必须终止执行。它几乎就像没有完全加载一样;任何可能发生这种情况的原因?

Blank window that appears shortly after execution

以下是我项目的相关代码:

主要课程:

public class Main extends Application {

    public static GUILauncher gl;
    public CommandGUIController cgc;

    @Override
    public void start(Stage primaryStage) {

        startProgram(primaryStage);
    }

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

    public void startProgram(Stage primaryStage){

        gl = new GUILauncher(primaryStage);

        cgc = new CommandGUIController();
        gl.showGUI("../view/CommandGUI.fxml", "../view/application.css", "JavaChat Client", cgc);

        println("Welcome to JavaChat");
        println("Server or Client mode? (Type \"1\" for Server, \"2\" for Client)");
        boolean isGoodInput = false;
        while(!isGoodInput){
            String in = sc.nextLine();
            if(in.equals("1")){
                isGoodInput = true;
                gl.showServerGUI();
            }
            if(in.equals("2")){
                isGoodInput = true;
                gl.showClientGUI();
            }
            if(!(in.equals("1")||(in.equals("2")))){
                System.out.println("Please try again... must be a number between 1 and 2 inclusively.");
            }
        }


    }

    public void println(String str){
        cgc.writeToOutput(str);
    }
}

GUILauncher类:

public class GUILauncher {

    private Stage primaryStage;

    public GUILauncher(Stage inS){
         primaryStage = inS;
    }    

    public void showGUI(String FXMLpath, String csspath, String title, Object controller){

        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource(FXMLpath));               
            loader.setController(controller);
            Parent root = (Parent) loader.load();
            Scene scene = new Scene(root);

            scene.getStylesheets().add(getClass().getResource(csspath).toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.setTitle(title);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }


}

最后,CommandGUIController:

public class CommandGUIController implements Initializable, InputController {

    private String input;

    @FXML
    private TextArea outputArea;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        // TODO Auto-generated method stub

    }

    @FXML
    public void enterInput(KeyEvent e){
        if(e.getCode() == KeyCode.ENTER){
            input = outputArea.getText().substring(outputArea.getText().lastIndexOf("\n") + 1);
            //System.out.println(input);
        }
    }

    public void writeToOutput(String str){
        outputArea.appendText(str + "\n");
    }

    public String getInput(){
        return input;
    }

    public void setInput(String strInput){
        input = strInput;
    }
}

提前致谢!

编辑:CommandGUI.fxml按要求:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.TextArea?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <GridPane layoutX="114.0" layoutY="110.0" prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
        <columnConstraints>
          <ColumnConstraints hgrow="SOMETIMES" maxWidth="40.0" minWidth="10.0" prefWidth="100.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
          <ColumnConstraints hgrow="SOMETIMES" maxWidth="40.0" minWidth="10.0" prefWidth="100.0" />
        </columnConstraints>
        <rowConstraints>
          <RowConstraints maxHeight="50.0" minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
          <RowConstraints maxHeight="50.0" minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
        </rowConstraints>
         <children>
            <TextArea fx:id="outputArea" onKeyPressed="#enterInput" prefHeight="200.0" prefWidth="200.0" wrapText="true" GridPane.columnIndex="1" GridPane.rowIndex="1" />
         </children>
      </GridPane>
   </children>
</AnchorPane>

EDIT2(重要):经过一些测试后,我想我意识到我最初遗漏的主要代码与冻结有关。该程序挂在sc.nextLine(),正在等待输入。

不管;我还有这个问题;即使我在主要的gl.showGUI()中显示它,GUI也显示为完全空白。我决定尝试更多的测试,并从代码中消除循环,最后GUI最终加载;几乎看起来它必须先解决才能看到它?然而,这没有任何意义,GUI应该在println()代码运行之前加载,以便我可以与它进行交互。为什么GUI会出现在最后?我该如何解决这个问题,我是否必须运行一个单独的线程,或者是否有一个我可以调用的方法会强制它加载到下一行之前?再次感谢您的阅读

0 个答案:

没有答案