如何在JavaFX8中调整SwingNode内部的Swing控件?

Suz*_*ioc 5 java swing javafx javafx-8

如何调整JavaFX8 Swing内部SwingNode的控件?

有时,我在内部调整了控件SwingNode.但SwingNode似乎抵制了这一点.

resize()apidoc中说,那

应用程序不应直接调用此方法.如果应用程序需要直接设置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);
    }

}
Run Code Online (Sandbox Code Playgroud)

小智 5

在与基本相同的问题战斗了几个小时后,我终于弄清楚发生了什么。

基本上,问题在于 SwingNode 的父级试图在布局发生时根据父级的大小设置其大小。因此,当您调整按钮大小,然后触发布局时,SwingNode 的父级会将其设置回其默认大小。发生这种情况是因为 SwingNode 覆盖了 isResizable() 方法以返回 true,从而允许其父对象调整其大小。

为了避免这种情况,您可以:

  • 创建 SwingNode 的自定义子类,它将 isResizable() 覆盖为 false,

或者:

  • 在包含 SwingNode 的组上调用 setAutosizeChildren(false)。

如果您在 FXML 中定义类,则可能需要使用后一种技术。

请注意,顺便说一下,您仍然可以在 SwingNode 上调用 resize(width,height),即使它将 isResizable() 覆盖为 false。