1.编写主程序启动代码
首先新建一个主窗口类,继承自javafx.application.Application类:
public class Main extends Application {
public static void main(String[] args) throws Exception {
launch(args);
}
@Override
public void start(Stage primaryStage) throws IOException {
//加载布局
Parent root = FXMLLoader.load(getClass().getResource("activity_main.fxml"));
primaryStage.setTitle("窗口标题");
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
@Override
public void stop() throws Exception {
super.stop();
System.exit(1);
}
}
其中的start方法是实现Application类的启动方法,程序启动之后,框架会首先调用该方法。因此初始化动作应当放在该方法中执行。stop方法是结程序束的时候框架会调用该方法
2.编写布局与管理器
布局文件打开结构如下:
<AnchorPane xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.172-ea"
prefHeight="476.0" prefWidth="726.0" fx:controller="MainActivity">
<Label layoutX="90.0" layoutY="7.0" text="文本" />
<TextField fx:id="chaseText" layoutX="589.0" layoutY="133.0" text="输入框" />
<Button fx:id="planStartButton" onAction="#buttonClick" layoutX="528.0" layoutY="337.0" text="按钮" />
</AnchorPane>
其中fx:controller="MainActivity"代表与其绑定的java类
其中fx:id=""代表了控件的id
其中的onAction="#buttonClick"的用处是定义绑定事件的。
public class MainActivity implements Initializable {
public TabPane tabPane;
LotteryManagement lotteryManagement = new LotteryManagement();
public void initialize(URL location, ResourceBundle resources) {
System.out.println("初始化完毕");
Tab tab = FXMLLoader.load(getClass().getResource("activity_tab.fxml"));
TextArea planRecordTextArea = (TextArea) tab.getContent().lookup("#planRecordTextArea");
TextArea bottomPourRecordTextArea = (TextArea) tab.getContent().lookup("#bottomPourRecordTextArea");
TextField omitText = (TextField) tab.getContent().lookup("#omitText");
TextField chaseText = (TextField) tab.getContent().lookup("#chaseText");
TextField moneyText = (TextField) tab.getContent().lookup("#moneyText");
tab.setText("模拟");
tabPane.getTabs().add(tab);
}
}