JAVA实验三

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

天津理工大学实验报告

1.

源代码:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class oyf10_3{

public static void main(String args[])

{ ComputerFrame fr=new ComputerFrame();

fr.setTitle("计算");

}

}

class ComputerFrame extends JFrame implements ActionListener{ JTextField text1,text2,text3;

JButton button1,button2,button3,button4;

JLabel label;

public ComputerFrame(){

setLayout(new FlowLayout());

text1=new JTextField(10);

text2=new JTextField(10);

text3=new JTextField(10);

label=new JLabel(" ",JLabel.CENTER);

label.setBackground(Color.green);

add(text1);

add(label);

add(text2);

add(text3);

button1=new JButton("加");

button2=new JButton("减");

button3=new JButton("乘");

button4=new JButton("除");

add(button1);

add(button2);

add(button3);

add(button4);

button1.addActionListener(this);

button2.addActionListener(this);

button3.addActionListener(this);

button4.addActionListener(this);

setSize(300,320);

setVisible(true);

addWindowListener(new WindowAdapter()

{ public void windowClosing(WindowEvent e)

{ System.exit(0);

}

});

validate();

}

public void actionPerformed(ActionEvent e)

{ double n;

if(e.getSource()==button1)

{ double n1,n2;

try{ n1=Double.parseDouble(text1.getText());

n2=Double.parseDouble(text2.getText());

n=n1+n2;

text3.setText(String.valueOf(n));

label.setText("+");

}

catch(NumberFormatException ee)

{ text3.setText("请输入数字字符");

}

}

else if(e.getSource()==button2)

{ double n1,n2;

try{ n1=Double.parseDouble(text1.getText());

n2=Double.parseDouble(text2.getText());

n=n1-n2;

text3.setText(String.valueOf(n));

label.setText("-");

}

catch(NumberFormatException ee)

{ text3.setText("请输入数字字符");

}

}

else if(e.getSource()==button3)

{double n1,n2;

try{ n1=Double.parseDouble(text1.getText());

n2=Double.parseDouble(text2.getText());

n=n1*n2;

text3.setText(String.valueOf(n));

label.setText("*");

}

catch(NumberFormatException ee)

{ text3.setText("请输入数字字符");

}

}

else if(e.getSource()==button4)

{double n1,n2;

try{ n1=Double.parseDouble(text1.getText());

n2=Double.parseDouble(text2.getText());

n=n1/n2;

text3.setText(String.valueOf(n));

label.setText("/");

}

catch(NumberFormatException ee)

{ text3.setText("请输入数字字符");

}

}

validate();

}

}

相关文档
最新文档