JavaFX example: the realization of a simple calendar

Java's ui library is divided into three generations, the first generation is awt, the second generation is swing, and the third generation is javafx, which is a substitution relationship between each other. Desktop development now generally uses swing and javafx, today we use javafx to develop a simple calendar query application.

The final effect is as follows:
Insert picture description here
git address: JavaFX calendar implementation


javafx development

There are two ways to develop javafx development interface:
1. Write through Java code.
All the interface and logic are implemented through Java code, and there are many things written.

2. Write and realize through JavaFX Scene Builder + Java code.
JavaFX Scene Builder is a visual design tool that allows rapid creation of application interface through drag and drop. And the code is created as a file in XML format. The interface is developed by dragging and dropping with JavaFX Scene Builder, and the logic is relatively simple and fast to implement in Java.

The following example is implemented by JavaFX Scene Builder + Java.


下载JavaFX Scene Builder

Official website download: JavaFX Scene Builder official website


Create project

Choose Java Fx for the new project idea.
(Note: Try to use jdk8 and above. Below jdk8 does not have its own javafx, you need to import the jar package yourself, Baidu yourself)
Insert picture description here
The structure after completion is as follows, the entrance of the Main program, sample.fxml is used for JavaFX Scene Builder drag and drop Generate interface, Controller is used to bind interface and process monitoring events:
Insert picture description here

Select sample.fxml, right-click open in sceneBuilder, then select
Insert picture description here
and then select the installed JavaFX Scene BuilderInsert picture description here

interface design

Select the corresponding components from the upper left corner to the middle for layout. As shown in the figure, I selected 7 components, and the left side is the information of all the selected components. Select any component in the middle and there are specific settings for the component on the right, such as the length, width, default prompt of the input box, and the name of the component.
Insert picture description here
Set the name of the component, which will be used in Java later:
Insert picture description here
After saving, the content of sample.fxml becomes as follows:
manually add fx:controller="sample.Controller" to associate the sample.fxml file with the Controller file, so write below Only Java code can use the things in fxml.

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="399.0" prefWidth="627.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
   <children>
      <Button fx:id="id_1" layoutX="103.0" layoutY="120.0" mnemonicParsing="false" onAction="#buttonAction1" prefHeight="30.0" prefWidth="119.0" text="获取日历" textFill="#3a6f2e" />
      <TextField fx:id="text_1" layoutX="102.0" layoutY="61.0" promptText="请输入年份,如:2021" />
      <TextField fx:id="text_2" layoutX="380.0" layoutY="61.0" promptText="请输入月份,如:2" />
      <Text layoutX="48.0" layoutY="82.0" strokeType="OUTSIDE" strokeWidth="0.0" text=" 年:" wrappingWidth="35.0" />
      <Text layoutX="332.0" layoutY="82.0" strokeType="OUTSIDE" strokeWidth="0.0" text=" 月:" wrappingWidth="35.0" />
      <TextArea fx:id="text_3" layoutX="167.0" layoutY="197.0" prefHeight="146.0" prefWidth="274.0" />
   </children>
</AnchorPane>

Code:

Controller:

package sample;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import java.net.URL;
import java.util.ResourceBundle;

public class Controller implements Initializable {
    
    
    @FXML
    private Button id_1;
    @FXML
    private TextField text_1;//从fxml获取组件text_1,用于读取年
    @FXML
    private TextField text_2;//从fxml获取组件text_2,用于读取月
    @FXML
    private TextArea text_3;//从fxml获取组件text_3,用于输出

    @Override
    public void initialize(URL location, ResourceBundle resources)
    {
    
    }

    //点击按钮时触发的事件
    public void buttonAction1(ActionEvent actionEvent)
    {
    
    
        String year = text_1.getText();//获取文本框输入的内容
        String month = text_2.getText();//获取文本框输入的内容
        String result=Service.calculationDate(year,month);
        text_3.setText(result);
    }
}

service:

package sample;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

public class Service {
    
    
    public static String calculationDate(String y, String m) {
    
    
        char[] yearChar = y.toCharArray();
        char[] monthChar = m.toCharArray();
        if(null==y||y.length()==0||null==m||m.length()==0){
    
    
            return "年份或月份不能输入为空。";
        }

        for (int i = 0; i < yearChar.length; i++) {
    
    
            boolean isOrNo = Character.isDigit(yearChar[i]);
            if (yearChar[0] == '0' || !isOrNo) {
    
    
                return "年份输入错误,请输入正确的年份。";
            }
        }

        for (int i = 0; i < monthChar.length; i++) {
    
    
            boolean isOrNo = Character.isDigit(monthChar[i]);
            if (yearChar[0] == '0' || !isOrNo) {
    
    
                return "月份输入错误,请输入正确的月份。";
            }
        }

        int year = Integer.parseInt(y);
        int month = Integer.parseInt(m);

        if (year < 1900||(year==1900&&month==1)) {
    
    
            return "年份需要大于等于1900。当年份是1900时,月份需要大于1。";
        }

        if (month > 12) {
    
    
            return "月份输入错误,请输入正确的月份。";
        }

        LocalDate endDate = LocalDate.of(year, month, 1);//设置输入的日期
        LocalDate startDate = LocalDate.of(1900, 1, 1);
        long sumDay = startDate.until(endDate, ChronoUnit.DAYS);//总相差的天数:3657
        /*以上代码计算1900年1月1号到输入的年月的总天数,如输入2018和8,则计算1900.1.1-2018.7.31的天数*/

        long xingQi = (sumDay + 1) % 7; //xingQi用来计算输入的月份1号星期几
        long everydayXingQi = sumDay + 1;
        int monthDay = endDate.lengthOfMonth();//用来接收输入的月份有几天

        String result = "日\t一\t二\t三\t四\t五\t六\n";
        for (int i = 0; i < xingQi; i++) {
    
        //输出每月1号前的空格
            result += "\t";
        }
        for (int i = 1; i <= monthDay; i++) {
    
      //输出每个月的天数和控制换行
            if (everydayXingQi % 7 == 6) {
    
    
                result += i + "\n";
            } else {
    
    
                result += i + "\t";
            }
            everydayXingQi++;
        }
        return result;
    }
}

Main:

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;

public class Main extends Application {
    
    

    @Override
    public void start(Stage primaryStage) throws Exception{
    
    
        //引用sample.fxml页面布局
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        //设置窗口名称
        primaryStage.setTitle("日历");
        //设置窗口大小
        primaryStage.setScene(new Scene(root, 627, 399));
        //固定窗口大小使其无法最大化
        primaryStage.setResizable(false);
        //设置窗口的图标.
        primaryStage.getIcons().add(new Image("/sample/image/rili.jpg"));
        primaryStage.show();
    }

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

Start the project and it's ok.


Tagged into a jar package:
Insert picture description hereInsert picture description here

Of course, you can also type the written program into a jar package or an exe program, and package it as follows:

Insert picture description here
Insert picture description here
Insert picture description here
An exe file will be generated under javaFxText.
Insert picture description here
After opening the folder, double-click to run it.
Insert picture description here
Insert picture description here
But this exe file is too difficult to find. After searching for a long time, I wanted to create a shortcut, but the shortcut only supports absolute paths. , We can go back to the upper directory and create a new txt file input:

start JavaFxTest\JavaFxTest.exe
exit

Start is followed by your own folder name and exe name.
After saving the file, change the suffix to bat

You can use it after double-clicking.
Insert picture description here

Regarding other packaging methods, I haven't tried it yet. For example, first type it into a jar package and then type it into an exe file through other tools.
Reference: package java into exe- run Java project on a machine without jdk or jre
java tool (1): run java project on a computer without jdk environment to make exe file


Javafx tutorial:
Javafx tutorial
w3cschool-javafx tutorial
JavaFX overview and tutorial
javafxchina Introduction to
JavaFX (1): My first JavaFX program How to
use JavaFX Scene Builder
How to create a text area in JavaFX JavaFX
project package
Use exe4j to type java files into exe File running detailed tutorial

Guess you like

Origin blog.csdn.net/qq_33697094/article/details/112237463