实验二 JavaFX基础
学号: 姓名: 班级:
【实验目的和要求】
- 了解 JavaFX 编程的基础知识。
- 使用JavaFX 来演示OOP。
【实验内容】
- 用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);
});
}
}
- 设计一个时钟界面,给出当前时间.
建两个类,一个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())
);
}
}
代码:(截图和源代码)