JavaFX问答式计算器
来源:互联网 发布:如何做一名数据分析师 编辑:程序博客网 时间:2024/05/22 15:04
效果展示
设计Counter类:
package sample;
/**
* Created by shenqianqian on 17/10/14.
*/
public class Counter {
private double num1=0.0;
private double num2=0.0;
private char flag;
public String[] intS;
public Counter(String text){
if(text.contains("+")){//判断输入文本中的计算符号
intS=text.split("\\+");//用正则表达式去掉字符串中的符号
this.flag='+';
this.num1=Integer.parseInt(intS[0]);
this.num2=Integer.parseInt(intS[1]);
}
else if(text.contains("-")){
intS=text.split("\\-");
this.flag='-';
this.num1=Integer.parseInt(intS[0]);
this.num2=Integer.parseInt(intS[1]);
}
else if(text.contains("*")){
intS=text.split("\\*");
this.flag='*';
this.num1=Integer.parseInt(intS[0]);
this.num2=Integer.parseInt(intS[1]);
}
else {
intS=text.split("\\D");
this.flag='/';
this.num1=Integer.parseInt(intS[0]);
this.num2=Integer.parseInt(intS[1]);
}
}
public double getResult(){//计算结果
switch (this.flag){
case '+':
return num1+num2;
case '-':
return num1-num2;
case '*':
return num1*num2;
case '/':
return num1/num2;
default:
return 0;
}
}
}
设计Main类
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main extends Application {
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
// Panel p to hold the label and text field
BorderPane paneForTextField = new BorderPane();
paneForTextField.setPadding(new Insets(5, 5, 5, 5));
paneForTextField.setStyle("-fx-border-color: green");
paneForTextField.setLeft(new Label("请输入: "));
TextField tf = new TextField();
tf.setAlignment(Pos.BOTTOM_RIGHT);
paneForTextField.setCenter(tf);
BorderPane mainPane = new BorderPane();
// Text area to display contents
TextArea ta = new TextArea();
mainPane.setCenter(new ScrollPane(ta));
mainPane.setTop(paneForTextField);
// Create a scene and place it in the stage
Scene scene = new Scene(mainPane, 450, 200);
primaryStage.setTitle("CounterText "); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
tf.setOnAction(event -> {//为文本框设置响应时间
String text=tf.getText();
//正则表达式去除汉字
String reg = "[\u4e00-\u9fa5]";
Pattern pat = Pattern.compile(reg);
Matcher mat=pat.matcher(text);
String rStr = mat.replaceAll("");
Counter counter=new Counter(rStr);
//取结果
double res=counter.getResult();
//文本域展示
ta.setText(rStr+" = "+res+'\n');
});
}
}
- JavaFX问答式计算器
- linux 问答式学习
- Java 问答式教程
- JavaFx
- JavaFX
- JavaFX
- javaFX
- JavaFx
- JAVAFX
- javafx
- JavaFX
- JavaFX
- javaFX
- javaFX
- JavaFX
- JavaFX
- Hive开发经验问答式总结
- Hive开发经验问答式总结
- 现在广播通知:使用用友软件的亲们注意了,官方有重要紧急通知
- PCIe SSD飞入寻常百姓家靠什么?
- Mac monkey环境搭建
- redis入门
- Unity最新版打包AssetBundle和加载的方法
- JavaFX问答式计算器
- 第二章:HTML基础
- MFC框架界面开发(一):视图和框架
- CF 376D. Maximum Submatrix 2 枚举+前缀和
- 数字图像处理之二维图像的傅里叶变换(2)
- HBase概念
- 第五周 项目3
- CodeForces 760APetr and a calendar
- 关于Integer类的学习