2013-03-05 778 views
41

我有一个只有一个FXML文件的JavaFX应用程序。在这个文件中我有一个AnchorPane,里面有一个StackPane。下面是截图:JavaFX面板内部面板自动调整大小

my panel inside panel application

当我启动这个应用程序,我想与AnchorPane自动调整StackPane。从而; StackPane会自动获得当前的可用宽度和高度。在调整应用程序的大小时,AnchorPane会自动调整大小,但StackPane保持其固定大小。

如何自动调整StackPane的大小并使其在其父面板中完全拉伸?

我的代码

Main.java

package app; 

import javafx.application.Application; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.stage.Stage; 

public class Main extends Application { 

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

    @Override 
    public void start(Stage stage) throws Exception { 
     Parent root = FXMLLoader.load(getClass().getResource("Main.fxml")); 
     Scene scene = new Scene(root,800,600); 
     scene.getStylesheets().add(this.getClass().getResource("/app/style1.css").toExternalForm()); 
     stage.setScene(scene); 
     stage.show(); 
    } 
} 

MainController.java

package app; 

import java.net.URL; 
import java.util.ResourceBundle; 
import javafx.fxml.FXML; 
import javafx.fxml.Initializable; 
import javafx.scene.layout.AnchorPane; 
import javafx.scene.layout.StackPane; 

public class MainController implements Initializable { 

    @FXML 
    private AnchorPane anchorPane; 
    @FXML 
    private StackPane stackPane; 

    @Override 
    public void initialize(URL url, ResourceBundle rb) { 
    stackPane.setPrefSize(anchorPane.getPrefWidth(), anchorPane.getPrefHeight()); //didn't work 
    }  
} 

Main.fxml

<?xml version="1.0" encoding="UTF-8"?> 
<?import java.lang.*?> 
<?import java.util.*?> 
<?import javafx.scene.*?> 
<?import javafx.scene.control.*?> 
<?import javafx.scene.layout.*?> 

<AnchorPane fx:id="anchorPane" xmlns:fx="http://javafx.com/fxml" fx:controller="app.MainController"> 
    <StackPane fx:id="stackPane" ></StackPane> 
</AnchorPane> 

style1.css

#anchorPane { 
    -fx-border-width: 2px; 
    -fx-border-color: chartreuse; 
} 
#stackPane { 
    -fx-border-width: 2px; 
    -fx-border-color: red; 

    /* didn't work */ 
    -fx-hgap: 100%; 
    -fx-vgap: 100%; 
} 

回答

62

经过搜索和测试的时间终于得到了它只是张贴问题了!

可以使用 “AnchorPane.topAnchor,AnchorPane.bottomAnchor,AnchorPane.leftAnchor,AnchorPane.rightAnchor” 具有值 “0.0” FXML命令,以适应/拉伸/对齐AnchorPane中的子元素。所以,这些命令告诉子元素在调整大小时跟随其父元素。

我更新的代码Main.fxml

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

<?import java.lang.*?> 
<?import java.util.*?> 
<?import javafx.scene.*?> 
<?import javafx.scene.control.*?> 
<?import javafx.scene.layout.*?> 

<AnchorPane fx:id="anchorPane" xmlns:fx="http://javafx.com/fxml" fx:controller="app.MainController"> 
    <!--<StackPane fx:id="stackPane" ></StackPane>--> <!-- replace with the following --> 
    <StackPane fx:id="stackPane" AnchorPane.topAnchor="0.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" ></StackPane> 
</AnchorPane> 

海尔结果如下:

enter image description here

对于API文档:http://docs.oracle.com/javafx/2/api/javafx/scene/layout/AnchorPane.html

+2

你能接受这个答案。将让我们知道这个问题有一个解决方案。良好的研究结果,正在寻找这个。 – blo0p3r 2013-03-05 13:42:15

+2

@ blo0p3r我会但现在系统说:“你可以在2天内接受你自己的答案”;)希望有更多的其他建议。 – 2013-03-05 14:32:26

+0

嗨。作为一个按钮被添加。例如。 \t \t \t按钮fx:id =“button”layoutX =“172.0”layoutY =“195.0”onAction =“#ingresarLogin”prefHeight =“35.0”prefWidth =“87.0”text =“Ingresar”/> \t 这是正确的? thanksssss – 2015-02-09 14:28:45

12

我在设计一个SceneBuilder GUI,试图使主容器适应任何窗口大小。它应该始终是100%宽。

在这里,您可以在SceneBuilder设置这些值:

AnchorPane Constraints in SceneBuilder

拨动点/红线实际上只是添加/删除Korki张贴在他的解决方案的属性(AnchorPane.topAnchor等。 )。

+0

这会产生什么FXML代码?我想看看它。 – 2016-03-22 15:52:10

+0

那么你是如何开始SceneBuilder并亲眼看到的......它的完成时间不到20秒... – specializt 2016-05-11 10:04:14

6

另见here

@FXML 
private void mnuUserLevel_onClick(ActionEvent event) { 
    FXMLLoader loader = new FXMLLoader(getClass().getResource("DBedit.fxml")); 
    loader.setController(new DBeditEntityUserlevel()); 

    try { 
      Node n = (Node)loader.load(); 
      AnchorPane.setTopAnchor(n, 0.0); 
      AnchorPane.setRightAnchor(n, 0.0); 
      AnchorPane.setLeftAnchor(n, 0.0); 
      AnchorPane.setBottomAnchor(n, 0.0); 
      mainContent.getChildren().setAll(n); 
    } catch (IOException e){ 
      System.out.println(e.getMessage()); 
    } 
} 

的情况是孩子FXML加载到父AnchorPane。 为了使孩子在符合其父母使用AnChorPane.setxxxAnchor命令拉伸。

0

这很简单,因为您正在使用FXMLBuilder

只要遵循这些简单的步骤:

  1. 打开FXML文件到FXMLBuilder。
  2. 选择堆栈窗格。
  3. 打开布局选项卡[FXMLBuilder的左侧选项卡]。
  4. 在舞台调整大小(如TOP,LEFT,RIGHT,BOTTOM)期间,设置希望计算窗格大小的边值。
0

如果您使用Scene Builder,您将在右侧看到一个通常有三个选项(“属性”,“布局”和“代码”)的手风琴面板。在第二个(“布局”)中,您将看到一个名为“[parent layout] Constraints”的选项(在您的情况下为“AnchorPane Constrainsts”)。

您应该将“0”放在表示父布局的元素的四边。

0

无需放弃。

只是选择窗格右击然后选择适合于母公司

它会自动将窗格大小调整为锚定窗格大小。