java GUI

1.java

編寫程序,隨機生成兩個數,用戶輸入兩個數的和,並進行評判。程序的初始界面以下:app

隨機出題1.png

點擊「獲取題目」,隨機生成兩個100之內的int類型的數,界面以下:dom

隨機出題2.png

提示:ui

(1)使用java.util.Random類的nextInt(int n)生成一個100之內的隨機數。nextInt(int n) 的做用: 返回一個介於 0(包括)和指定值n(不包括)之間均勻分佈的 int 值。this

(2)Integer類的靜態方法parseInt(String str),能夠將字符串轉換爲int類型的整數。spa

【試題輸入輸出】

當用戶輸入一個正確的答案時,點擊「確認答案」按鈕,顯示以下界面:rest

隨機出題3.png

 

當用戶輸入一個錯誤的答案時,點擊「確認答案」按鈕,顯示以下界面:orm

隨機出題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;ip

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("答案錯誤");
}
}ci

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); }}

相關文章
相關標籤/搜索