Main中的方法
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.net.URL;
import javafx.fxml.JavaFXBuilderFactory;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
URL location = getClass().getResource("sample.fxml");
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(location);
fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory());
Parent root = fxmlLoader.load();
//如果使用 Parent root = FXMLLoader.load(...) 静态读取方法,无法获取到Controller的实例对象
primaryStage.setTitle("Hello World");
Scene scene = new Scene(root, 400, 500);
//加载css样式
//scene.getStylesheets().add(getClass().getResource("style1.css").toExternalForm());
primaryStage.setScene(scene);
Controller controller = fxmlLoader.getController(); //获取Controller的实例对象
//Controller中写的初始化方法
controller.Init();
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Controller类:
package sample;
import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ComboBox;
import javafx.event.ActionEvent;
import javafx.collections.ObservableList;
public class Controller {
@FXML
private Button mButton;
@FXML
private Label mLabel;
@FXML
private ComboBox comboBox_API;
Integer ClickCount=0;
private ObservableList<String> apiList=FXCollections.observableArrayList();
public void Init(){
apiList.add("创建订单");
apiList.add("获取订单信息");
this.comboBox_API.setItems(apiList);
this.mButton.setText("按钮1");
}
@FXML
public void onButtonClick(ActionEvent event) {
ClickCount++;
mLabel.setText("HelloWorld"+ClickCount);
}
}
Fxml文件:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.collections.ObservableList?>
<GridPane alignment="center" hgap="10" prefHeight="400.0" prefWidth="500.0" vgap="10" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<columnConstraints>
<ColumnConstraints prefWidth="60"/>
<ColumnConstraints prefWidth="60" />
<ColumnConstraints prefWidth="280"/>
</columnConstraints>
<rowConstraints>
<RowConstraints prefHeight="40"/>
<RowConstraints prefHeight="200" />
<RowConstraints prefHeight="40" />
<RowConstraints prefHeight="200" />
</rowConstraints>
<children>
<Label layoutX="10" layoutY="10" text="API接口" GridPane.columnIndex="0" GridPane.rowIndex="0">
<GridPane.margin>
<Insets bottom="10.0" top="5.0" />
</GridPane.margin>
</Label>
<ComboBox fx:id="comboBox_API" prefWidth="120" GridPane.columnIndex="1" GridPane.rowIndex="0">
<GridPane.margin>
<Insets bottom="10.0" top="5.0" />
</GridPane.margin>
</ComboBox>
<Button fx:id="mButton" mnemonicParsing="false" onAction="#onButtonClick" text="Button" GridPane.columnIndex="2" GridPane.rowIndex="1" />
<Label fx:id="mLabel" text="Label" GridPane.columnIndex="2" GridPane.rowIndex="2" />
</children>
</GridPane>