如何在JavaFX8中调整位于SwingNode内的Swing控件大小?

5
如何调整 JavaFX8 中 SwingNode 内的 Swing 控件大小?
有时候,我需要在 SwingNode 中调整控件的大小。但是,SwingNode 似乎抵制这种操作。
resize() api 文档中所述:

应用程序不应该直接调用此方法。如果应用程序需要直接设置 SwingNode 的大小,则应设置 Swing 组件的最小/首选/最大大小约束,这些约束将相应地传播到 SwingNode,并且它的父级在布局期间将遵守这些设置。

但是显然这并不起作用。
以下是示例代码。
问题是:如何允许控件变得更大?
public class Try_Sizes_01 extends Application {

    private static final Logger log = LoggerFactory.getLogger(Try_Sizes_01.class);

    private static final String text = "Very Long Text For Appear On Button ";
    private static int position = 7;

    //private JButton button = new JButton("short");
    private JButton button = new JButton(text.substring(0, position));

    private SwingNode swingNode = new SwingNode();
    {
        swingNode.setContent(button);
    }

    @Override
    public void start(Stage stage) throws Exception {


        Group group = new Group();
        group.getChildren().add(swingNode);

        Scene scene = new Scene(group);

        stage.setTitle("Try_Sizes_01");
        stage.setScene(scene);
        stage.show();

        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {

                new Timer(1000, new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {

                        /*
                        button.setText(button.getText() + text.charAt(position));

                        position++;
                        if( position >= text.length() ) {
                            position=0;
                        }
                        */

                        button.setPreferredSize(new Dimension((int)button.getPreferredSize().getWidth()+10, (int)button.getPreferredSize().getHeight()));
                        button.setMinimumSize(new Dimension((int)button.getPreferredSize().getWidth(), (int)button.getPreferredSize().getHeight()));
                        //button.revalidate();

                        //button.setBounds(0, 0, (int)button.getBounds().getWidth()+10, (int)button.getBounds().getHeight());

                        Platform.runLater(new Runnable() {

                            @Override
                            public void run() {

                                //swingNode.autosize(); // does not work
                                //swingNode.resize(button.getBounds().getWidth(), button.getBounds().getHeight()); // does not work and cancels button resizing
                                //swingNode.setContent(button); // works sometimes but imperfect


                            }
                        });

                        log.info("Swing thread");
                        log.info("Preferred width is now = {}", button.getPreferredSize().getWidth());
                        log.info("Bounds width is now = {}", button.getBounds().getWidth());

                    }
                }).start();

            }
        });

    }

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

}
3个回答

5
经过数小时的奋斗,我终于搞清楚了问题所在。基本上,问题是SwingNode的父类在布局发生时会根据父类的大小设置其大小。所以当你调整按钮的大小并触发布局时,SwingNode的父类将其重新设置为默认大小。这是因为SwingNode覆盖了isResizable()方法并返回true,允许其父对象调整其大小。
为了避免这种情况,你可以:
  • 创建一个自定义的SwingNode子类,并将isResizable()重写为false,
或者:
  • 在包含SwingNode的Group上调用setAutosizeChildren(false)。
如果你在FXML中定义你的类,则可能需要使用后一种技术。
顺便提一下,即使SwingNode覆盖了isResizable()方法并将其设置为false,你仍然可以调用resize(width,height)方法。

0

我不确定是否完全相同,但在获取Swing组件与父容器正确调整大小方面似乎有关。在我的情况下,我有一个包含JFreeChart(ChartPanel)的SwingNode,当父框架(SplitPane内的边框窗格)自身调整大小时,我根本无法使其正确调整大小。最终,我只是添加了一个监听器来监听高度/宽度属性:

pane.widthProperty().addListener((w,o,n)->c.resizeChart((int)n.intValue(), (int)pane.getHeight()));
pane.heightProperty().addListener((w,o,n)->c.resizeChart((int)pane.getWidth(), (int)n.intValue()));

我尝试过的其他方法都无法模拟这个。

谢谢。

-1

我发现面板监听器有所帮助,但由于我的组件的专有性质,它仍然无法调整大小。我正在使用FXML,并尝试将SwingNode放置在Pane中进行布局。然后我注意到许多示例都使用StackPane而不仅仅是Pane,突然间只要做出这个更改,代码就可以工作了。这是在AnchorPane内部完成的,这也似乎确保了组件的初始显示填充了所有可用空间。总之,在AnchorPane内部使用StackPane,并将所有锚点设置为0,可以确保控件填充所有初始可用空间,然后进行所有调整大小操作。当添加手动调整大小监听器时,一切都开始正常工作。

pane.widthProperty().addListener((w,o,n)->c.resizeChart((int)n.intValue(), (int)pane.getHeight()));
pane.heightProperty().addListener((w,o,n)->c.resizeChart((int)pane.getWidth(), (int)n.intValue()));

“c”是什么类型的对象?或者说,“pane”呢? - MMAdams

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接