我对JavaFX相当陌生,我正在学习如何在调整大小的同时操作窗口。我的目标是拥有一个包含4个子窗口的窗口,这些子窗口在调整主窗口大小的同时调整大小。
随附2个最小,完整,可验证的测试。第一个mcv1使用Java创建窗口,第二个mcv2使用通过Scene Builder创建的fxml。这些都是使用Eclipse开发的,需要从那里运行。
使用mcv1时,窗口大小正确(请参见outputmcv1.jpg),而mcv2时,窗口将停止调整大小(请参见outputmcv2.jpg)。子窗口停止扩展。
真正的问题是:FXML中是否存在阻止节点扩展的内容?我希望使用FXML,以便获得多语言支持,但是该实验可以使我转向本机Java代码。
outputmcv1.jpg
代码文件:Mcv1
Mcv1.java
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class Mcv1 extends Application {
public Mcv1() {
}
@Override
public void start(Stage stage) throws Exception {
Group root = new Group();
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("borders.css").toExternalForm());
GridPane grid = new GridPane();
AnchorPane menu = new AnchorPane();
MenuBar bar = new MenuBar();
Menu fileMenu = new Menu("File");
bar.getMenus().add(fileMenu);
menu.getChildren().add(bar);
AnchorPane leftTop = new AnchorPane();
leftTop.getStyleClass().add("red-border");
AnchorPane leftBottom = new AnchorPane();
leftBottom.getStyleClass().add("yellow-border");
AnchorPane rightTop = new AnchorPane();
rightTop.getStyleClass().add("blue-border");
AnchorPane rightBottom = new AnchorPane();
rightBottom.getStyleClass().add("green-border");
root.getChildren().add(grid);
grid.add(menu, 0, 0);
grid.add(leftTop,0,1);
grid.add(leftBottom,0,2);
grid.add(rightTop,1,1);
grid.add(rightBottom,1,2);
// Creating a scene object
scene.widthProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observableValue, Number oldSceneWidth,Number newSceneWidth) {
Double widthD = newSceneWidth.doubleValue();
leftTop.setPrefWidth(widthD*0.4);
rightTop.setPrefWidth(widthD*0.6);
leftBottom.setPrefWidth(widthD*0.4);
rightBottom.setPrefWidth(widthD*0.6);
leftTop.setMaxWidth(widthD*0.4);
rightTop.setMaxWidth(widthD*0.6);
leftBottom.setMaxWidth(widthD*0.4);
rightBottom.setMaxWidth(widthD*0.6);
}
});
scene.heightProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observableValue, Number oldSceneHeight,Number newSceneHeight) {
Double heightD = newSceneHeight.doubleValue();
leftTop.setPrefHeight(heightD*0.6);
rightTop.setPrefHeight(heightD*0.6);
leftBottom.setPrefHeight(heightD*0.4);
rightBottom.setPrefHeight(heightD*0.4);
leftTop.setMaxHeight(heightD*0.6);
rightTop.setMaxHeight(heightD*0.6);
leftBottom.setMaxHeight(heightD*0.4);
rightBottom.setMaxHeight(heightD*0.4);
}
});
// Setting title to the Stage
stage.setTitle("Jungle Finance");
// Adding scene to the stage
stage.setScene(scene);
//Displaying the contents of the stage
stage.show();
} public static void main(String args[]){
launch();
}
}
boders.css
red-border {
-fx-background-color:red;
}
.green-border {
-fx-background-color:green;
}
.yellow-border {
-fx-background-color:yellow;
}
.blue-border {
-fx-background-color:blue;
}
Mcv2代码 Mcv2.java
package com.mcv2;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Mcv2 extends Application {
public Mcv2() {
}
@Override
public void start(Stage stage) throws Exception {
FXMLLoader loader = new FXMLLoader(getClass().getResource("MainScreen.fxml"));
Parent root = loader.load();
ScreenController controller = loader.getController();
Scene scene = new Scene(root);
// Creating a scene object
scene.widthProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observableValue, Number oldSceneWidth,Number newSceneWidth) {
controller.setWidth(newSceneWidth);
}
});
scene.heightProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observableValue, Number oldSceneHeight,Number newSceneHeight) {
controller.setHeight(newSceneHeight);
}
});
// Setting title to the Stage
stage.setTitle("Jungle Finance");
// Adding scene to the stage
stage.setScene(scene);
//Displaying the contents of the stage
stage.show();
}
public static void main(String args[]){
launch();
}
}
ScreenController.java
package com.mcv2;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.MenuBar;
import javafx.scene.layout.AnchorPane;
public class ScreenController implements Initializable {
@FXML
private MenuBar menuBar;
@FXML
private AnchorPane leftTop;
@FXML
private AnchorPane leftBottom;
@FXML
private AnchorPane rightTop;
@FXML
private AnchorPane rightBottom;
public ScreenController() {
}
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
}
@FXML
private void window2Resized() {
}
@FXML
private void fileOpenMenu () {
}
public void setWidth(Number width) {
Double widthD = width.doubleValue();
leftTop.setPrefWidth(widthD*0.4);
rightTop.setPrefWidth(widthD*0.6);
leftBottom.setPrefWidth(widthD*0.4);
rightBottom.setPrefWidth(widthD*0.6);
leftTop.setMaxWidth(widthD*0.4);
rightTop.setMaxWidth(widthD*0.6);
leftBottom.setMaxWidth(widthD*0.4);
rightBottom.setMaxWidth(widthD*0.6);
menuBar.setPrefWidth(widthD);
}
public void setHeight(Number height) {
Double heightD = height.doubleValue();
leftTop.setPrefHeight(heightD*0.6);
rightTop.setPrefHeight(heightD*0.6);
leftBottom.setPrefHeight(heightD*0.4);
rightBottom.setPrefHeight(heightD*0.4);
leftTop.setMaxHeight(heightD*0.6);
rightTop.setMaxHeight(heightD*0.6);
leftBottom.setMaxHeight(heightD*0.4);
rightBottom.setMaxHeight(heightD*0.4);
}
}
MainScreen.fxml
?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<GridPane fx:id="pane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" onDragDetected="#window2Resized" onDragDone="#window2Resized" onDragDropped="#window2Resized" onDragEntered="#window2Resized" onDragExited="#window2Resized" onDragOver="#window2Resized" onMouseDragEntered="#window2Resized" onMouseDragExited="#window2Resized" onMouseDragOver="#window2Resized" onMouseDragReleased="#window2Resized" onTouchMoved="#window2Resized" onTouchPressed="#window2Resized" onTouchReleased="#window2Resized" onTouchStationary="#window2Resized" prefHeight="507.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.mcv2.ScreenController">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="294.0" minWidth="10.0" prefWidth="140.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="460.0" minWidth="10.0" prefWidth="460.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="249.0" minHeight="10.0" prefHeight="54.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="403.0" minHeight="10.0" prefHeight="375.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="479.0" minHeight="10.0" prefHeight="105.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<AnchorPane prefHeight="200.0" prefWidth="200.0" GridPane.columnSpan="2">
<children>
<MenuBar fx:id="menuBar" layoutY="2.0" prefHeight="25.0" prefWidth="346.0">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem fx:id="fileOpen" mnemonicParsing="false" onAction="#fileOpenMenu" onMenuValidation="#fileOpenMenu" text="Open" />
<MenuItem mnemonicParsing="false" text="Save" />
<MenuItem mnemonicParsing="false" text="Save As" />
<MenuItem mnemonicParsing="false" text="Close" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
<items>
<MenuItem mnemonicParsing="false" text="Delete" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Window">
<items>
<MenuItem mnemonicParsing="false" text="New" />
<MenuItem mnemonicParsing="false" text="Close" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="Index" />
<MenuItem mnemonicParsing="false" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
</children>
</AnchorPane>
<AnchorPane fx:id="leftTop" prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: red;" GridPane.rowIndex="1" />
<AnchorPane fx:id="rightTop" prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: blue;" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<AnchorPane fx:id="leftBottom" prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: yellow;" GridPane.rowIndex="2" />
<AnchorPane fx:id="rightBottom" prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: green;" GridPane.columnIndex="1" GridPane.rowIndex="2" />
</children>
</GridPane>
最后一个问题。我将所有带有Eclipse设置的代码保存在一个zip文件中,但无法解决如何发布它的问题。有什么想法吗?
答案 0 :(得分:0)
我已手动删除了由Scene Builder生成的列和行约束。子窗口现在可以正确展开。