Java Fx 使用详解

 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.编写布局与管理器

布局文件打开结构如下:

     
        

其中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);

    }
}

https://blog.csdn.net/johnson_moon/article/details/53572705

你可能感兴趣的:(Java Fx 使用详解)