• java GUI


    1.

    编写程序,随机生成两个数,用户输入两个数的和,并进行评判。程序的初始界面如下:

    随机出题1.png

    点击“获取题目”,随机生成两个100以内的int类型的数,界面如下:

    随机出题2.png

    提示:

    (1)使用java.util.Random类的nextInt(int n)生成一个100以内的随机数。nextInt(int n) 的作用: 返回一个介于 0(包括)和指定值n(不包括)之间均匀分布的 int 值。

    (2)Integer类的静态方法parseInt(String str),可以将字符串转换为int类型的整数。

    【试题输入输出】

    当用户输入一个正确的答案时,点击“确认答案”按钮,显示如下界面:

    随机出题3.png

    当用户输入一个错误的答案时,点击“确认答案”按钮,显示如下界面:

    随机出题4.png

    import javafx.application.Application;
    import javafx.geometry.Insets;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.Label;
    import javafx.scene.control.TextField;
    import javafx.scene.layout.GridPane;
    import javafx.scene.layout.HBox;
    import javafx.stage.Stage;
    import java.util.Random;

    public class count extends Application {
    public static void main(String[] args) {
    Application.launch(args);
    }

    private Label a = new Label("");
    private Button bt1 = new Button("获取题目");
    private Button bt2 = new Button("确认答案");
    private TextField tf1 = new TextField();
    private TextField tf2 = new TextField();
    private TextField tf3 = new TextField();

    public void start(Stage primaryStage) {
    GridPane pane = new GridPane();
    pane.setPadding(new Insets(11));
    pane.setHgap(5);
    pane.setVgap(5);

    pane.add(bt1, 0, 0);
    pane.add(tf1, 1, 0);
    pane.add(new Label("+"), 2, 0);
    pane.add(tf2, 3, 0);
    pane.add(new Label("="), 4, 0);
    pane.add(tf3, 5, 0);
    HBox hb = new HBox(10);
    hb.getChildren().addAll(bt2,a);
    pane.add(hb, 3, 1);
    bt1.setOnAction(e->RandomNumber());
    bt2.setOnAction(e->Count());

    Scene scene = new Scene(pane);
    primaryStage.setScene(scene);
    primaryStage.show();
    }

    public void RandomNumber() {
    Random r = new Random();
    tf1.setText(""+r.nextInt(100));
    tf2.setText(""+r.nextInt(100));
    }

    public void Count() {
    int n = Integer.parseInt(tf1.getText())+Integer.parseInt(tf2.getText());
    int num = Integer.parseInt(tf3.getText());
    if(n==num)
    a.setText("答案正确");
    else
    a.setText("答案错误");
    }
    }

    2.

    编写一个程序,计算投资值在给定利率以及给定年数下的未来值。计算公式如下:

        投资计算.png

    (1)编写Investment类按照上面的公式计算未来值,UML类图如下:

    投资javafx.png

    (2)编写GUI界面类,使用TextField显示利率、投资总额、年数,当用户点击“计算”按钮时在文本域显示未来值。

    【试题输入输出】

    界面如下:

    import javafx.application.Application;
    import javafx.geometry.HPos;
    import javafx.geometry.Insets;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.Label;
    import javafx.scene.control.TextField;
    import javafx.scene.layout.GridPane;
    import javafx.stage.Stage;

    public class TouZi extends Application {
    public static void main(String[] args) {
    Application.launch(args);
    }

    private TextField tfInvestAmount = new TextField();
    private TextField tfYear = new TextField();
    private TextField tfYearInterest = new TextField();
    private TextField tfTotalAmount = new TextField();
    private Button bt = new Button("计算");

    public void start(Stage primaryStage) {
    GridPane pane = new GridPane();
    pane.setPadding(new Insets(11));
    pane.setHgap(5);
    pane.setVgap(5);
    pane.add(new Label("投资总额"), 0, 0);
    pane.add(tfInvestAmount, 1, 0);
    pane.add(new Label("投资年数"), 0, 1);
    pane.add(tfYear, 1, 1);
    pane.add(new Label("年利率"), 0, 2);
    pane.add(tfYearInterest, 1, 2);
    pane.add(new Label("未来值"), 0, 3);
    pane.add(tfTotalAmount, 1, 3);
    GridPane.setHalignment(bt, HPos.RIGHT);
    bt.setOnAction(e -> getTotalAmount() );
    pane.add(bt, 1, 4);

    Scene scene = new Scene(pane);
    primaryStage.setScene(scene);
    primaryStage.setTitle("投资计算器");
    primaryStage.show();
    }

    private void getTotalAmount() {
    double investAmount = Double.parseDouble(tfInvestAmount.getText());
    int year = Integer.parseInt(tfYear.getText());
    double yearInterest = Double.parseDouble(tfYearInterest.getText());
    InvestmentCompute a =new InvestmentCompute(investAmount,year,yearInterest);
    tfTotalAmount.setText(""+a.getTotalAmount());
    }

    class InvestmentCompute{
    private double investAmount;
    private int year;
    private double yearInterest;

    public InvestmentCompute() {
    }
    public InvestmentCompute(double investAmount,int year,double yearInterest) {
    this.investAmount = investAmount;
    this.year = year;
    this.yearInterest = yearInterest;
    }
    public double getInvestAmount() {
    return investAmount;
    }
    public void setInvestAmount(double investAmount) {
    this.investAmount = investAmount;
    }
    public int getYear() {
    return year;
    }
    public void setYear(int year) {
    this.year = year;
    }
    public double getYearInterest() {
    return yearInterest;
    }
    public void setYearInterest(double yearInterest) {
    this.yearInterest = yearInterest;
    }
    public double getTotalAmount() {
    return investAmount*(Math.pow((1+yearInterest/1200),year*12));
    }
    }
    }

    编写一个用户登录界面,如下:

    登录1.png

    【试题输入输出】

    假设有效用户名为“张三”、有效密码为“123456”。当用户点击“重置”按钮时,清空用户名和密码;当用户输入错误的用户名或者密码时,显示错误提示界面;当用户输入正确的用户名和密码时,显示欢迎界面。

           

    import javafx.application.Application;
    import javafx.geometry.HPos;
    import javafx.geometry.Insets;
    import javafx.geometry.Pos;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.Label;
    import javafx.scene.control.TextField;
    import javafx.scene.layout.GridPane;
    import javafx.scene.layout.HBox;
    import javafx.scene.layout.Pane;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;

    public class Denglu extends Application {
    public static void main(String[] args) {
    Application.launch(args);
    }

    private TextField tfName = new TextField();
    private TextField tfPassword = new TextField();
    private Button btDL = new Button("登录");
    private Button btCZ = new Button("重置");

    public void start(Stage primaryStage) {
    GridPane pane = new GridPane();
    pane.setPadding(new Insets(11));
    pane.setHgap(5);
    pane.setVgap(5);
    pane.add(new Label("用户名"), 0, 0);
    pane.add(tfName, 1, 0);
    pane.add(new Label("密 码"), 0, 1);
    pane.add(tfPassword , 1, 1);

    HBox hbox = new HBox(15);
    hbox.setPadding(new Insets(10,10,0,0));
    hbox.setAlignment(Pos.CENTER_LEFT);
    hbox.getChildren().addAll(btDL,btCZ);
    btDL.setOnAction(e ->loginAction( new Stage()));
    btCZ.setOnAction(e ->resetAction());
    pane.add(hbox, 1, 2);

    Scene scene = new Scene(pane);
    primaryStage.setScene(scene);
    primaryStage.setTitle("登录界面");
    primaryStage.show();
    }

    public void loginAction(Stage primaryStage) {
    String Name = tfName.getText();
    String Password = tfPassword.getText();
    if(Name.equals("张三")&&Password.equals("123456"))
    primaryStage.setScene(new Scene(new ApplicationForm("欢迎你,张三"),250,120));
    else
    primaryStage.setScene(new Scene(new ApplicationForm("用户名或密码错误"),250,120));
    primaryStage.show();
    }

    class ApplicationForm extends StackPane{
    public ApplicationForm(String n) {
    this.getChildren().add(new Label(n));
    this.setPadding(new Insets(10));
    }
    }

    public void resetAction() {
    tfName.setText("");
    tfPassword.setText("");
    }
    }

     4

    编写程序,实现在面板上移动小球,界面如下:

    圆-移动.png

    提示:使用javafx.scene.shape.Circle类绘制圆。

    【试题输入输出】

    点击“向左”、“向右”、“向上”和“向下”按钮,可以向相应的方向移动小球。

    import javafx.application.Application;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.geometry.Insets;
    import javafx.geometry.Pos;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.layout.BorderPane;
    import javafx.scene.layout.HBox;
    import javafx.scene.layout.Pane;
    import javafx.scene.layout.StackPane;
    import javafx.scene.paint.Color;
    import javafx.scene.shape.Circle;
    import javafx.stage.Stage;

    public class MoveCircle extends Application {
    public static void main(String[] args) {
    Application.launch(args);
    }

    Circle circle = new Circle(250,200,80);
    public void start(Stage primaryStage) {
    circle.setStroke(Color.BLACK);
    circle.setFill(Color.LIGHTBLUE);
    Pane pane = new Pane();
    pane.setPadding(new Insets(5));
    pane.setStyle("-fx-border-color:black;-fx-background-color:white");
    pane.getChildren().add(circle);

    HBox hbox = new HBox();
    hbox.setPadding(new Insets(10));
    hbox.setStyle("-fx-background-color:lightblue");
    Button btLeft = new Button("向左");
    btLeft.setOnAction(new LeftHandler());
    Button btRight = new Button("向右");
    btRight.setOnAction(new RightHandler());
    Button btTop = new Button("向上");
    btTop.setOnAction(new TopHandler());
    Button btBottom = new Button("向下");
    btBottom.setOnAction(new BottomHandler());
    hbox.getChildren().addAll(btLeft ,btRight ,btTop ,btBottom);
    hbox.setAlignment(Pos.CENTER);
    hbox.setSpacing(5);

    BorderPane bpane = new BorderPane();
    bpane.setPadding(new Insets(5));
    bpane.setStyle("-fx-border-color:green");
    bpane.setCenter(pane);
    bpane.setBottom(hbox);

    Scene scene = new Scene(bpane,500,450);
    primaryStage.setScene(scene);
    primaryStage.show();
    }

    class LeftHandler implements EventHandler<ActionEvent>{
    public void handle(ActionEvent event) {
    circle.setCenterX(circle.getCenterX()-5);
    circle.setCenterY(circle.getCenterY());
    }
    }

    class RightHandler implements EventHandler<ActionEvent>{
    public void handle(ActionEvent event) {
    circle.setCenterX(circle.getCenterX()+5);
    circle.setCenterY(circle.getCenterY());
    }
    }

    class TopHandler implements EventHandler<ActionEvent>{
    public void handle(ActionEvent event) {
    circle.setCenterX(circle.getCenterX());
    circle.setCenterY(circle.getCenterY()-5);
    }
    }

    class BottomHandler implements EventHandler<ActionEvent>{
    public void handle(ActionEvent event) {
    circle.setCenterX(circle.getCenterX());
    circle.setCenterY(circle.getCenterY()+5);
    }
    }
    }

    5

    编写一个简单的计算器,完成加、减、乘、除的功能。

    【试题输入输出】

    效果图如下:

    import javafx.application.Application;
    import javafx.geometry.Insets;
    import javafx.geometry.Pos;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.Label;
    import javafx.scene.control.TextField;
    import javafx.scene.layout.BorderPane;
    import javafx.scene.layout.HBox;
    import javafx.stage.Stage;

    public class Calculator extends Application {
    public static void main(String[] args) {
    Application.launch(args);
    }

    private TextField tf1 = new TextField();
    private TextField tf2 = new TextField();
    private TextField tf3 = new TextField();
    private Label a = new Label(" ");
    private Label b = new Label("=");
    private Button bt1 = new Button("加");
    private Button bt2 = new Button("减");
    private Button bt3 = new Button("乘");
    private Button bt4 = new Button("除");

    public void start(Stage primaryStage) {
    BorderPane pane = new BorderPane();
    pane.setPadding(new Insets(10));
    HBox hb1 = new HBox(5);
    hb1.getChildren().addAll(tf1,a,tf2,b,tf3);
    a.setPadding(new Insets(5,0,5,0));
    b.setPadding(new Insets(5,0,5,0));
    HBox hb2 = new HBox(7);
    hb2.setPadding(new Insets(10));
    hb2.getChildren().addAll(bt1,bt2,bt3,bt4);
    bt1.setOnAction(e->JiaCount());
    bt2.setOnAction(e->JianCount());
    bt3.setOnAction(e->ChengCount());
    bt4.setOnAction(e->ChuCount());
    pane.setTop(hb1);
    pane.setBottom(hb2);
    hb2.setAlignment(Pos.CENTER);

    Scene scene = new Scene(pane);
    primaryStage.setScene(scene);
    primaryStage.setTitle("计算器");
    primaryStage.show();
    }

    public void JiaCount() {
    a.setText("+");
    double num = Double.parseDouble(tf1.getText())+Double.parseDouble(tf2.getText());
    tf3.setText(""+num);
    }

    public void JianCount() {
    a.setText("-");
    double num = Double.parseDouble(tf1.getText())-Double.parseDouble(tf2.getText());
    tf3.setText(""+num);
    }

    public void ChengCount() {
    a.setText("*");
    double num = Double.parseDouble(tf1.getText())*Double.parseDouble(tf2.getText());
    tf3.setText(""+num);
    }

    public void ChuCount() {
    a.setText("/");
    double num = Double.parseDouble(tf1.getText())/Double.parseDouble(tf2.getText());
    tf3.setText(""+num);
    }
    }

    6.

    编写程序,在场景中显示一个圆,在文本框中输入圆的半径,点击“确定”按钮后,改变圆的大小。

    设置圆的半径7.png            

    import javafx.application.Application;
    import javafx.geometry.Insets;
    import javafx.geometry.Pos;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.TextField;
    import javafx.scene.layout.BorderPane;
    import javafx.scene.layout.HBox;
    import javafx.scene.layout.StackPane;
    import javafx.scene.paint.Color;
    import javafx.scene.shape.Circle;
    import javafx.stage.Stage;

    public class ChangeCircle extends Application {
    public static void main(String[] args) {
    Application.launch(args);
    }

    Circle circle = new Circle(50);
    private TextField tf = new TextField();
    private Button bt = new Button("确定");

    public void start(Stage primaryStage) {
    circle.setStroke(Color.BLACK);
    circle.setFill(Color.WHITE);
    StackPane pane = new StackPane();
    pane.setStyle("-fx-border-color:blue;-fx-background-color:white");
    pane.getChildren().add(circle);

    HBox hbox = new HBox(10);
    hbox.setPadding(new Insets(11));
    hbox.getChildren().addAll(tf,bt);
    hbox.setAlignment(Pos.CENTER);
    bt.setOnAction(e -> Change());
    hbox.setSpacing(5);

    BorderPane bpane = new BorderPane();
    bpane.setPadding(new Insets(5));
    bpane.setCenter(pane);
    bpane.setBottom(hbox);

    Scene scene = new Scene(bpane,350,250);
    primaryStage.setScene(scene);
    primaryStage.show();
    }

    public void Change() {
    double Radius = Double.parseDouble(tf.getText());
    circle.setRadius(Radius);
    }
    }

  • 相关阅读:
    asp.net保存远程图片
    JS中的转义字符
    SQL_统计某列的和
    SQL Server 出错自动回滚
    SQL中的循环语句_类似FOR循环
    转 C# 控制IE
    抓取html 写正则
    正则截取内容
    用于测试的字符串
    C#关闭IE相应的窗口 .
  • 原文地址:https://www.cnblogs.com/w670973122/p/10818260.html
Copyright © 2020-2023  润新知