用Java写一个基于MVVM的中英文单词计算器
时间: 2023-11-18 21:05:24 浏览: 137
用MVVM架构实现的计算器小程序实例,供WPF和MVVM初学者参考.
好的,我会尽力回答您的问题。首先,让我们来了解一下MVVM模式是什么。
MVVM是Model-View-ViewModel的缩写,是一种软件架构模式,它将应用程序分为三个部分:模型(数据和业务逻辑)、视图(用户界面)和视图模型(连接模型和视图的中间件)。
下面是一个基于MVVM的中英文单词计算器的Java代码示例:
Model类:
```
public class CalculatorModel {
private float operand1;
private float operand2;
private float result;
public void setOperand1(float operand1) {
this.operand1 = operand1;
}
public void setOperand2(float operand2) {
this.operand2 = operand2;
}
public float getResult() {
return result;
}
public void add() {
result = operand1 + operand2;
}
public void subtract() {
result = operand1 - operand2;
}
public void multiply() {
result = operand1 * operand2;
}
public void divide() {
if (operand2 == 0) {
result = Float.NaN;
} else {
result = operand1 / operand2;
}
}
}
```
ViewModel类:
```
public class CalculatorViewModel {
private CalculatorModel model;
private String operand1;
private String operand2;
private String result;
public CalculatorViewModel() {
model = new CalculatorModel();
}
public void setOperand1(String operand1) {
this.operand1 = operand1;
}
public void setOperand2(String operand2) {
this.operand2 = operand2;
}
public String getResult() {
return result;
}
public void add() {
model.setOperand1(Float.parseFloat(operand1));
model.setOperand2(Float.parseFloat(operand2));
model.add();
result = Float.toString(model.getResult());
}
public void subtract() {
model.setOperand1(Float.parseFloat(operand1));
model.setOperand2(Float.parseFloat(operand2));
model.subtract();
result = Float.toString(model.getResult());
}
public void multiply() {
model.setOperand1(Float.parseFloat(operand1));
model.setOperand2(Float.parseFloat(operand2));
model.multiply();
result = Float.toString(model.getResult());
}
public void divide() {
model.setOperand1(Float.parseFloat(operand1));
model.setOperand2(Float.parseFloat(operand2));
model.divide();
result = Float.toString(model.getResult());
}
}
```
View类:
```
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CalculatorView extends JFrame {
private CalculatorViewModel viewModel;
private JTextField operand1TextField;
private JTextField operand2TextField;
private JTextField resultTextField;
private JButton addButton;
private JButton subtractButton;
private JButton multiplyButton;
private JButton divideButton;
public CalculatorView() {
viewModel = new CalculatorViewModel();
operand1TextField = new JTextField(10);
operand2TextField = new JTextField(10);
resultTextField = new JTextField(10);
resultTextField.setEditable(false);
addButton = new JButton("+");
subtractButton = new JButton("-");
multiplyButton = new JButton("*");
divideButton = new JButton("/");
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
viewModel.setOperand1(operand1TextField.getText());
viewModel.setOperand2(operand2TextField.getText());
viewModel.add();
resultTextField.setText(viewModel.getResult());
}
});
subtractButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
viewModel.setOperand1(operand1TextField.getText());
viewModel.setOperand2(operand2TextField.getText());
viewModel.subtract();
resultTextField.setText(viewModel.getResult());
}
});
multiplyButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
viewModel.setOperand1(operand1TextField.getText());
viewModel.setOperand2(operand2TextField.getText());
viewModel.multiply();
resultTextField.setText(viewModel.getResult());
}
});
divideButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
viewModel.setOperand1(operand1TextField.getText());
viewModel.setOperand2(operand2TextField.getText());
viewModel.divide();
resultTextField.setText(viewModel.getResult());
}
});
JPanel panel = new JPanel(new GridLayout(4, 2));
panel.add(new JLabel("Operand 1:"));
panel.add(operand1TextField);
panel.add(new JLabel("Operand 2:"));
panel.add(operand2TextField);
panel.add(new JLabel("Result:"));
panel.add(resultTextField);
panel.add(addButton);
panel.add(subtractButton);
panel.add(multiplyButton);
panel.add(divideButton);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Calculator");
this.setContentPane(panel);
this.pack();
}
public static void main(String[] args) {
CalculatorView view = new CalculatorView();
view.setVisible(true);
}
}
```
这个计算器可以进行加、减、乘、除四种运算,视图中包含两个文本框和四个按钮,点击按钮后,视图模型会将文本框中的内容转换为浮点数,并调用计算器模型中相应的方法进行计算,最后将结果显示在结果文本框中。
希望这个代码示例对您有所帮助!
阅读全文