闰年检验

闰年检验

相关代码:

package test1;

 

import java.util.regex.Matcher;

import java.util.regex.Pattern;

import javafx.application.Application;

import javafx.event.ActionEvent;

import javafx.event.EventHandler;

import javafx.scene.Scene;

import javafx.scene.control.Button;

import javafx.scene.control.TextField;

import javafx.scene.layout.AnchorPane;

import javafx.scene.paint.Color;

import javafx.scene.text.Font;

import javafx.scene.text.Text;

import javafx.stage.Stage;

 

public class test02 extends Application {

    public static boolean isRegularRptCode(String rptCode,String regEx) {

        Pattern p1 = Pattern.compile(regEx);

        Matcher m1 = p1.matcher(rptCode);

        boolean rs1 = m1.matches();

        return rs1;

    }

    public static void main(String[] args) {

        test02.launch(args);

    }

    public void start(Stage stage)throws Exception {

       

        stage.setTitle("TEST");

        AnchorPane root = new AnchorPane();

        Scene scene = new Scene(root, 500, 500);

        scene.setFill(Color.WHITE);

       

        Text usename= new Text("输入:");

        usename.setFont(Font.font ("Serif", 28));

        AnchorPane.setTopAnchor(usename, 100.0);

        AnchorPane.setLeftAnchor(usename, 50.0);

       

        Text tips= new Text("验证:");

        tips.setFont(Font.font ("Serif", 28));

        AnchorPane.setTopAnchor(tips, 150.0);

        AnchorPane.setLeftAnchor(tips, 50.0);

       

       

        final TextField textblock=new TextField();

        AnchorPane.setTopAnchor(textblock, 105.0);

        AnchorPane.setLeftAnchor(textblock, 200.0);

       

        final TextField textblock2=new TextField();

        AnchorPane.setTopAnchor(textblock2, 155.0);

        AnchorPane.setLeftAnchor(textblock2, 200.0);

       

        Button button = new Button("检验");

        AnchorPane.setTopAnchor(button, 405.0);

        AnchorPane.setRightAnchor(button, 50.0);

       

        button.setOnAction( new EventHandler<ActionEvent>( ) {

            public void handle(ActionEvent actEvt) {       

                final String input1;

                input1 = textblock.getText();

                //1

               

                if(!isRegularRptCode(input1,"[0-9]*")){

                    textblock2.setText("er1: 字符应为数字");

                }

                else{

                    int year;

                    year = Integer.parseInt(input1);

                    if((year % 4 == 0 && year % 100 != 0) || year%400==0)

                       textblock2.setText("闰年");

                    else{

                       textblock2.setText("非闰年");

                    }

                }

              

            }

        } );

        root.getChildren().addAll(usename,tips,button,

               textblock,textblock2);

        stage.setScene(scene);

        stage.show();

    }

}

相关截图:

原文地址:https://www.cnblogs.com/wangdongT-T/p/4399690.html