【问题标题】:Combine 3 JavaFX features into one class on single GUI window在单个 GUI 窗口上将 3 个 JavaFX 功能组合到一个类中
【发布时间】:2015-10-04 22:57:02
【问题描述】:

我正在尝试将 3 个功能组合到一个 JavaFX 类中。我的第一个功能显示“WELCOME TO JAVA”围绕一个圆圈。第二个显示随机 1 和 0 的 10x10 矩阵。第三个显示笑脸。它们应该在一个窗格中一个接一个地显示。我有第一个和第三个功能,但矩阵一个让我失望。尽管所有内容都应该在一个窗格中显示在同一个 GUI 窗口(每个教授)上,但除了创建 gridPane 之外,我看不出我还能如何完成矩阵。它在没有尺寸限制的情况下显示良好,但它占据了整个屏幕,我的其他 2 个功能不可见。当我添加约束时,它变小并且数字不可见。我不确定如何解决这个问题。有人可以帮忙吗?

        Pane pane = new Pane();

        // Create a circle and set its properties
        Circle circle = new Circle();
        circle.setCenterX(100);
        circle.setCenterY(100);
        circle.setRadius(50);
        circle.setStroke(null); 
        circle.setFill(null);
        pane.getChildren().add(circle); // Add circle to the pane

        //Display WELCOME TO JAVA with the text forming a circle
        int i = 0;
        String phrase = "WELCOME TO JAVA ";
        double degree = 360 / phrase.length();
        for (double degrees = 0; i < phrase.length(); i++, degrees += degree) {
            double pointX = circle.getCenterX() + circle.getRadius() *
                Math.cos(Math.toRadians(degrees));
            double pointY = circle.getCenterY() + circle.getRadius() *
                Math.sin(Math.toRadians(degrees));
            Text letter = new Text(pointX, pointY, phrase.charAt(i) + "");
            letter.setFill(Color.BLACK);
            letter.setFont(Font.font("Times New Roman", FontWeight.BOLD, 20));
            letter.setRotate(degrees + 90);
            pane.getChildren().add(letter); }

        //Create a 10x10 matrix of 1s and 0s
        GridPane pane2 = new GridPane();
        pane2.setHgap(1);
        pane2.setVgap(1);

        Button[][] matrix;

        int length = 10;
        int width = 10;

        ArrayList<TextField> textFields = new ArrayList<>();
        for (int y = 0; y < length; y++) {
            ColumnConstraints colConst = new ColumnConstraints();
            colConst.setPercentWidth(10);
            pane2.getColumnConstraints().add(colConst);
            for (int x = 0; x < width; x++) {
                RowConstraints rowConst = new RowConstraints();
                rowConst.setPercentHeight(10);
                pane2.getRowConstraints().add(rowConst);
                Random rand = new Random();
                int random1 = rand.nextInt(2);
                TextField textf = new TextField();
                textf.setText("" + random1);
                textf.setPrefSize(15, 15);
                pane2.setRowIndex(textf,  x);
                pane2.setColumnIndex(textf,  y);
                pane2.getChildren().add(textf);
            }}

                //Create a smiley face
                Circle circle2 = new Circle();
                circle2.setCenterX(600.0f);
                circle2.setCenterY(100.0f);
                circle2.setRadius(50.0f);
                circle2.setStroke(Color.BLACK);
                circle2.setFill(null);

                pane.getChildren().add(circle2);

                Circle leftInnerEye = new Circle();
                    leftInnerEye.setCenterX(580.0f);
                    leftInnerEye.setCenterY(85.0f);
                    leftInnerEye.setRadius(5);
                    leftInnerEye.setStroke(Color.BLACK);
                    pane.getChildren().add(leftInnerEye);

                Ellipse leftOutterEye = new Ellipse();
                    leftOutterEye.setCenterX(580.0f);
                    leftOutterEye.setCenterY(85.0f);
                    leftOutterEye.setRadiusX(11.0f);
                    leftOutterEye.setRadiusY(8.0f);
                    leftOutterEye.setStroke(Color.BLACK);
                    leftOutterEye.setFill(null);
                    pane.getChildren().add(leftOutterEye);

                Circle rightEye = new Circle();
                    rightEye.setCenterX(620.0f);
                    rightEye.setCenterY(85.0f);
                    rightEye.setRadius(5);
                    rightEye.setStroke(Color.BLACK);
                    pane.getChildren().add(rightEye);

                Ellipse rightOutterEye = new Ellipse();
                    rightOutterEye.setCenterX(620.0f);
                    rightOutterEye.setCenterY(85.0f);
                    rightOutterEye.setRadiusX(11.0f);
                    rightOutterEye.setRadiusY(8.0f);
                    rightOutterEye.setStroke(Color.BLACK);
                    rightOutterEye.setFill(null);
                    pane.getChildren().add(rightOutterEye);

                Polygon nose = new Polygon();
                    nose.getPoints().setAll(
                            600d, 90d,
                            588d, 115d,
                            612d, 115d );
                    nose.setStroke(Color.BLACK);
                    nose.setFill(null);
                    pane.getChildren().add(nose);

                Arc mouth = new Arc(600, 115, 30, 16, 180, 180);
                    mouth.setFill(null);
                    mouth.setType(ArcType.OPEN);
                    mouth.setStroke(Color.BLACK);
                    pane.getChildren().add(mouth);

            HBox hbox = new HBox(pane, pane2);
            hbox.autosize();
            hbox.setAlignment(Pos.BASELINE_LEFT);
            hbox.setPadding(new Insets(20));

        // Create a scene and place it in the stage
        Scene scene = new Scene(hbox, 1000, 500);
        primaryStage.setTitle("Laura's Chapter 14"); // Set the stage title
        primaryStage.setScene(scene); // Place the scene in the stage
        primaryStage.show(); // Display the stage
        } 

    catch(Exception e) {
        e.printStackTrace();
    }
}

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

【问题讨论】:

  • 我刚收到教授的回复,说指令不正确。它不应该是单个窗格。应该是分开的。所以我唯一需要帮助的就是正确调整矩阵窗格的大小。
  • 您好,您可以考虑编辑您的问题以反映您的新要求,以便我们更好地帮助您。您可能还想尽可能地浓缩这个问题,因为它非常广泛。你的代码有什么本质的东西给你带来麻烦或你不理解?

标签: javafx panes


【解决方案1】:

为每个要素创建一个窗格,然后将要素添加到 VBoxHBox,这将是场景的根窗格。这样您就可以为您的第二个功能使用GridPane。 JavaFX 中有不同的布局可用,它们的行为也不同。看看这个documentation 及其子文档。

【讨论】:

    猜你喜欢
    • 2019-07-13
    • 2017-08-02
    • 2013-06-28
    • 1970-01-01
    • 2021-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多