我正在使用BorderPane,其中右侧区域未使用。在中心区域,我有一个带有Canvas的HBox和另一个控件,例如一个按钮。我希望Canvas具有相同的宽度和高度,值为
Canvas width and height = minimum{maximum possible Canvas height, maximum possible Canvas width}
(换句话说:画布应该是正方形)
我的问题是:如何确定画布可以增长到的最大宽度和最大高度?
这是我的FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<BorderPane id="BorderPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml">
<bottom>
<Label text="Bottom area" />
</bottom>
<center>
<HBox>
<children>
<Canvas width="300" height="300" />
<Button mnemonicParsing="false" text="some button next to the Canvas" />
</children>
</HBox>
</center>
<left>
<Button mnemonicParsing="false" text="Left area"/>
</left>
<top>
<MenuBar>
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="Close" />
</items>
</Menu>
</menus>
</MenuBar>
</top>
</BorderPane>
感谢任何提示!
答案 0 :(得分:0)
我能看到这件事的最简单方法是:canvas.getwidth()和canvas.getHeight()
答案 1 :(得分:0)
构建GUI后,您必须手动添加画布,以便计算其大小:
public class YourController implements Initializable {
@FXML HBox mHBox;
@FXML Button mButton;
@Override
public void initialize(final URL paramURL, final ResourceBundle paramResourceBundle) {
Platform.runLater(new Runnable() { public void run() {
double w0 = mHBox.getWidth();
double w1 = mButton.getWidth();
double h0 = mHBox.getHeight();
double size = Math.min(w0-w1, h0);
Canvas canvas = new Canvas(size, size);
mHBox.getChildren().add(0, canvas);
}});
}
}
Canvas的问题是不可调整大小:(