ZUCC 高级程序设计实验2(复杂版)

实验二    JavaFX基础

学号:            姓名:           班级:             

【实验目的和要求】

  1. 了解 JavaFX 编程的基础知识。
  2. 使用JavaFX 来演示OOP。

【实验内容】

  1. 用JavaFX设计一个登录界面,实现用户名、密码和登录按钮的界面。

代码:(截图和源代码)

package sample;

import javafx.application.Application;

import javafx.scene.Scene;

import javafx.scene.control.Button;

import javafx.scene.control.Label;

import javafx.scene.control.PasswordField;

import javafx.scene.control.TextField;

import javafx.scene.layout.BorderPane;

import javafx.scene.layout.FlowPane;

import javafx.scene.layout.StackPane;

import javafx.stage.Stage;

public class Main extends Application {

    public static void main(String[] args) {

        launch(args);

    }

    @Override

    public void start(Stage stage) throws Exception {

        FlowPane fp = new FlowPane();

        Label lc1 = new Label("用户名");

        TextField tf = new TextField("请输入你的用户名");

        Label lc2 = new Label("密码");

        PasswordField pwd = new PasswordField();

        Button n = new Button("登录");

        fp.getChildren().addAll(lc1,tf,lc2,pwd,n);

        Scene sc = new Scene(fp, 230, 100);

        stage.setTitle("登录");

        stage.setScene(sc);

        stage.show();

        n.setOnAction(e -> {

            String username = tf.getText();

            String password = pwd.getText();

            System.out.println("username "+username+" password "+password);

        });

    }

}

  1. 设计一个时钟界面,给出当前时间.

建两个类,一个ClockPane,一个主程序类

package sample;

import javafx.scene.layout.Pane;

import javafx.scene.paint.Color;

import javafx.scene.shape.Circle;

import javafx.scene.shape.Line;

import javafx.scene.text.Text;

import java.util.Calendar;

import java.util.GregorianCalendar;

public class ClockPane extends Pane{

    private int hour;

    private int minute;

    private int second;

    private double w=250,h=250;

    public ClockPane() {

        setCurrentTime();

    }

    public ClockPane(int hour,int minute,int second) {

        this.hour=hour;

        this.minute=minute;

        this.second=second;

        paintClock();

    }

    public int getHour() {

        return hour;

    }

    public void setHour(int hour) {

        this.hour=hour;

        paintClock();

    }

    public int getMinute() {

        return minute;

    }

    public void setMinute(int minute) {

        this.minute=minute;

        paintClock();

    }

    public int getSecond() {

        return second;

    }

    public void setSecond(int second) {

        this.second=second;

        paintClock();

    }

    public double getW() {

        return w;

    }

    public void setW(double w) {

        this.w=w;

        paintClock();

    }

    public double getH() {

        return h;

    }

    public void setH(double h) {

        this.h=h;

        paintClock();

    }

    public void setCurrentTime() {

        Calendar calendar=new GregorianCalendar();

        this.hour=calendar.get(Calendar.HOUR_OF_DAY);

        this.minute=calendar.get(Calendar.MINUTE);

        this.second=calendar.get(Calendar.SECOND);

        paintClock();

    }

    protected void paintClock() {

        double clockRadius=Math.min(w,h)*0.8*0.5;

        double centerX=w/2;

        double centerY=h/2;

        Circle circle=new Circle(centerX,centerY,clockRadius);

        circle.setFill(Color.WHITE);

        circle.setStroke(Color.BLACK);

        Text t1=new Text(centerX-5,centerY-clockRadius+12,"12");

        Text t2=new Text(centerX-clockRadius+3,centerY+5,"9");

        Text t3=new Text(centerX+clockRadius-10,centerY+3,"3");

        Text t4=new Text(centerX-3,centerY+clockRadius-3,"6");

        double sLength=clockRadius*0.8;

        double scondX=centerX+sLength*Math.sin(second*(2*Math.PI/60));

        double scondY=centerY-sLength*Math.cos(second*(2*Math.PI/60));

        Line sline=new Line(centerX,centerY,scondX,scondY);

        sline.setStroke(Color.RED);

        double mLength=clockRadius*0.65;

        double minuteX=centerX+mLength*Math.sin(minute*(2*Math.PI/60));

        double minuteY=centerY-mLength*Math.cos(minute*(2*Math.PI)/60);

        Line mline=new Line(centerX,centerY,minuteX,minuteY);

        mline.setStroke(Color.BLUE);

        double hLength=clockRadius*0.5;

        double hourX=centerX+hLength*Math.sin((hour%12+minute/60.0)*(2*Math.PI/12));

        double hourY=centerY-hLength*Math.cos((hour%12+minute/60)*(2*Math.PI/12));

        Line hline=new Line(centerX,centerY,hourX,hourY);

        hline.setStroke(Color.GREEN);

        getChildren().clear();

        getChildren().addAll(circle,t1,t2,t3,t4,sline,mline,hline);

    }

}

package sample;

import javafx.animation.Timeline;

import javafx.application.Application;

import javafx.geometry.Pos;

import javafx.scene.Scene;

import javafx.scene.layout.BorderPane;

import javafx.stage.Stage;

import javafx.util.Duration;

import javafx.event.ActionEvent;

import javafx.event.EventHandler;

import javafx.animation.KeyFrame;

import javafx.scene.control.Label;

public class Main extends Application {

    @Override

    public void start(Stage primaryStage) {

        ClockPane clock=new ClockPane();

        BorderPane borderPane=new BorderPane();

        EventHandler<ActionEvent> eventHandler=e -> {

            clock.setCurrentTime();

            String timeString=clock.getHour()+":"+clock.getMinute()+":"+clock.getSecond();

            Label lblCurrentTime=new Label(timeString);

            borderPane.setCenter(clock);

            borderPane.setBottom(lblCurrentTime);

            BorderPane.setAlignment(lblCurrentTime, Pos.TOP_CENTER);

        };

        Timeline animation=new Timeline(new KeyFrame(Duration.millis(1000),eventHandler));

        animation.setCycleCount(Timeline.INDEFINITE);

        animation.play();

        Scene scene=new Scene(borderPane,250,250);

        primaryStage.setTitle("ClockAnimation");

        primaryStage.setScene(scene);

        primaryStage.show();

        borderPane.widthProperty().addListener(ov ->

                clock.setW(borderPane.getWidth())

        );

        borderPane.heightProperty().addListener(ov ->

                clock.setH(borderPane.getHeight())

        );

    }

}

 

 代码:(截图和源代码)

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值