package enablebuttons; import javafx.application.Application; import javafx.embed.swing.SwingNode; import javafx.event.ActionEvent; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Tooltip; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.layout.FlowPane; import javafx.stage.Stage; import javax.swing.SwingUtilities; public class EnableButtons extends Application { public static Button fxbutton; public static void main(String[] args) { launch(args); } @Override public void start(Stage stage) { final SwingNode swingNode = new SwingNode(); createSwingContent(swingNode); FlowPane pane = new FlowPane(); Image fxButtonIcon = new Image(getClass().getResourceAsStream("images/left.gif")); fxbutton = new Button("启用JButton", new ImageView(fxButtonIcon)); fxbutton.setTooltip(new Tooltip("点击此按钮以启用Swing按钮。")); fxbutton.setStyle("-fx-font: 18 arial; -fx-base: #cce6ff;"); fxbutton.setDisable(true); fxbutton.setOnAction((ActionEvent e) -> { SwingUtilities.invokeLater(() -> { ButtonHtml.b1.setEnabled(true); }); fxbutton.setDisable(true); }); pane.getChildren().addAll(swingNode, fxbutton); Scene scene = new Scene(pane, 300, 100); stage.setScene(scene); stage.setTitle("启用按钮示例"); stage.show(); } private void createSwingContent(final SwingNode swingNode) { SwingUtilities.invokeLater(() -> { swingNode.setContent(new ButtonHtml()); }); } }
JavaFX:互操作性