我在JavaFx中编写了一个简单的应用程序,并想在其中使用swingNode,所以我在更改节点的大小或位置时遇到了问题
public class SwingFx extends Application {
@Override
public void start (Stage stage) {
final SwingNode swingNode = new SwingNode();
createSwingContent(swingNode);
StackPane pane = new StackPane();
pane.getChildren().add(swingNode);
stage.setTitle("Swing in JavaFX");
stage.setScene(new Scene(pane, 250, 150));
stage.show();
}
private void createSwingContent(final SwingNode swingNode) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JButton jButton = new JButton("Click me!");
jButton.setBounds(0,0,5,5);
swingNode.setContent(jButton);
}
});
}
}
我设置JButton的界限但是当我运行应用程序时它不起作用 按钮出现在中间,默认宽度和高度
答案 0 :(得分:1)
如果希望setbounds()
工作
public class SwingFx extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start (Stage stage) {
final SwingNode swingNode = new SwingNode();
createSwingContent(swingNode);
StackPane pane = new StackPane();
pane.getChildren().add(swingNode);
stage.setTitle("Swing in JavaFX");
stage.setScene(new Scene(pane, 250, 150));
stage.show();
}
private void createSwingContent(final SwingNode swingNode) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JButton jButton = new JButton("Click me!");
jButton.setBounds(10,10,50,50);
JPanel panel = new JPanel();
panel.setLayout(null);
panel.add(jButton);
swingNode.setContent(panel);
}
});
}
}