简体   繁体   English

JavaFX 2窗格动态调整大小

[英]JavaFX 2 Pane Dynamic Resizing

I have a Pane within my interface. 我的界面中有一个窗格。 On that Pane I add Shape objects and drag them around. 在该窗格上,我添加了Shape对象并拖动它们。 I what to make my Pane bigger when one Shape reaches the visual bounds of the Pane. 当一个Shape到达窗格的可视范围时,我该如何使窗格更大。 As a prototype, I try to make it bigger whenever I click my mouse. 作为原型,每当我单击鼠标时,我都会尝试使其更大。 The Pane is child (within) a ScrollPane. 窗格是ScrollPane中的子级。 Here is my code: 这是我的代码:

    paneArea.setOnMouseClicked(new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent mouseEvent) {
            System.out.println(paneArea.getWidth()+" "+paneArea.getHeight());
            paneArea.resize(paneArea.getWidth() + 100, paneArea.getHeight() + 100);                
            System.out.println(paneArea.getWidth()+" "+paneArea.getHeight());
        }
    });

When I click twice I get the following result: 当我单击两次时,将得到以下结果:

602.0 574.0
702.0 674.0
602.0 574.0
702.0 674.0

So the Pane basically resizes briefly and then returns to its original size.On the FXML, the max height and width are set to USE_PREF_SIZE as well as MAX_VALUE. 因此,窗格基本上会短暂地调整大小,然后返回其原始大小。在FXML上,最大高度和宽度设置为USE_PREF_SIZE和MAX_VALUE。 I also tried to increase maxWidth/height before calling resize but made no difference. 我还尝试在调用resize之前增加maxWidth / height,但没有区别。 Any advice? 有什么建议吗?

I have been using wrong method as well as fxml option. 我一直使用错误的方法以及fxml选项。 The way to do it was with setPrefSize method, and then set the max size at USE_PREF in fxml file. 方法是使用setPrefSize方法,然后在fxml文件中的USE_PREF处设置最大大小。

Your code is wrong. 您的代码是错误的。 Instead of using 而不是使用

paneArea.resize(paneArea.getWidth() + 100, paneArea.getHeight() + 100); 

use 采用

paneArea.setPrefWidth(paneArea.getWidth() + 100) ; 
paneArea.setPrefHeight(paneArea.getHeight)() + 100) ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM