gpt4 book ai didi

JavaFX 8 Swing 兼容性

转载 作者:搜寻专家 更新时间:2023-10-30 19:46:02 24 4
gpt4 key购买 nike

我目前正在评估是否可以使用 SwingNode 将我当前的 Swing 应用程序转换为 JavaFX 8,然后慢慢将所有内容更改为 JavaFX 组件。我偶然发现了两个问题,它们可能是 JavaFX 8 中挥之不去的错误,但我不能确定。

  1. 将内容添加到 SwingNode 后,未绘制 Javafx 菜单正确(大部分时间),导致黑条
  2. 使 JScrollBars 不透明似乎不起作用

    import java.awt.Color;
    import java.awt.GridLayout;

    import javafx.application.Application;
    import javafx.embed.swing.SwingNode;
    import javafx.scene.Scene;
    import javafx.scene.control.Menu;
    import javafx.scene.control.MenuBar;
    import javafx.scene.layout.StackPane;
    import javafx.scene.layout.VBox;
    import javafx.stage.Stage;

    import javax.swing.JPanel;
    import javax.swing.JScrollPane;


    public class Test extends Application {

    @Override
    public void start(Stage stage) throws Exception {
    stage.setMaximized(true);

    VBox vbox = new VBox();
    MenuBar menuBar = new MenuBar();
    menuBar.getMenus().add(new Menu("bla"));
    vbox.getChildren().add(menuBar);

    StackPane mainPane = new StackPane();
    mainPane.setPrefSize(9999, 9999); //Better way to ensure all space used?
    vbox.getChildren().add(mainPane);
    vbox.setVisible(true);

    Scene scene = new Scene(vbox, 9999, 9999); //Better way to ensure all space used?
    stage.setScene(scene);

    SwingNode swingNode = new SwingNode();
    mainPane.getChildren().add(swingNode);

    JPanel jPanel = new JPanel();
    swingNode.setContent(jPanel); //Seems to interfere with painting of menu

    jPanel.setBackground(Color.WHITE);
    jPanel.setLayout(new GridLayout(2, 1));
    jPanel.add(new JPanel());

    JPanel nonOpaquePanel = new JPanel();
    nonOpaquePanel.setOpaque(false);
    JScrollPane scrollPane = new JScrollPane(nonOpaquePanel);
    scrollPane.setOpaque(false);
    scrollPane.getViewport().setOpaque(false); //Black background -> Bug?
    jPanel.add(scrollPane);

    stage.show();
    }

    /**
    * Main method launching the application.
    */
    public static void main(String[] args) {
    launch(args);
    }
    }

    Executable jar

编辑:我编辑了示例以使用 EDT 创建 Swing 组件。那并没有改变任何事情。还是我做错了什么?

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.embed.swing.SwingNode;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;


public class Test extends Application {
private JPanel jPanel = null;

@Override
public void start(Stage stage) throws Exception {
if (Platform.isFxApplicationThread()) { // all of this happens on the fx application thread
stage.setMaximized(true);

VBox vbox = new VBox();
MenuBar menuBar = new MenuBar();
menuBar.getMenus().add(new Menu("bla"));
vbox.getChildren().add(menuBar);

StackPane mainPane = new StackPane();
mainPane.setPrefSize(9999, 9999); //Better way to ensure all space used?
vbox.getChildren().add(mainPane);
vbox.setVisible(true);

Scene scene = new Scene(vbox, 9999, 9999); //Better way to ensure all space used?
stage.setScene(scene);

SwingNode swingNode = new SwingNode();
mainPane.getChildren().add(swingNode);

SwingUtilities.invokeLater(new Runnable() { //Use EDT to build swing components
@Override
public void run() {
jPanel = new JPanel();

jPanel.setBackground(Color.WHITE);
jPanel.setLayout(new GridLayout(2, 1));
jPanel.add(new JPanel());

JLabel nonOpaquePanel = new JLabel("Bla");
nonOpaquePanel.setPreferredSize(new Dimension(5000, 5000));
nonOpaquePanel.setOpaque(false);
JScrollPane scrollPane = new JScrollPane(nonOpaquePanel);
scrollPane.setOpaque(false);
scrollPane.getViewport().setOpaque(false); //Black background -> Bug?
jPanel.add(scrollPane);
}
});

Thread.sleep(1000); //terrible way to wait for the EDT, I know
swingNode.setContent(jPanel); //Seems to interfere with painting of menu
stage.show();

SwingUtilities.invokeLater(new Runnable() { //I am pretty sure this isn't necessary, but whatever
@Override
public void run() {
jPanel.repaint();
}
});
}
}

/**
* Main method launching the application.
*/
public static void main(String[] args) {
launch(args);
}
}

最佳答案

尝试在进行更新调用时将更新代码包装在其中。此代码之前在其他项目中对我有所帮助。

Platform.runLater(new Runnable() {
@Override
public void run() {
// Update the text node with calculated results
}
});

Source

关于JavaFX 8 Swing 兼容性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22808223/

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