java - JavaFX 中自动调整 Pane 大小

标签 java intellij-idea javafx-8

我使用 javafx 编写了一个绘制形状的小程序。

我有一个包含所有 UI 控件的 Pane 。 我遇到的问题是,当我放大场景时,我的 Pane 无法调整大小:

enter image description here enter image description here

我绘制形状的 Pane 不会调整大小。当我检查 Pane 大小是否调整时,它返回 true。 另一个问题是,当我通过鼠标单击绘制形状时,我遇到了这个“错误”:

enter image description here

这是我的代码:

package sample;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

import java.util.ArrayList;

public class Main extends Application {

    public Pane Mpane;
    public Button button;
    public Pane pane;
    ArrayList<Circle> circles;

    @Override
    public void start(Stage primaryStage) throws Exception{
        Mpane= FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("GraphFX");
        primaryStage.setScene(new Scene(Mpane, Mpane.getPrefWidth(), Mpane.getPrefHeight()));
        primaryStage.show();

    }


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

    public void ButtonAction(ActionEvent actionEvent) {
    }
    Circle CreateCircle(Double x,Double y){
        Circle circle=new Circle();
        circle.setCenterX(x);
        circle.setCenterY(y);
        circle.setRadius(30);
        return circle;
    }
    public void PaneMouseClick(MouseEvent me){
       circles=new ArrayList<Circle>();
       circles.add(CreateCircle(me.getX(),me.getY()));
        for (Circle circle : circles) {
            pane.getChildren().add(circle);
        }
        System.out.printf("llol"+me.getX()+"Y"+me.getY());

    }
}


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

<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.Pane?>

<Pane fx:id="Mpane" prefHeight="227.0" prefWidth="307.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Main">
   <children>
      <Button fx:id="button" layoutY="2.0" mnemonicParsing="false" onAction="#ButtonAction" text="Button" />
      <AnchorPane fx:id="pane" style="-fx-background-color:white" layoutY="27.0" onMouseClicked="#PaneMouseClick" prefHeight="200.0" prefWidth="307.0" />
   </children>
</Pane>

最佳答案

您可以使用Anchorpane约束

例如:

AnchorPane.setTopAnchor(Mpane, 100.0);
AnchorPane.setLeftAnchor(Mpane, 10.0);
AnchorPane.setRightAnchor(Mpane, 10.0);
AnchorPane.setBottomAnchor(Mpane, 10.0);

关于java - JavaFX 中自动调整 Pane 大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24394048/

相关文章:

git - IntelliJ 和 git,如何忽略 IDEA 中的空白更改?

javafx-2 - 有没有办法在指定时间后隐藏一个阶段?

java - 使用 ObservableMap 填充并同步 ComboBox?

java - 如何使用jvmti获取方法局部变量和类变量的值

java - 如何在oracle adf中显示水平列?

java - 如何在集合操作中使用arrayUnion

grails - 从 IntelliJ 中的 "Navigate Files"中排除项目\文件夹

java - 如何在 WhatsApp 上将图像和文本一起分享给特定收件人?

java - 带有 Java 7 的 OSX 10.8.2 Mt Lion 上的 IntelliJ 12

java - 更新到 java 8 后 ListView 出现问题