一、计算器实验报告
一、 实验名称及其要求:
A) 名称: java计算器的设计
B) 要求:
1.实验目的:图形界面设计。
熟悉java.awt包中的组件,掌握图形界面设计方法,理解委托事件处理模型。
2.题意:
请设计并实现Windows系统中“计算器”的窗口及功能。
3.实验要求:
(1)设计图形界面添加菜单:窗口上添加各种组件及菜单,并处理组件及菜单的事件监听程序。
(2)运算:实现多种运算,保证运算正确性。
二、实验步骤
先新建两个类,一个类是普通型计算器的Count1,另一个类是科学性计算器的Count2
一)、普通型计算器
1、先利用Java各种图形组件完成计算的整体界面,在界面中添加按钮以及菜单项,如图
① 先定义各按钮以及菜单、菜单项
JPanel p2= new JPanel();
JTextField jt2 = new JTextFiled();
JMenuBar jmb = new JMenuBar () ;
JMenu check = new JMenu ("查看(V)") ;
JMenu edit = new JMenu ("编辑(E)") ;
JMenu help = new JMenu ("帮助(H)") ;
JMenuItem help_about = new JMenuItem("关于") ;
JMenuItem help_help = new JMenuItem("操作说明") ;
JMenuItem exit = new JMenuItem("退出") ;
JRadioButton rb2 = new JRadioButton("标准型",true) ;
JRadioButton rb1 = new JRadioButton("科学型") ;
ButtonGroup bg = new ButtonGroup() ;
按钮的定义形式如下:JButton b = new JButton(”i”),i表示各按钮上的字符
将各按钮添加到面板p2中
至于添加按钮进面板的由于比较简单,这里就不做说明了。
将文本框放在框架的南部,而面板放在框架的中间具体代码实现如下:
Container container =this.getContentPane() ;
BorderLayout border = new BorderLayout(0,0) ;
container.setLayout(border) ;
container.add(jt2,BorderLayout.NORTH) ;
container.add(p2,BorderLayout.SOUTH) ;
②各按钮以及菜单项添加监听事件功能,形式如下:
按钮或菜单项.addActionListener(this)
2、实现各按钮的功能
①在查看菜单中加入选择普通型计算器还是科学性计算器菜单项,具体实现选择的代码如下:
JRadioButton rb2 = new JRadioButton("标准型",true) ;
JRadioButton rb1 = new JRadioButton("科学型") ;
ButtonGroup bg = new ButtonGroup() ;
public void itemStateChanged(ItemEvent e) {
if (e.getSource() instanceof JRadioButton){
if(rb1.isSelected()){
Count1 count1 = new Count1() ;
count1.setVisible(true) ;
this.setVisible(false) ;
}}}
②帮助菜单以及菜单项功能的实现代码如下:
help_about.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "此版本仅用于测试,请在下载后24小时内\n自动删除," +
"若想继续使用请购买正版", "系统消息", JOptionPane.INFORMATION_MESSAGE) ;
}
});
help_help.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(null, "c表示清除当前操作。CE表示结果置零,重新输入。√表示根号。%表示去余数" +
"\n。+表示相加。-表示相减。*表示相乘。/表示相除。" +
"1/x表示去倒数。\n←表示后退。±表示取相反数","操作说明",JOptionPane.INFORMATION_MESSAGE) ;
}
}) ;
③按钮功能实现。先得到按钮的字符String s = e.getActionCommand() ;
定义sum=0,表示结果。
Ⅰ、如果s是0到9的字符或者是点,对于点要规定文本框上显示的点只有一各,则首先要定义点的一个变量index1来监控点的个数,具体实现代码如下:
if('0'<=s.charAt(0)&&s.charAt(0)<='9'|| e.getSource()==b15 ){//非四则运算符
if(flag)
jt2.setText(s);
else{
jt2.setText(jt2.getText()+s) ;
if(e.getSource() == b15 ){
index1++ ;
if(index1 > 1){
JOptionPane.showMessageDialog(this, "您的输入有误,请重新输入", "系统消息", JOptionPane.INFORMATION_MESSAGE) ;
jt2.setText("") ;
index1 = 0 ;
}
}
}
flag = false ;//非第一次输入
}
Ⅱ、±功能的实现。将文本框里的字符串转化为double型,如果得到的是正数则将原文本框得到的字符串前面加字符‘—’,如果得到的是负数,则将原文本框里的字符串去掉即可,代码的具体实现如下:if(s.equals("±")){
String t = jt2.getText() ;
double y = Double.parseDouble(t) ;
if(y>0)
jt2.setText("-"+t) ;
else if(y<0){
y=-y ;
t = String.valueOf(y) ;
jt2.setText(t) ;
}
else
jt2.setText(t) ;
}
Ⅲ、←功能的实现。表示退格,得文本框里的字符串,然后再去掉字符串的最后一个字符即可,代码的具体实现如下:if(s.equals("←")){
String t = jt2.getText() ;
int l = t.length() ;
t = t.substring(0, l-1) ;
jt2.setText(t) ;
}
Ⅳ、C功能的实现。表示清楚当前文本框,直接将文本框里的字符串设为空字符即可,代码的具体实现如下:if(s.equals("c"))
jt2.setText("") ;
Ⅴ、CE功能的实现。表示清楚所有的记录,将sum置为0,清楚当前的文本框,代码的具体实现如下:if(s.equals("CE")){
sum = 0 ;
jt2.setText("") ;
}
Ⅵ、1/x功能的实现。将文本框的字符串转化为double型
if (s.equals("1/x")){
double y = Double.parseDouble(jt2.getText()) ;
sum=1/y ;
jt2.setText(""+sum) ;
}
Ⅶ、-+/%*=√功能的实现。
double y = Double.parseDouble(jt2.getText()) ;
if(str.equals("+")) sum += y ;
if(str.equals("-")) sum -= y ;
if(str.equals("/")) sum /= y ;
if(str.equals("*")) sum *= y ;
if(str.equals("=")) sum = y ;
if(str.equals("%")) sum %= y ;
if(str.equals("√")) sum =Math.sqrt(y) ;
jt2.setText(""+sum) ;
二)、科学型计算器
1、先利用Java各种图形组件完成计算的整体界面,在界面中添加按钮以及菜单项,如图
(±, X?Y的功能未实现)
① 菜单以及菜单项的设置于定义与普通型的一样。按钮的设置另外再添加进一部分按钮,基本上与普通型的一样。面板的设置与普通型的做法一样。
② 各按钮监听,基本与普通型的一样。
③ 菜单项功能的实现
help_about.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "此版本仅用于测试,请在下载后24小时内\n自动删除," +
"若想继续使用请购买正版", "系统消息", JOptionPane.INFORMATION_MESSAGE) ;
}
});
help_help.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(null, "cos,sin,tan,√,1/x只能用于单独的简单运算\n" +
"X?Y,±功能未实现" ,"操作说明",JOptionPane.INFORMATION_MESSAGE) ;
}
}) ;
exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
int result = JOptionPane.showConfirmDialog(null, "是否要退出", "退出",
JOptionPane.YES_NO_OPTION) ;
if (result == JOptionPane.YES_OPTION){
System.exit(0) ;
}
}});
查看菜单项功能的实现
选择科学型或者普通型
public void itemStateChanged(ItemEvent e) {
if (e.getSource() instanceof JRadioButton)
{
if(rb2.isSelected()){
Count2 count2 = new Count2() ;
count2.setVisible(true) ;
this.setVisible(false) ;
}
}
}
退出功能
exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
int result = JOptionPane.showConfirmDialog(null, "是否要退出", "退出",
JOptionPane.YES_NO_OPTION) ;
if (result == JOptionPane.YES_OPTION){
System.exit(0) ;
}
}});
④ 按钮功能的实现
Ⅰ CE、C、←、1/x以及.功能的实现跟普通型的一样,这里就不多做说明了
Ⅱ cos、sin、tan、π功能的实现。调用Math方法就行了,具体代码实现如下:Textshow表示文本框
if (str1.equals("cos")){
double b = Double.parseDouble(Textshow.getText()) ;
a = Math.cos(b) ;
str2 = String.valueOf(a) ;
Textshow.setText(str2) ;
}
if (str1.equals("sin")){
double b = Double.parseDouble(Textshow.getText()) ;
a = Math.sin(b) ;
str2 = String.valueOf(a) ;
Textshow.setText(str2) ;
}
if (str1.equals("tan")){
double b = Double.parseDouble(Textshow.getText()) ;
a = Math.tan(b) ;
str2 = String.valueOf(a) ;
Textshow.setText(str2) ;
if (str1.equals("π")){
double b = 3.1415926535898 ;
str2 = String.valueOf(b) ;
Textshow.setText(str2) ;
}
Ⅲ 数字键、加减乘除、括号键功能的实现。点击按钮得到,保证文本框上的字符串符合运算规则。
if (e.getActionCommand().equals("="))
{
String expr = string;
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
Object result;
try {
result = engine.eval(expr);
System.out.println(result);
String str = String.valueOf(result) ;
Textshow.setText(str) ;
} catch (ScriptException e1) {
e1.printStackTrace();
}
}
三、操作过程
1、点击普通型可转化为普通型计算器
2、普通型计算器的运算
比如计算8+9
3、科学型计算器运算
四、实验心得 :对swing和awt包里的容器类、辅助类、组件类的使用进一步熟悉,系统的了解了这三大类。
package Count;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Count2 extends JFrame implements ActionListener,ItemListener
{
JMenuBar jmb = new JMenuBar () ;
JMenu check = new JMenu ("查看(V)") ;
JMenu edit = new JMenu ("编辑(E)") ;
JMenu help = new JMenu ("帮助(H)") ;
JMenuItem help_about = new JMenuItem("关于") ;
JMenuItem help_help = new JMenuItem("操作说明") ;
JMenuItem exit = new JMenuItem("退出") ;
int index1 = 0 ;//判断是否多次输入'.'
JRadioButton rb2 = new JRadioButton("标准型",true) ;
JRadioButton rb1 = new JRadioButton("科学型") ;
ButtonGroup bg = new ButtonGroup() ;
JButton b1 = new JButton("1") ;
JButton b2 = new JButton("2") ;
JButton b3 = new JButton("3") ;
JButton b4 = new JButton("4") ;
JButton b5 = new JButton("5") ;
JButton b6 = new JButton("6") ;
JButton b7 = new JButton("7") ;
JButton b8 = new JButton("8") ;
JButton b9 = new JButton("9") ;
JButton b0 = new JButton("0") ;
JButton b10 = new JButton ("/") ;
JButton b11 = new JButton ("+") ;
JButton b12 = new JButton ("-") ;
JButton b13 = new JButton ("*") ;
JButton b14 = new JButton ("=") ;
JButton b15 = new JButton (".") ;
JButton b16 = new JButton ("%") ;
JButton b17 = new JButton ("c") ;
JButton b18 = new JButton ("CE") ;
JButton b19 = new JButton ("√") ;
JButton b20 = new JButton ("M+") ;
JButton b21 = new JButton ("M-") ;
JButton b22 = new JButton ("±") ;
JButton b23 = new JButton ("←") ;
JButton b24 = new JButton ("MR") ;
JButton b25 = new JButton ("MC") ;
JButton b26 = new JButton ("MS") ;
JButton b27 = new JButton ("1/x") ;
// JTextField jt1 = new JTextField(5) ;
JTextField jt2 = new JTextField(16) ;
// JPanel p1 = new JPanel () ;
JPanel p2 = new JPanel () ;
boolean flag = true ;//第一次输入
double sum = 0 ;
String str = "=";
Count2(){
this.setJMenuBar(jmb) ;
jmb.add(check) ;
jmb.add(edit) ;
jmb.add(help) ;
help.add(help_about) ;
help.add(help_help) ;
check.add(rb1) ;
check.add(rb2) ;
bg.add(rb1) ;
bg.add(rb2) ;
check.add(exit) ;
// p1.setB(10,1) ;
this.setSize(230,270) ;
this.setLocation (200,200) ;
this.setResizable(false) ;
this.setTitle ("计算器") ;
this.setVisible(true) ;
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
GridLayout flow = new GridLayout(7,4,0,0) ;
// p1.setLayout(flow) ;
// p1.add(jt1) ;
// p1.add(jt2) ;
// p1.setSize(0,0) ;
p2.setLayout(flow) ;
p2.add(b24) ;
p2.add(b25) ;
p2.add(b26) ;
p2.add(b27) ;
p2.add(b20) ;
p2.add(b21) ;
p2.add(b22) ;
p2.add(b23) ;
p2.add(b17);
p2.add(b18);
p2.add(b19);
p2.add(b16);
p2.add(b7) ;
p2.add(b8) ;
p2.add(b9) ;
p2.add(b10) ;
p2.add(b4) ;
p2.add(b5) ;
p2.add(b6) ;
p2.add(b13) ;
p2.add(b1) ;
p2.add(b2) ;
p2.add(b3) ;
p2.add(b12) ;
p2.add(b0) ;
p2.add(b15) ;
p2.add(b14) ;
p2.add(b11) ;
Container container =this.getContentPane() ;
BorderLayout border = new BorderLayout(0,0) ;
container.setLayout(border) ;
container.add(jt2,BorderLayout.NORTH) ;
container.add(p2,BorderLayout.SOUTH) ;
b0.addActionListener(this) ;
b1.addActionListener(this) ;
b2.addActionListener(this) ;
b3.addActionListener(this) ;
b4.addActionListener(this) ;
b5.addActionListener(this) ;
b6.addActionListener(this) ;
b7.addActionListener(this) ;
b8.addActionListener(this) ;
b9.addActionListener(this) ;
b10.addActionListener(this) ;
b11.addActionListener(this) ;
b12.addActionListener(this) ;
b13.addActionListener(this) ;
b14.addActionListener(this) ;
b15.addActionListener(this) ;
b16.addActionListener(this) ;
b17.addActionListener(this) ;
b18.addActionListener(this) ;
b19.addActionListener(this) ;
b27.addActionListener(this) ;
b23.addActionListener(this) ;
b22.addActionListener(this) ;
rb1.addItemListener(this) ;
rb2.addItemListener(this) ;
help_about.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "此版本仅用于测试,请在下载后24小时内\n自动删除," +
"若想继续使用请购买正版", "系统消息", JOptionPane.INFORMATION_MESSAGE) ;
}
});
help_help.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(null, "c表示清除当前操作。CE表示结果置零,重新输入。√表示根号。%表示去余数" +
"\n。+表示相加。-表示相减。*表示相乘。/表示相除。" +
"1/x表示去倒数。\n←表示后退。±表示取相反数","操作说明",JOptionPane.INFORMATION_MESSAGE) ;
}
}) ;
exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
int result = JOptionPane.showConfirmDialog(null, "是否要退出", "退出",
JOptionPane.YES_NO_OPTION) ;
if (result == JOptionPane.YES_OPTION){
System.exit(0) ;
}
}
});
}
public static void main(String[] args) {
new Count2() ;
}
public void actionPerformed(ActionEvent e) {
String s = e.getActionCommand() ;
if(s.equals("±")){
String t = jt2.getText() ;
double y = Double.parseDouble(t) ;
if(y>0)
jt2.setText("-"+t) ;
else if(y<0){
y=-y ;
t = String.valueOf(y) ;
jt2.setText(t) ;
}
else
jt2.setText(t) ;
}
if(s.equals("←")){
String t = jt2.getText() ;
int l = t.length() ;
t = t.substring(0, l-1) ;
jt2.setText(t) ;
index1=0;
}
else if (s.equals("1/x")){
double y = Double.parseDouble(jt2.getText()) ;
if(str.equals("1/x")) sum=1/y ;
jt2.setText(""+sum) ;
str = s ;
flag = true ;
}
else if('0'<=s.charAt(0)&&s.charAt(0)<='9'|| e.getSource()==b15 ){//非四则运算符
if(flag)
jt2.setText(s);
else{
if(e.getSource() == b15 ){
index1++ ;
if(index1 > 1){
// JOptionPane.showMessageDialog(this, "您的输入有误,请重新输入", "系统消息", JOptionPane.INFORMATION_MESSAGE) ;
jt2.setText(jt2.getText()) ;
// index1 = 0 ;
}
else
jt2.setText(jt2.getText()+s) ;
}
else
jt2.setText(jt2.getText()+s) ;
}
flag = false ;//非第一次输入
}
else if(s.equals("c"))
{
jt2.setText("") ;
index1=0;
}
else if(s.equals("CE")){
sum = 0 ;
jt2.setText("") ;
index1=0;
}
else{//四则运算符
double y = Double.parseDouble(jt2.getText()) ;
if(str.equals("+")) sum += y ;
if(str.equals("-")) sum -= y ;
if(str.equals("/")) sum /= y ;
if(str.equals("*")) sum *= y ;
if(str.equals("=")) sum = y ;
if(str.equals("%")) sum %= y ;
if(str.equals("√")) sum =Math.sqrt(y) ;
// if(str.equals(".")) sum +=x*0.1 ;
// if(str.equals("c")) jt2.setText("") ;
jt2.setText(""+sum) ;
str = s ;
// jt1.setText(str) ;
flag=true ;
}
}
public void itemStateChanged(ItemEvent e) {
if (e.getSource() instanceof JRadioButton)
{
if(rb1.isSelected()){
Count1 count1 = new Count1() ;
count1.setVisible(true) ;
this.setVisible(false) ;
}
}
}
}
package Count;
import java.awt.*;
import javax.swing.* ;
import java.awt.event.*;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class Count1 extends JFrame implements ActionListener , ItemListener
{
public static void main(String[] args){
new Count1() ;
}
String string ="" ;
// JTextField jt = new JTextField() ;
//为以后添加其他的辅助功能
private TextField Textshow=new TextField(40);
// private JPanel pan1=new JPanel();
private JPanel pan2=new JPanel();
// private JButton button[] = new JButton[30] ;
JMenuBar jmb = new JMenuBar () ;
JMenu check = new JMenu ("查看(V)") ;
JMenu edit = new JMenu ("编辑(E)") ;
JMenu help = new JMenu ("帮助(H)") ;
JMenuItem help_about = new JMenuItem("关于") ;
JMenuItem help_help = new JMenuItem("操作说明") ;
JMenuItem exit = new JMenuItem("退出") ;
JRadioButton rb2 = new JRadioButton("标准型") ;
JRadioButton rb1 = new JRadioButton("科学型",true) ;
ButtonGroup bg = new ButtonGroup() ;
private JButton button1=new JButton("1");
private JButton button2=new JButton("2");
private JButton button3=new JButton("3");
private JButton button4=new JButton("4");
private JButton button5=new JButton("5");
private JButton button6=new JButton("6");
private JButton button7=new JButton("7");
private JButton button8=new JButton("8");
private JButton button9=new JButton("9");
private JButton button10=new JButton("0");
private JButton button11=new JButton("+");
private JButton button12=new JButton("-");
private JButton button13=new JButton("* ");
private JButton button14=new JButton("/");
private JButton button15=new JButton("%");
private JButton button16=new JButton("(");
private JButton button17=new JButton(")");
private JButton button18=new JButton("c");
private JButton button19=new JButton(".");
private JButton button20=new JButton("=");
private JButton button21=new JButton("CE");
private JButton button22=new JButton("←");
private JButton button23=new JButton("cos") ;
private JButton button24=new JButton("sin");
private JButton button25=new JButton("tan");
private JButton button26=new JButton("1/x");
private JButton button27=new JButton("X?Y");
private JButton button28=new JButton("±");
private JButton button29=new JButton("π");
private JButton button30=new JButton("√");
// private Font wordFont1 =new Font("TimesRoman",Font.BOLD,13);
// private Font wordFont2 =new Font("TimesRoman",Font.BOLD,30);
public Count1() //构造方法来初始化本程序的变量
{
super("我的科学计算器");
setup();
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
button6.addActionListener(this);
button7.addActionListener(this);
button8.addActionListener(this);
button9.addActionListener(this);
button10.addActionListener(this);
button11.addActionListener(this);
button12.addActionListener(this);
button13.addActionListener(this);
button14.addActionListener(this);
button15.addActionListener(this);
button16.addActionListener(this);
button17.addActionListener(this);
button18.addActionListener(this);
button19.addActionListener(this);
button20.addActionListener(this);
button21.addActionListener(this);
button22.addActionListener(this);
button23.addActionListener(this);
button24.addActionListener(this);
button25.addActionListener(this);
button26.addActionListener(this);
button27.addActionListener(this);
button28.addActionListener(this);
button29.addActionListener(this);
button30.addActionListener(this);
//this.setSize(900,900) ;
this.setLocation(200,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
setResizable(false); //设置此图形界面是不可以改变大小的
pack();
setVisible(true);
}
public void setup() //搭建次程序的图形应用界面
{
this.setJMenuBar(jmb) ;
jmb.add(check) ;
jmb.add(edit) ;
jmb.add(help) ;
help.add(help_about) ;
help.add(help_help) ;
check.add(rb1) ;
check.add(rb2) ;
bg.add(rb1) ;
bg.add(rb2) ;
check.add(exit) ;
pan2.setLayout(new GridLayout(5,6,5,6));
pan2.add(button21) ;
pan2.add(button18) ;
pan2.add(button22) ;
pan2.add(button23) ;
pan2.add(button24) ;
pan2.add(button25) ;
pan2.add(button7) ;
pan2.add(button8) ;
pan2.add(button9) ;
pan2.add(button26) ;
pan2.add(button27) ;
pan2.add(button29) ;
pan2.add(button4) ;
pan2.add(button5) ;
pan2.add(button6) ;
pan2.add(button11) ;
pan2.add(button12) ;
pan2.add(button13) ;
pan2.add(button1) ;
pan2.add(button2) ;
pan2.add(button3) ;
pan2.add(button15) ;
pan2.add(button14) ;
pan2.add(button30) ;
pan2.add(button10) ;
pan2.add(button19) ;
pan2.add(button20) ;
pan2.add(button16) ;
pan2.add(button17) ;
pan2.add(button28) ;
rb1.addItemListener(this) ;
rb2.addItemListener(this) ;
help_about.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "此版本仅用于测试,请在下载后24小时内\n自动删除," +
"若想继续使用请购买正版", "系统消息", JOptionPane.INFORMATION_MESSAGE) ;
}
});
help_help.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(null, "cos,sin,tan,√,1/x只能用于单独的简单运算\n" +
"X?Y,±功能未实现" ,"操作说明",JOptionPane.INFORMATION_MESSAGE) ;
}
}) ;
exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
int result = JOptionPane.showConfirmDialog(null, "是否要退出", "退出",
JOptionPane.YES_NO_OPTION) ;
if (result == JOptionPane.YES_OPTION){
System.exit(0) ;
}
}
});
Container container = this.getContentPane() ;
container.setLayout(new BorderLayout( ) );
container.add("North",Textshow);
container.add("Center",pan2);
}
public void actionPerformed(ActionEvent e) //事件响应的处理
{
String str1 = e.getActionCommand() ;
String str2 ;
double a ;
//三角函数处理
if (str1.equals("cos")){
double b = Double.parseDouble(Textshow.getText()) ;
a = Math.cos(b) ;
str2 = String.valueOf(a) ;
Textshow.setText(str2) ;
}
else
if (str1.equals("sin")){
double b = Double.parseDouble(Textshow.getText()) ;
a = Math.sin(b) ;
str2 = String.valueOf(a) ;
Textshow.setText(str2) ;
}
else
if (str1.equals("tan")){
double b = Double.parseDouble(Textshow.getText()) ;
a = Math.tan(b) ;
str2 = String.valueOf(a) ;
Textshow.setText(str2) ;
}
else if (str1.equals("1/x")){
double y = Double.parseDouble(Textshow.getText()) ;
a=1/y ;
str2 = String.valueOf(a) ;
Textshow.setText(str2) ;
}
else if (str1.equals("π")){
double b = 3.1415926535898 ;
str2 = String.valueOf(b) ;
Textshow.setText(str2) ;
}
else if (str1.equals("√")){
double b = Double.parseDouble(Textshow.getText()) ;
a = Math.sqrt(b) ;
str2 = String.valueOf(a) ;
Textshow.setText(str2) ;
}
else
if(str1.equals("←")){
string = Textshow.getText() ;
int l = string.length() ;
string = string.substring(0, l-1) ;
Textshow.setText(string) ;
}
else
if(e.getSource()==button18 || e.getSource()==button21)
{
string ="" ;
Textshow.setText(string);
}
else
{
//混合四则运算
if (e.getActionCommand().equals("="))
{
String expr = string;
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
Object result;
try {
result = engine.eval(expr);
// System.out.println(result);
String str = String.valueOf(result) ;
Textshow.setText(str) ;
} catch (ScriptException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
}
else{
string = string+ e.getActionCommand() ;
Textshow.setText(string) ;
}
}
}
public void itemStateChanged(ItemEvent e) {
if (e.getSource() instanceof JRadioButton)
{
if(rb2.isSelected()){
Count2 count2 = new Count2() ;
count2.setVisible(true) ;
this.setVisible(false) ;
}
}
}
}