javafx8调整大小永远不会缩小

时间:2016-03-11 19:44:31

标签: resize javafx-8

我有以下示例代码fxml

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

<?import javafx.scene.*?>
<?import javafx.scene.canvas.*?>
<?import org.cornova.javafx.*?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<SplitPane dividerPositions="0.5" maxHeight="Infinity" maxWidth="Infinity" minHeight="0" minWidth="0" orientation="VERTICAL" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="org.cornova.javafx.PanController">
  <items>
      <HBox prefHeight="246.0" prefWidth="603.0">
         <children>
            <VBox HBox.hgrow="ALWAYS">
               <children>
                  <StackPane fx:id="stackPane" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="165.0" prefWidth="570.0">
                     <children>
                        <ResizableCanvas fx:id="spectrum" height="195.0" width="573.0" StackPane.alignment="TOP_LEFT" />
                     </children>
                  </StackPane>
                  <ResizableCanvas fx:id="freqScale" height="10.0" width="570.0" VBox.vgrow="NEVER" />
               </children>
            </VBox>
            <ResizableCanvas fx:id="dbmScale" height="195.0" width="30.0" HBox.hgrow="NEVER" />
         </children>
      </HBox>
    <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0" />
  </items>
</SplitPane>

我将其与以下控制器相结合

/*
 * Here comes the text of your license
 * Each line should be prefixed with  * 
 */
package org.cornova.javafx;

/**
 * Sample Skeleton for 'PanView.fxml' Controller Class
 */

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.scene.layout.StackPane;
import org.cornova.javafx.ResizableCanvas;

public class PanController {

    @FXML // ResourceBundle that was given to the FXMLLoader
    private ResourceBundle resources;

    @FXML // URL location of the FXML file that was given to the FXMLLoader
    private URL location;

    @FXML // fx:id="stackPane"
    private StackPane stackPane; // Value injected by FXMLLoader

    @FXML // fx:id="spectrum"
    private ResizableCanvas spectrum; // Value injected by FXMLLoader

    @FXML // fx:id="freqScale"
    private ResizableCanvas freqScale; // Value injected by FXMLLoader

    @FXML // fx:id="dbmScale"
    private ResizableCanvas dbmScale; // Value injected by FXMLLoader

    @FXML // This method is called by the FXMLLoader when initialization is complete
    void initialize() {
        assert stackPane != null : "fx:id=\"stackPane\" was not injected: check your FXML file 'PanView.fxml'.";
        assert spectrum != null : "fx:id=\"spectrum\" was not injected: check your FXML file 'PanView.fxml'.";
        assert freqScale != null : "fx:id=\"freqScale\" was not injected: check your FXML file 'PanView.fxml'.";
        assert dbmScale != null : "fx:id=\"dbmScale\" was not injected: check your FXML file 'PanView.fxml'.";

        spectrum.widthProperty().bind(stackPane.widthProperty());
        spectrum.heightProperty().bind(stackPane.heightProperty());
        freqScale.widthProperty().bind(stackPane.widthProperty());
        dbmScale.heightProperty().bind(stackPane.heightProperty());
        spectrum.widthProperty().addListener((observable, oldValue, newValue) -> {
            System.out.println("spectrum width changed from " + oldValue + " to " + newValue);
        });
        spectrum.heightProperty().addListener((observable, oldValue, newValue) -> {
            System.out.println("spectrum height changed from " + oldValue + " to " + newValue);
        });
        freqScale.widthProperty().addListener((observable, oldValue, newValue) -> {
            System.out.println("Freq scale width changed from " + oldValue + " to " + newValue);
        });
        dbmScale.heightProperty().addListener((observable, oldValue, newValue) -> {
            System.out.println("dbm scale height changed from " + oldValue + " to " + newValue);
        });

        stackPane.widthProperty().addListener((observable, oldValue, newValue) -> {
            System.out.println("stackpane width changed from " + oldValue + " to " + newValue);
        });
        stackPane.heightProperty().addListener((observable, oldValue, newValue) -> {
            System.out.println("stackpane height changed from " + oldValue + " to " + newValue);
        });
    }
}

考虑到这两个,我记得,我的示例应用程序

package org.cornova.portablesdr;

import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.SplitPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

/**
 *
 * @author walt
 */
public class PanadapterView extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        try {
            URL url = getClass().getResource("/PanView.fxml");
            SplitPane root = (SplitPane)FXMLLoader.load(url);
            Scene scene = new Scene(root, 600,400);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch (Exception e) {
            Logger.getLogger(PanadapterView.class.getName()).log(Level.SEVERE, null, e);
        }
    }

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

的工作方式与我的预期非常相似,只是窗口的放大只是到目前为止,我根本无法收缩它。我为vbox和hbox添加了一个更改侦听器,它们在增长和收缩方面都能正常运行。

我希望在一个轴上固定两个刻度,HBox包含一个VBox和一个resizableCanvas,如FXControl中所述。它具有固定的宽度,其高度与堆栈窗格的高度配对,子VBox和另一个ResizableCanvas的父级。

在当前的fxml文件中,我相信stackpane的宽度似乎很好,我可以增长和缩小它,但高度永远不会超过200,我被迫用于prefHeight。但是,它可以覆盖prefWidth就好了。

此时我发现自己只是猜测下一步要改变什么。我真的一直试图研究这个问题。到目前为止,没有运气,显然。 这是最终的fxml文件。

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<SplitPane dividerPositions="0.5" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="0" minWidth="0" orientation="VERTICAL" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="org.cornova.javafx.PanController">
  <items>
      <HBox fx:id="hbox" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="0.0" minWidth="0.0">
         <children>
            <VBox fx:id="vbox" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="0.0" minWidth="0.0" prefHeight="170.0" prefWidth="570.0" HBox.hgrow="ALWAYS">
               <children>
                  <StackPane fx:id="stackPane" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="0.0" minWidth="0.0" prefHeight="200.0" prefWidth="570.0">
                     <children>
                        <ResizableCanvas fx:id="spectrum" />
                     </children>
                  </StackPane>
                  <ResizableCanvas fx:id="freqScale" height="30.0" width="570.0" VBox.vgrow="NEVER" />
               </children>
            </VBox>
            <ResizableCanvas fx:id="dbmScale" height="200.0" width="30.0" HBox.hgrow="NEVER" />
         </children>
      </HBox>
    <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0" />
  </items>
</SplitPane>

我希望这一切都有道理。很明显,我有些遗漏。

谢谢!

1 个答案:

答案 0 :(得分:0)

答案是Stackpane(在SceneBuilder中)有一个继承的Vgrow设置,我无法在任何地方找到解释,它似乎不是优先级枚举中的值。

如果有人能够阐明继承我的目的,我相信其他人会很感激。