修改程序Example11_13.java,要求按照MVC开发模式来编写,设计一个MathListener.java,实现对ActionEvent事件的监听,不再使用窗体做监视器对象。
时间: 2024-03-11 17:48:33 浏览: 64
好的,根据您的要求,我对Example11_13.java进行了修改,按照MVC开发模式来编写,设计了一个MathListener.java来实现对ActionEvent事件的监听,不再使用窗体做监视器对象。修改后的代码如下:
MathModel.java
```java
public class MathModel {
private int num1;
private int num2;
private int result;
public void setNum1(int num1) {
this.num1 = num1;
}
public void setNum2(int num2) {
this.num2 = num2;
}
public int getResult() {
return result;
}
public void add() {
result = num1 + num2;
}
public void subtract() {
result = num1 - num2;
}
public void multiply() {
result = num1 * num2;
}
public void divide() {
result = num1 / num2;
}
}
```
MathView.java
```java
import javax.swing.*;
import java.awt.*;
public class MathView extends JFrame {
private JLabel num1Label;
private JLabel num2Label;
private JLabel resultLabel;
private JTextField num1TextField;
private JTextField num2TextField;
private JTextField resultTextField;
private JButton addButton;
private JButton subtractButton;
private JButton multiplyButton;
private JButton divideButton;
public MathView() {
setTitle("Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(4, 3));
num1Label = new JLabel("Num1:");
add(num1Label);
num1TextField = new JTextField();
add(num1TextField);
num2Label = new JLabel("Num2:");
add(num2Label);
num2TextField = new JTextField();
add(num2TextField);
resultLabel = new JLabel("Result:");
add(resultLabel);
resultTextField = new JTextField();
resultTextField.setEditable(false);
add(resultTextField);
addButton = new JButton("+");
add(addButton);
subtractButton = new JButton("-");
add(subtractButton);
multiplyButton = new JButton("*");
add(multiplyButton);
divideButton = new JButton("/");
add(divideButton);
pack();
setVisible(true);
}
public int getNum1() {
return Integer.parseInt(num1TextField.getText());
}
public int getNum2() {
return Integer.parseInt(num2TextField.getText());
}
public void setResult(int result) {
resultTextField.setText(String.valueOf(result));
}
public void addMathListener(MathListener listener) {
addButton.addActionListener(listener);
subtractButton.addActionListener(listener);
multiplyButton.addActionListener(listener);
divideButton.addActionListener(listener);
}
}
```
MathListener.java
```java
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MathListener implements ActionListener {
private MathModel model;
private MathView view;
public MathListener(MathModel model, MathView view) {
this.model = model;
this.view = view;
this.view.addMathListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
int num1 = view.getNum1();
int num2 = view.getNum2();
if (e.getSource() == view.addButton) {
model.setNum1(num1);
model.setNum2(num2);
model.add();
} else if (e.getSource() == view.subtractButton) {
model.setNum1(num1);
model.setNum2(num2);
model.subtract();
} else if (e.getSource() == view.multiplyButton) {
model.setNum1(num1);
model.setNum2(num2);
model.multiply();
} else if (e.getSource() == view.divideButton) {
model.setNum1(num1);
model.setNum2(num2);
model.divide();
}
view.setResult(model.getResult());
}
}
```
MathController.java
```java
public class MathController {
public static void main(String[] args) {
MathModel model = new MathModel();
MathView view = new MathView();
new MathListener(model, view);
}
}
```
在修改后的程序中,MathModel类负责处理数据的逻辑,MathView类负责显示用户界面,MathListener类实现对ActionEvent事件的监听,MathController类负责启动整个程序。通过MVC开发模式,将程序的不同职责分离开来,使得程序的设计更加清晰,易于维护和扩展。
阅读全文