目录
1 实验名称
Java Swing使用MVC结构及事件的处理
2 实验目的
继续熟练掌握在IDEA中调试代码
掌握Java Swing中事件处理模型
掌握MVC结构的概念及实现
掌握异常的处理
掌握常用对话框的使用
3 实验源代码
Application.Java:
public class Application {
public static void main(String[] args) {
MyJFrame myJFrame = new MyJFrame();
myJFrame.init();
}
}
MyJFrame.java:
public class MyJFrame extends JFrame {
private JPanel jPanel = null;
private JTextField tf_sideA = null;///or tfSideA
private JTextField tf_sideB = null;
private JTextField tf_sideC = null;
private JButton btn_compute = null;
private JTextArea ta_showResult = null;
//定义监听器对象
private MyListener myListener = null;
void init(){
setTitle("使用MUC结构计算三角形面积");
setBounds(200,300,300,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
jPanel = new JPanel();
jPanel.add(new JLabel("边A: "));
tf_sideA = new JTextField(8);
jPanel.add(tf_sideA);
jPanel.add(new JLabel("边B: "));
tf_sideB = new JTextField(8);
jPanel.add(tf_sideB);
jPanel.add(new JLabel("边C: "));
tf_sideC = new JTextField(8);
jPanel.add(tf_sideC);
btn_compute = new JButton("计算面积");
jPanel.add(btn_compute);
add(jPanel, BorderLayout.NORTH);
ta_showResult = new JTextArea();
add(ta_showResult,BorderLayout.CENTER);
validate();
myListener = new MyListener();//创建监听器对象
btn_compute.addActionListener(myListener);//在按钮上添加/绑定事件监听器对象
}
//定义监听器的类
private class MyListener implements ActionListener{
//对于用户事件进行逻辑处理的方法
@Override
public void actionPerformed(ActionEvent e) {
//创建数据模型对象,三角形对象
Triangle triangle = new Triangle();
//提取三个文本框用户输入的数据
double sideA = Double.parseDouble(tf_sideA.getText().toString().trim());
double sideB = Double.parseDouble(tf_sideB.getText().toString().trim());
double sideC = Double.parseDouble(tf_sideC.getText().toString().trim());
triangle.setSides(sideA,sideB,sideC);//将提取出来的三条边数据存放到模型对象中
double resultArea = triangle.getArea();
ta_showResult.setText("三角形"+sideA+","+sideB+","+sideC+"的面积:"+resultArea);
}
}
}
Triangle.java:
public class Triangle {
private double sideA;
private double sideB;
private double sideC;
private boolean isTriangle(double sideA,double sideB,double sideC){
if(((sideA+sideB)>sideC)&&((sideA+sideC)>sideB)&&((sideB+sideC)>sideA)){
return true;
}else{
return false;
}
}
public void setSides(double sideA,double sideB,double sideC){
if(isTriangle(sideA,sideB,sideC)){
this.sideA = sideA;
this.sideB = sideB;
this.sideC = sideC;
}else{
System.out.println(sideA+","+sideB+","+sideC+"这三条边不能构成三角形!");
System.exit(0);
}
}
Triangle(){
}
Triangle(double sideA,double sideB,double sideC){
if(isTriangle(sideA,sideB,sideC)){
this.sideA = sideA;
this.sideB = sideB;
this.sideC = sideC;
}else{
System.out.println(sideA+","+sideB+","+sideC+"这三条边不能构成三角形!");
System.exit(0);//
}
}
public double getArea(){
double p =(sideA+sideB+sideC)/2;
double area = Math.sqrt(p*(p-sideA)*(p-sideB)*(p-sideC));
System.out.println(sideA+","+sideB+","+sideC+"其三边构成面积;"+area);
return area;
}
}
MyJFrame.java:
// 为tf_sideC添加事件监听处理
tf_sideC.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int n = JOptionPane.showConfirmDialog(MyJFrame.this,
"are right or not","sure",
JOptionPane.YES_NO_CANCEL_OPTION);
if(n == JOptionPane.YES_OPTION){
}
if(n == JOptionPane.NO_OPTION){
tf_sideA.setText(null);
tf_sideB.setText(null);
tf_sideC.setText(null);
}
if(n == JOptionPane.CANCEL_OPTION){
tf_sideA.setText(null);
tf_sideB.setText(null);
tf_sideC.setText(null);
}
}
});//添加监听器对象匿名
btn_compute = new JButton("计算面积");
jPanel.add(btn_compute);
add(jPanel, BorderLayout.NORTH);
ta_showResult = new JTextArea();
add(ta_showResult,BorderLayout.CENTER);
validate();
myListener = new MyListener();//创建监听器对象
btn_compute.addActionListener(myListener);//在按钮上添加/绑定事件监听器对象
}
//定义监听器的类
private class MyListener implements ActionListener{
//对于用户事件进行逻辑处理的方法
@Override
public void actionPerformed(ActionEvent e){
//创建数据模型对象,三角形对象
Triangle triangle = new Triangle();
double sideA = 0;
double sideB = 0;
double sideC = 0;//提取三个文本框用户输入的数据
try {
sideA = Double.parseDouble(tf_sideA.getText().toString().trim());
sideB = Double.parseDouble(tf_sideB.getText().toString().trim());
sideC = Double.parseDouble(tf_sideC.getText().toString().trim());
}catch(NumberFormatException numberFormatException) {
System.out.println("数据异常~");
ta_showResult.setText("no");
//弹出对话框
JOptionPane.showMessageDialog(MyJFrame.this,
"no", "警告",
JOptionPane.WARNING_MESSAGE);
triangle.isTriangle = false;
}
if((sideA<0)||(sideB<0)||(sideC<0)) {
ta_showResult.setText("no");
tf_sideA.setText(null);
tf_sideB.setText(null);
tf_sideC.setText(null);
}else{
// triangle.setSides(sideA,sideB,sideC);//将提取出来的三条边数据存放到模型对象中
// triangle.isTriangle = triangle.isTriangle(sideA,sideB,sideC);
triangle.setSideA(sideA);
triangle.setSideB(sideB);
triangle.setSideC(sideC);
triangle.isTriangle = triangle.isTriangle();
if(triangle.isTriangle){
double resultArea = triangle.getArea();
ta_showResult.setText("三角形"+sideA+","+sideB+","+sideC+"的面积:"+resultArea);
}else{
ta_showResult.setText("no triangle");
tf_sideA.setText(null);
tf_sideB.setText(null);
tf_sideC.setText(null);
}
}
}
}
}
4 实验运行结果图
5 总结
模型-视图-控制器简称为MVC,其目的是以会话的形式提供方便的GUI支持。本实验中,编写了一个封装三角形的类(模型角色),然后再编写一个窗口,窗口使用3个文本框和一个文本区为三角形对象中的数据提供视图,其中3个文本框用来显示和更新三角形对象的三个边的长度。从面向对象的角度看,MVC结构可以使程序更具有对象化的特性。创建一个消息对话框,其中参数parentComponent指定对话框可见时的位置,如果它为null,对话框会在屏幕的正前面居中显示。