四川省建行网站,怎么宣传自己的平台,天津做网站要多少钱,友情链接是在网站后台做吗javafx 和swing我很快将不得不在基于Swing的胖客户端中处理JavaFX –哦#xff0c;对不起#xff0c;我的意思是“多层富客户端”#xff01; 因此#xff0c;这使我来看看JFXPanel 。 JFXPanel是一个javax.swing.JComponent#xff0c;用于将JavaFX内容嵌入到Swing-UI中… javafx 和swing 我很快将不得不在基于Swing的胖客户端中处理JavaFX –哦对不起我的意思是“多层富客户端” 因此这使我来看看JFXPanel 。 JFXPanel是一个javax.swing.JComponent用于将JavaFX内容嵌入到Swing-UI中。 JFXPanel的用法类似于JPanel并且可以通过EDT作为通用Swing组件进行访问除了必须通过JavaFX应用程序线程来处理JavaFX组件之外。 为了解决这些问题我创建了两个类似的面板Swing JavaFX每个面板都有一个按钮一个TextField和一个Label并将它们放置在JSplitPane和JFrame中 仔细看里面 要尝试Swing - JavaFX互操作性按钮操作是将文本从TextField设置为JLabel反之亦然。 JPanel处理通用的Swing东西没有什么特别的但是JFXPanel包含JavaFX控件 public class SwingFXPanel extends JFXPanel {private Button testButton;private TextField testTextField;private Label testLabel;private VBox pane;public SwingFXPanel() {init();}private void init() {testButton new Button(I am a JavaFX Button);testTextField new TextField();testLabel new Label(empty);pane new VBox();pane.setAlignment(Pos.CENTER);pane.getChildren().addAll(testTextField, testButton, testLabel);Platform.runLater(this::createScene);}private void createScene() {Scene scene new Scene(pane);setScene(scene);}public Button getTestButton() {return testButton;}public TextField getTestTextField() {return testTextField;}public Label getTestLabel() {return testLabel;}
} 这里很重要将场景添加到JavaFX Application线程内的JFXPanel Platform.runLater(this::createScene); 如果您致电 createScene() 从另一个线程您会获得Runtime-Exception java.lang.IllegalStateException: Not on FX application thread; currentThread AWT-EventQueue-0 同样每次与JavaFX相关内容的交互都必须放在JavaFX Application Thread上 例如 Platform.runLater(() - {swingFXPanel.getTestLabel().setText(swingPanel.getTestTextField().getText());
});public class InteropFrame extends JFrame {private JSplitPane centralSplitPane;private SwingPanel swingPanel;private SwingFXPanel swingFXPanel;public InteropFrame(){init();}private void init() {setTitle(Swing - JavaFX Interoperatbiliy);setSize(500, 500);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setLayout(new BorderLayout());centralSplitPane new JSplitPane();centralSplitPane.setDividerLocation(0.5);centralSplitPane.setResizeWeight(0.3);swingPanel new SwingPanel();swingFXPanel new SwingFXPanel();swingPanel.getTestButton().addActionListener((ActionEvent e) - {Platform.runLater(() - {swingFXPanel.getTestLabel().setText(swingPanel.getTestTextField().getText());});});swingFXPanel.getTestButton().setOnAction((javafx.event.ActionEvent t) - {swingPanel.getTestLabel().setText(swingFXPanel.getTestTextField().getText());});centralSplitPane.setLeftComponent(swingPanel);centralSplitPane.setRightComponent(swingFXPanel);add(centralSplitPane, BorderLayout.CENTER);}
}另外处理FXML也很简单 public class SwingFXMLPanel extends JFXPanel {FXMLprivate Button testButton;FXMLprivate TextField testTextField;FXMLprivate Label testLabel;private VBox rootPane;private URL fxmlResource;public SwingFXMLPanel(URL fxmlResource){this.fxmlResource fxmlResource;init();}private void init(){rootPane new VBox();FXMLLoader loader new FXMLLoader(fxmlResource);loader.setController(this);loader.setRoot(rootPane);try {loader.load();} catch (IOException ex) {Logger.getLogger(SwingFXMLPanel.class.getName()).log(Level.SEVERE, null, ex);}testButton.setText(I am a JavaFX Button);testLabel.setText(empty);Platform.runLater(this::createScene);}private void createScene() {Scene scene new Scene(rootPane);setScene(scene);}public Button getTestButton() {return testButton;}public TextField getTestTextField() {return testTextField;}public Label getTestLabel() {return testLabel;}} 对我来说让我的同事尽可能多地接受在Swing中使用JavaFX至关重要。 因此我想简化特定的FX应用程序线程处理。 因此如果使用JPanel的主要区别只是添加以下内容那么也许可以实现 private void createScene() {Scene scene new Scene(rootPane);setScene(scene);} 并致电 Platform.runLater(this::createScene); 在JFXPanel中 。 您可以在此处找到完整的示例代码。 翻译自: https://www.javacodegeeks.com/2014/11/swing-and-javafx-working-with-jfxpanel.htmljavafx 和swing