利用JavaFX Scene Builder 2.0制作图形界面
制作完成后,将其保存,会自动保存为一个FXML文件
将fxml文件导入IDEA中,并添加JavaFX相关依赖
右键fxml右键点击此处,可以自动打开SceneBuilder,对应的更新会自动保存到fxml文件
定义启动类,呈现界面
/**
* Main 类,继承自 Application,是 JavaFX 应用程序的入口
*/
public class Main extends Application {
/**
* 主函数,用于启动 JavaFX 应用程序
*
* @param args 命令行参数
*/
public static void main(String[] args) {
// 启动 JavaFX 应用程序,传递命令行参数
launch(args);
}
/**
* 重写 start 方法,用于设置应用程序的初始界面
*
* @param stage 应用程序的主舞台
* @throws Exception 如果在加载 FXML 文件时发生异常
*/
@Override
public void start(Stage stage) throws Exception {
// 设置舞台的标题
stage.setTitle("Lgt的简易计算器");
// 创建一个 FXMLLoader 对象,用于加载 FXML 文件
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("calculator.fxml"));
// 加载 FXML 文件,并将其转换为 Parent 类型的对象
Parent root = fxmlLoader.load();
// 创建一个新的 Scene 对象,并将加载的 FXML 作为其根节点
Scene scene = new Scene(root);
// 将 Scene 设置到舞台上
stage.setScene(scene);
// 显示舞台
stage.show();
}
}
定义控制类,实现具体逻辑
public class Controller {
@FXML
private Label result;
@FXML
private Label expression;
private boolean lastIsNum = false;
private StringBuilder expressionBuilder = new StringBuilder();
@FXML
public void onClicked(ActionEvent event) {
Button button = (Button) event.getTarget();
String text = button.getText();
if (isNumeric(text) || text.equals(".")) {
handleNumericInput(text);
} else {
handleOperatorInput(text);
}
}
private void handleNumericInput(String text) {
String resultText = result.getText();
if (text.equals(".")) {
if (resultText.contains(".")) {
return; // Already contains a decimal point
}
if (resultText.isEmpty()) {
resultText = "0"; // If there's no number before the decimal point
}
}
result.setText(resultText + text);
lastIsNum = true;
}
private void handleOperatorInput(String text) {
switch (text) {
case "+":
case "-":
case "*":
case "/":
expressionBuilder.append(result.getText()).append(" ").append(text);
expression.setText(expressionBuilder.toString());
result.setText(""); // Clear the result field
break;
case "=":
expressionBuilder.append(" ").append(result.getText()).append(" = ");
expression.setText(expressionBuilder.toString());
double resultVal = calculateValue(expressionBuilder.toString().trim());
result.setText(String.valueOf(resultVal));
expressionBuilder.setLength(0); // Clear StringBuilder
break;
case "CE":
result.setText("");
break;
case "C":
result.setText("");
expression.setText("");
expressionBuilder.setLength(0);
break;
case "<=":
String currentText = result.getText();
if (!currentText.isEmpty()) {
result.setText(currentText.substring(0, currentText.length() - 1));
}
break;
case "X^2":
if(result.getText()==null)
{
result.setText("ERROR");
}
result.setText(Double.toString(Math.pow(Double.parseDouble(result.getText()), 2)));
break;
case "X^0.5":
if(result.getText()==null)
{
result.setText("ERROR");
}
result.setText(Double.toString(Math.pow(Double.parseDouble(result.getText()), 0.5)));
break;
case "1/X":
if(result.getText()==null||Double.parseDouble(result.getText())==0)
{
result.setText("ERROR");
}
result.setText(Double.toString(Math.pow(Double.parseDouble(result.getText()), -1)));
break;
case "%":
if(result.getText()==null)
{
result.setText("ERROR");
}
result.setText(Double.toString(Double.parseDouble(result.getText())*0.01));
break;
case "+/-":
if(result.getText()==null)
{
result.setText("ERROR");
}
String s = result.getText();
if(s.contains("."))
{
double val = Double.parseDouble(result.getText());
val*=-1;
result.setText(Double.toString(val));
}else {
int val = Integer.parseInt(result.getText());
val *= -1;
result.setText(Integer.toString(val));
}
break;
default:
break;
}
lastIsNum = false;
}
private double calculateValue(String s) {
String[] split = s.split(" ");
BigDecimal a = new BigDecimal(split[0]);
BigDecimal b = new BigDecimal(split[2]);
switch (split[1]) {
case "+":
return a.add(b).doubleValue();
case "-":
return a.subtract(b).doubleValue();
case "*":
return a.multiply(b).doubleValue();
case "/":
if (b.compareTo(BigDecimal.ZERO) == 0) {
return 0; // Handle division by zero
}
return a.divide(b, 10, BigDecimal.ROUND_HALF_UP).doubleValue(); // 保留10位小数并四舍五入
default:
return 0;
}
}
private boolean isNumeric(String str) {
return str.matches("-?\\d+(\\.\\d+)?");
}
}
运行代码,进行测试
完成!(这只是一个简易的计算器,更加复杂的计算大家可以自行尝试!如有Bug,欢迎指正!)