下面将介绍JavaFx的第二个实例,制作简单的计算器(真的只是一个简简单单的计算机,有很多功能还没有实现,有兴趣的同学可以自己完善,之后有空的话也会详细讲一下,程序有bug大家可以多多提出哈)
package com.example.chatclient;
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.control.TextField;
import javafx.scene.effect.DropShadow;
import javafx.scene.input.DragEvent;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Border;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.shape.Circle;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
public class Calculator extends Application {
double result = 0;//结果
String Laststr = "0";//上一字符,用于保存输入的数字
TextField textField1;//输出窗口
char LastOperator = ' ';//上一个运算符
String show= new String();
@Override
public void start(Stage primaryStage){
//1>设置上方栏
TextField textField = new TextField("Calculator");
textField.setFont(new Font(30));//设置字体大小为30
textField.setAlignment(Pos.CENTER);//将文本居中
textField.setEditable(false);//使文本不可编辑
textField.setStyle("-fx-background-color: #B65C61;-fx-text-fill: white");//设置字体以及填充色
textField.setPrefWidth(300);//设置文本框宽度
textField.setPrefHeight(85);//设置文本框高度
//设置按钮及其属性
Button f1 = new Button("-");
Button f2 = new Button("·");
Button f3 = new Button("□");
f1.setPrefWidth(85);//设置按钮大小:宽
f1.setPrefHeight(85);//设置按钮大小:高
f2.setPrefWidth(85);
f2.setPrefHeight(85);
f3.setPrefWidth(85);
f3.setPrefHeight(85);
f1.setStyle("-fx-background-color: #B65C61;-fx-font-size: 30");//设置按钮填充色以及大小
f2.setStyle("-fx-background-color: #B65C61;-fx-font-size: 30");
f3.setStyle("-fx-background-color: #B65C61;-fx-font-size: 30");
//设计鼠标置于按钮上方时按钮效果
f1.setOnMouseEntered(o->((Button)o.getSource()).setStyle("-fx-border-width: 1;-fx-border-color: blue;-fx-font-size: 30"));
f2.setOnMouseEntered(o->((Button)o.getSource()).setStyle("-fx-border-width: 1;-fx-border-color: blue;-fx-font-size: 30"));
f3.setOnMouseEntered(o->((Button)o.getSource()).setStyle("-fx-border-width: 1;-fx-border-color: blue;-fx-font-size: 30"));
f1.setOnMouseExited(o->((Button)o.getSource()).setStyle("-fx-background-color: #B65C61;-fx-font-size: 30"));
f2.setOnMouseExited(o->((Button)o.getSource()).setStyle("-fx-background-color: #B65C61;-fx-font-size: 30"));
f3.setOnMouseExited(o->((Button)o.getSource()).setStyle("-fx-background-color: #B65C61;-fx-font-size: 30"));
//设计单击时按钮效果
f1.setOnAction(o->{
primaryStage.setIconified(true);//最小化窗口
});
f2.setOnAction(o->{//清零显示屏
result = 0;
Laststr = "0";
LastOperator = ' ';
show = new String();
textField1.setText("0.0");
});
f3.setOnAction(o->{
primaryStage.close();//关闭窗口
});
//置于HBox中
HBox hBox = new HBox(2);//设置元素间隔为2
hBox.setPadding(new Insets(2,2,2,2));
hBox.getChildren().addAll(f1,textField,f2,f3);
//2>设置显示器
textField1 = new TextField("0.0");//定义显示器并设置初始值
textField1.setAlignment(Pos.BASELINE_RIGHT);//设置显示内容右对齐
textField1.setStyle("-fx-background-color: #D9C1B2;-fx-border-width: 0;-fx-font-size: 20");
//3>设置键盘
//设置按钮
String string = "789+456-123*0.=/";
Button[] buttonarray = new Button[16];
for(int i=0;i<16;i++){
buttonarray[i]= new Buttons(string.charAt(i)+"");
buttonarray[i].setOnMouseEntered(o->{
((Button)o.getSource()).setStyle("-fx-background-color: #B65C61");
});
buttonarray[i].setOnMouseExited(o->{
((Button)o.getSource()).setStyle("-fx-border-width :0");
});
buttonarray[i].setOnAction(new CalculatorHandler());//为按钮添加触发事件
}
//填充按钮到单元格
GridPane gridPane = new GridPane();
gridPane.setVgap(3);//设置单元格之间的垂直间隙
gridPane.setHgap(2);//设置单元格之间的水平间隙
gridPane.setPadding(new Insets(0,2,2,2));//设置girdPane距离上边,右边,下边,左边的距离
int count=0;
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
gridPane.add(buttonarray[count++],j,i);
//将各元素放在BorderPane中
BorderPane borderPane = new BorderPane();
borderPane.setTop(hBox);
borderPane.setStyle("-fx-background-color: #D9C1B2;-fx-border-color: #B65C61;-fx-border-width: 8");
borderPane.setCenter(textField1);
borderPane.setBottom(gridPane);
DragWindowHandler handler = new DragWindowHandler(primaryStage);//创建DragWindowHandler
borderPane.setOnMouseDragged(handler::handle2);//设置鼠标拖动事件,鼠标放开时
borderPane.setOnMouseReleased(handler::handle1);//设置鼠标拖动事件,鼠标拖动时
//置入舞台
Scene scene = new Scene(borderPane);
primaryStage.setTitle("Calculator");
primaryStage.initStyle(StageStyle.UNDECORATED);//设置舞台属性,隐藏舞台自带的上边栏
primaryStage.setScene(scene);
primaryStage.setWidth(500);
primaryStage.setHeight(600);
primaryStage.show();
}
public static void main(String[] args){launch(args);}
class Buttons extends Button{
public Buttons(String args){
super(args);//调用父类的构造方法
setPrefWidth(130);//设置每个单元格的宽度
setPrefHeight(90);//设置每个单元格的长度
setFont(new Font(30));//设置字体大小
}
}
public class DragWindowHandler{//用于设置拖拽事件
private Stage primaryStage;
private double oldStageX;
private double oldStageY;
private double oldScreenX;
private double oldScreenY;
private boolean move = true;
public DragWindowHandler(Stage primaryStage){
this.primaryStage=primaryStage;
}
public void handle2(MouseEvent e){
if(move){//获取旧坐标
this.oldStageX = this.primaryStage.getX();
this.oldStageY = this.primaryStage.getY();
this.oldScreenX = e.getScreenX();
this.oldScreenY = e.getScreenY();
move=false;
}
else {
this.primaryStage.setX(e.getScreenX() - this.oldScreenX + this.oldStageX);//设置新X轴
this.primaryStage.setY(e.getScreenY() - this.oldScreenY + this.oldStageY);//设置新Y轴
}
}
public void handle1(MouseEvent e){move = true;}
}
class CalculatorHandler implements EventHandler{//设置按钮点击事件
@Override
public void handle(ActionEvent event){
String currentButton = ((Button)event.getSource()).getText();//获取目前点击的按钮
show+=currentButton;//展示框中展示的字符串
textField1.setText(show);
switch(currentButton){
case "0": case"1": case"2": case"3": case"4": case"5": case"6": case"7": case"8": case"9": case".":
if(Laststr.equals("0"))
Laststr = currentButton;//如果上一个输入的是0的话,刷新Laststr
else Laststr += currentButton;break;//小数有奇效
default:
compute();//计算
LastOperator=currentButton.charAt(0);
if(LastOperator=='=')compute();//等于时再计算一遍,因为即时性
}
}
private void compute(){//用于处理输入的数字
double Number = Double.parseDouble(Laststr);
System.out.println(Laststr);
Laststr = "0";
int o=0;
switch(LastOperator){
case ' ': result = Number;break;
case '+': result += Number;break;
case '-': result -= Number;break;
case '*': result *= Number;break;
case '/': result /= Number;break;
case '=':
textField1.setText(result + " ");
result=0;
LastOperator=' ';
Laststr = "0";
show= new String();break;
default:break;
}
}
}
}