javafx做桌面应用程序

2021-04-12 15:29

阅读:627

标签:primary   collect   style   prim   ring   version   str   system   lis   

idea可以直接开发javafx项目,但是需要一个SceneBuilder ,用来开发ui,或者按照  https://blog.csdn.net/hst_gogogo/article/details/82530929  ,进行集成

 

 

开发过程

说明:给fxml文件内标签设置control配置我这边没有生效,通过id注解控件,拿到的控件为空;通过 #方法名 注解控件方法是可以成功调用到方法

 

这边我用的是仿照安卓的控件查找和事件设置

 

 

java代码:、

Main.java

package sample;

import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.stage.Stage;

import java.util.Random;

public class Main extends Application {
    MainController mainController;
    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        Random random  = new Random();
        int i = random.nextInt(3);
        primaryStage.setTitle("Random 0-3 ,nextInt = " + i);
        mainController = new MainController();
        mainController.button_left = (Button) root.lookup("#button_left");
        mainController.button_right = (Button) root.lookup("#button_right");
        mainController.textArea_left = (TextArea) root.lookup("#textArea_left");
        mainController.textArea_right = (TextArea) root.lookup("#textArea_right");
        mainController.initAction();
        primaryStage.setScene(new Scene(root, 1280, 720));
        primaryStage.show();
    }


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

  

MainControl.java

package sample;

import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.input.MouseEvent;


public class MainController {

//    @FXML
    TextArea textArea_left;
//    @FXML
    TextArea textArea_right;
//    @FXML
    Button button_left;
//    @FXML
    Button button_right;

    int clickLeftCount = 0;
    int clickRightCount = 0;

//    @FXML
    void click_left() {
        if (button_left!=null){
            button_left.setText("我是button_left");
        }else {
            System.out.println("click_left is null");
        }
        if (textArea_left != null) {
            textArea_left.setText(clickLeftCount++ + "");
        }else {
            System.out.println("textArea_left is null");
        }
    }

//    @FXML
    void click_right() {
        if (button_right!=null){
            button_right.setText("我是button_right");
        }else {
            System.out.println("click_right is null");
        }
        if (textArea_right != null) {
            textArea_right.setText(clickRightCount++ + "");
        }else {
            System.out.println("textArea_right is null");
        }
    }

    public void initAction() {
        if (button_left!=null){
            button_left.setOnMouseClicked(new EventHandler() {
                public void handle(MouseEvent event) {
                    if (button_left!=null){
                        button_left.setText("我是button_left");
                    }else {
                        System.out.println("click_left is null");
                    }
                    if (textArea_left != null) {
                        textArea_left.setText(clickLeftCount++ + "");
                    }else {
                        System.out.println("textArea_left is null");
                    }
                }
            });
        }
        if (button_right!=null){
            button_right.setOnMouseClicked(new EventHandler() {
                public void handle(MouseEvent event) {
                    if (button_right!=null){
                        button_right.setText("我是button_right");
                    }else {
                        System.out.println("click_right is null");
                    }
                    if (textArea_right != null) {
                        textArea_right.setText(clickRightCount++ + "");
                    }else {
                        System.out.println("textArea_right is null");
                    }
                }
            });
        }

    }
}

  

fxml布局,是通过SceneBuilder绘制UI后,保存的fxml文件


        
        

  

javafx做桌面应用程序

标签:primary   collect   style   prim   ring   version   str   system   lis   

原文地址:https://www.cnblogs.com/fengchuxiaodai/p/13353882.html


评论


亲,登录后才可以留言!