问题描述
如何调整 Swing
控件哪个在JavaFX8中的 SwingNode
里面?
How to resize Swing
control which is inside SwingNode
in JavaFX8?
有时,我在 SwingNode
中调整了控件的大小。但是 SwingNode
似乎抵制了这一点。
Sometimes, I has controls resized inside SwingNode
. But SwingNode
seems to resist this.
在 resize()
apidoc,
It is said in resize()
apidoc, that
但显然它不起作用。
示例代码如下。
问题是:如何让控件变大?
The question is: how to allow control to turn bigger?
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);
}
}
推荐答案
在基本相同的问题上打了几个小时之后,我终于弄清楚发生了什么。
After fighting for hours with basically the same issue, I finally figured out what was going on.
基本上,问题是SwingNode的父亲正在尝试根据父级的大小设置布局时的大小。因此,当您调整按钮大小,然后触发布局时,SwingNode的父级会将其设置回其默认大小。之所以发生这种情况是因为SwingNode会覆盖isResizable()方法以返回true,从而为其父对象提供调整大小的权限。
Basically, the problem is that the parent of the SwingNode is trying to set its size when layout occurs, based on the size of the parent. So when you resize your button, and then trigger a layout, the parent of the SwingNode sets it back to its default size. This is occurring because SwingNode overrides the isResizable() method to return true, giving permission to its parent objects to resize it.
为了避免这种情况,您可以:
In order to avoid this, you can:
- 创建一个SwingNode的自定义子类,它将isResizable()重写为false,
或:
- 在包含SwingNode的组上调用setAutosizeChildren(false)。
如果你用FXML定义类,可能需要使用后一种技术。
The latter technique will probably need to be used if you are defining your classes in FXML.
顺便说一下,你仍然可以在SwingNode上调用resize(width,height),即使它将isResizable()重写为false。
Note, by the way, that you can still call resize(width,height) on a SwingNode even if it overrides isResizable() to false.
这篇关于如何在JavaFX8中调整SwingNode内部的Swing控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!