gpt4 book ai didi

java - 如何调整 JavaFX8 中 SwingNode 内的 Swing 控件的大小?

转载 作者:搜寻专家 更新时间:2023-10-31 20:20:46 26 4
gpt4 key购买 nike

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

有时,我在 SwingNode 中调整了控件的大小。但是 SwingNode 似乎反对这一点。

据说在resize() apidoc,那个

Applications should not invoke this method directly. If an application needs to directly set the size of the SwingNode, it should set the Swing component's minimum/preferred/maximum size constraints which will be propagated correspondingly to the SwingNode and it's parent will honor those settings during layout.

但显然它不起作用。

示例代码如下。

问题是:如何让控件变大?

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);
}

}

最佳答案

在为基本相同的问题奋斗了几个小时之后,我终于弄明白是怎么回事了。

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

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

  • 创建 SwingNode 的自定义子类,将 isResizable() 重写为 false,

或:

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

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

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

关于java - 如何调整 JavaFX8 中 SwingNode 内的 Swing 控件的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20350890/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com