JavaFX总结

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jiangxishidayuan/article/details/80986482

1. MouseClicked事件

鼠标点击事件分为单击和双击,当给控件添加setOnMouseClicked 监听时要区分。

pagination.setOnMouseClicked(e -> {
    //这里要区分单击和双击,否则当两下点击很快的时候,只会响应一次
});

2. 自定义控件

JavaFX有两种自定义控件的方式,分为耦合式和独立式。参考博客

  • 耦合式(FXML 和 Controller耦合在一起)

Java代码

public class YieldStarSimPane extends VBox {

  @FXML
  private TextField packageTextField;
  @FXML
  private Button browseBtn;
  @FXML
  private Button defaultBtn;

  public YieldStarSimPane() {
         FXMLLoaderUtil.load(YieldStarSimPane.class.getResource("YieldStarSimPane.fxml"), this, this, ResourceBundle.getBundle("com.asml.d4m.gui.client.overlay.ui.resources.messages"));
         // Do other things

  }
}

在FXML中定义fx:root,并且指定类型。

<fx:root type="VBox" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1">
    ...
</fx:root>
  • 独立式(FXML 和 Controller独立开来)

Java代码

public class LegendPaneController implements Initializable {
    @FXML private ListView<String> listView;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
      //在控件注入完之后,执行初始化操作
    }
}

public class LegendApp extends Application {
  @Override
  public void start(Stage stage) throws Exception {
    FXMLLoader loader = new FXMLLoader(LoginFX.class.getResource("legend.fxml"));
    LoginPaneController controller = new LoginPaneController();
    loader.setController(controller);
    //...
  }
}

FXML文件

<VBox xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
  <children>
     <ListView fx:id="listView"/>
  </children>
</VBox>

3. JavaFX TabPane Drag and Drop

源代码:dragdropfxDndTabPane
Maven 依赖

<dependency>
  <groupId>com.sibvisions.external.jvxfx</groupId>
  <artifactId>dndtabpane</artifactId>
  <version>0.1</version>
</dependency>

Example

public class App extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Tabs");
        Group root = new Group();
        Scene scene = new Scene(root, 400, 250, Color.WHITE);
        //Create the dnd pane
        DndTabPane tabPane = new DndTabPane();
        StackPane containerPane = new StackPane(tabPane);
        //Create the Skin, you can custom your skin
        DnDTabPaneSkin skin = new DnDTabPaneSkin(tabPane);
        //Set up the Draging
        DndTabPaneFactory.setup(DndTabPaneFactory.FeedbackType.MARKER, containerPane, skin);
        //Set the skin
        tabPane.setSkin(skin);
        for (int i = 0; i < 5; i++) {
            Tab tab = new Tab();
            tab.setText("Tab" + i);
            HBox hbox = new HBox();
            hbox.getChildren().add(new Label("Tab" + i));
            hbox.setAlignment(Pos.CENTER);
            tab.setContent(hbox);
            tabPane.getTabs().add(tab);
        }
        // bind to take available space
        containerPane.prefHeightProperty().bind(scene.heightProperty());
        containerPane.prefWidthProperty().bind(scene.widthProperty());
        root.getChildren().add(containerPane);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

效果图
Drag

4. JavaFX线程问题

线程刷新UI问题

  • 使用Platform的runLater方法,把刷新UI的操作放在runLater方法中去,这样刷新UI的操作就会在主线程中进行

    扫描二维码关注公众号,回复: 3181123 查看本文章
  • 使用javafx.concurrent中的Task类,这个类是线程安全的,可以用来刷新UI,注意这个需要使用一个thread对象来跑.

猜你喜欢

转载自blog.csdn.net/jiangxishidayuan/article/details/80986482