我有下面的示例代码FXMLjavafx8调整大小的增长永远不会收缩
<?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();
}
}
工作得非常符合我的预期,除了放大的窗口只有这么远,我不能缩小我t,完全可以。我向vbox和hbox添加了一个更改监听器,它们在增长和缩小方面都能正确执行。
我想要固定在一个轴上的两个刻度,HBox包含一个VBox和一个可调整的范围,如FXControl中所述。它有一个固定的宽度,它的高度与stackpane,一个VBox和另一个ResizableCanvas的父级的高度配对。
在当前的fxml文件中,我认为堆叠面板的宽度看起来很好,我可以将其收缩得很好,但高度永远不会超过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>
我希望这一切都有道理。很明显,我缺少一些东西。
谢谢!
“继承”在[JavaFX CSS文档](http://docs.oracle.com/javase/8/javafx/api/javafx/scene/doc-files/cssref.html#typeinherit) –