JavaFX总结

#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.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
源代码:dragdropfx,DndTabPane
Maven 依赖


  com.sibvisions.external.jvxfx
  dndtabpane
  0.1

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();
    }
}

效果图
JavaFX总结_第1张图片

#4. JavaFX线程问题
线程刷新UI问题

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

  • 使用javafx.concurrent中的Task类,这个类是线程安全的,可以用来刷新UI,注意这个需要使用一个thread对象来跑.

你可能感兴趣的:(Java进阶)