1

I am using a BorderPane, where the right area is unused. In the center area I have a HBox with a Canvas and another control e.g. a Button. I want the Canvas to have the same width and height with a value of

Canvas width and height = minimum{maximum possible Canvas height, maximum possible Canvas width}

(in other words: Canvas should be a square)

My problem is: How do I determine the maximum width and the maximum height that a Canvas could grow to?

Here is my 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>

Thanks for any hint!

4

2 に答える 2

0

これが行われていることを確認できる最も簡単な方法は、 canvas.getwidth() および canvas.getHeight() です。

于 2013-02-17T16:38:21.403 に答える
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);
        }});
    }
}

キャンバスの問題は、サイズを変更できないことです:(

于 2013-07-25T08:26:09.270 に答える