📄 calcular.java
字号:
import java.text.*;
import java.util.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class DivideByZeroException extends ArithmeticException
{
public DivideByZeroException()
{
super("试图除以0");
}
public DivideByZeroException(String s)
{
super(s);
}
}
public class Calcular extends JFrame implements ActionListener,Runnable
{
private static final double PI=3.1415926d;
private JTextField ResultField, TimeField;
private String StringOne, StringOperator, StringTwo,StringResult;
private JButton b[], ClearButton,getTimeButton;
private String names[]={"7","8","9","/","PI","abs","4","5","6","*","xcy","tan","1","2","3","-","sqrt","cos","0",".","=","+","sin","x_1"};
private String ListenerNames[]={"tan","sqrt","cos","sin","=","x_1","abs"};
private String BinaryOperatorNames="+ - * / xcy";
private String NoneListenerNames[]={"7","8","9","/","PI","4","5","6","*","xcy","1","2","3","-","0",".","+"};
private JPanel FouctionPane ;
private Container c;
private Thread thread;
private boolean TimeShowing;
private int i;
public void run()
{
while(true)
{
try{
Thread.sleep(1000);
}
catch(InterruptedException e)
{}
topaint();
}
}
public Calcular()
{
super("计算器");
this.getTimeButton = new JButton("切换时间");
FouctionPane=new JPanel();
b=new JButton[names.length];
for( i=0;i<names.length;++i)
{
b[i]=new JButton(names[i]);
b[i].addActionListener(this);
}
FouctionPane.setLayout(new GridLayout(4,6));
FouctionPane.setSize(380,300);
for(int i=0;i<names.length;i++)
{
FouctionPane.add(b[i]);
}
ClearButton=new JButton("清除");
ClearButton.addActionListener(this);
TimeField=new JTextField(20);
ResultField=new JTextField(30);
c = getContentPane();
c.setLayout(new FlowLayout());
c.add(ResultField);
c.add(TimeField);
c.add(this.getTimeButton);
c.add(ClearButton);
c.add(FouctionPane);
thread = new Thread(this);
this.TimeShowing = false;
this.getTimeButton.addActionListener(this);
this.setSize(400,300);
this.setResizable(false);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == getTimeButton)
if(!this.TimeShowing)
{
thread.start();
this.TimeShowing = !this.TimeShowing;
}
for(int j=0;j<NoneListenerNames.length;++j)
if(((JButton)(e.getSource())).getText()==NoneListenerNames[j])
{
if(NoneListenerNames[j]=="PI")
ResultField.setText(ResultField.getText()+(double)PI);
else
ResultField.setText(ResultField.getText()+NoneListenerNames[j]);
}
for(int k=0;k<ListenerNames.length;++k)
{
if(((JButton)(e.getSource())).getText()==ListenerNames[k])
{
if(((JButton)e.getSource()).getText().equals("=")==false)
{
StringOne=ResultField.getText();
if(((JButton)e.getSource()).getText()=="tan")
{
String s=" ";
try
{
s=myTan(StringOne);
}
catch(DivideByZeroException dvze)
{
JOptionPane.showMessageDialog(this,"90度没有正切值","非法输入",JOptionPane.ERROR_MESSAGE);
}
ResultField.setText(s);
}
else if(((JButton)e.getSource()).getText()=="sqrt")
{
String s=" ";
try
{
s=mySqrt(StringOne);
}
catch(DivideByZeroException dvze)
{
JOptionPane.showMessageDialog(this,"负数不能开平方","非法输入",JOptionPane.ERROR_MESSAGE);
}
ResultField.setText(s);
}
else if(((JButton)e.getSource()).getText()=="sin")
ResultField.setText(formatResult(Math.sin(Double.parseDouble(StringOne)*PI/180)));
else if(((JButton)e.getSource()).getText()=="cos")
ResultField.setText(formatResult(Math.cos(Double.parseDouble(StringOne)*PI/180)));
else if(((JButton)e.getSource()).getText()=="x_1")
ResultField.setText(formatResult(1/(Double.parseDouble(StringOne))));
else if(((JButton)e.getSource()).getText()=="abs")
ResultField.setText(formatResult(-Double.parseDouble(StringOne)));
}
else
{
StringResult=ResultField.getText();
textConvertThree(StringResult);
if(StringOperator=="+")
ResultField.setText(formatResult(Double.parseDouble(StringOne)+Double.parseDouble(StringTwo)));
else if(StringOperator=="-")
ResultField.setText(formatResult(Double.parseDouble(StringOne)-Double.parseDouble(StringTwo)));
else if(StringOperator=="*")
ResultField.setText(formatResult(Double.parseDouble(StringOne)*Double.parseDouble(StringTwo)));
else if(StringOperator=="/")
{
String str=" ";
try
{
double child=Double.parseDouble(StringOne);
double mother=Double.parseDouble(StringTwo);
str=quotient(child,mother);
}
catch(DivideByZeroException dbze)
{
JOptionPane.showMessageDialog(this,"除数不能为0","非法输入",JOptionPane.ERROR_MESSAGE);
}
ResultField.setText(str);
}
else if(StringOperator=="xcy")
ResultField.setText(miYunSuan(Double.parseDouble(StringOne),Double.parseDouble(StringTwo)));
StringOne=ResultField.getText();
}
}
}
if(e.getSource().equals(ClearButton))
{
ResultField.setText(" ");
StringOne=" ";
StringTwo=" ";
StringOperator=" ";
}
}
public String miYunSuan (double buttom ,double index)
{
return formatResult(Math.pow(buttom,index));
}
public String quotient (double child,double mother)
{
if(mother==0)
throw new DivideByZeroException();
return formatResult(child/mother);
}
public String formatResult(double one)
{
DecimalFormat doubleformat=new DecimalFormat("0.0000");
return doubleformat.format(one);
}
public void textConvertThree(String s)
{
StringTokenizer str=new StringTokenizer(s,BinaryOperatorNames);
StringOne=str.nextToken();
StringTwo=str.nextToken();
if(s.contains("+"))
StringOperator="+";
if(s.contains("-"))
StringOperator="-";
if(s.contains("*"))
StringOperator="*";
if(s.contains("/"))
StringOperator="/";
if(s.contains("xcy"))
StringOperator="xcy";
}
public void topaint()
{
Date today=new Date();
DateFormat f1= DateFormat.getDateInstance();
DateFormat f2= DateFormat.getTimeInstance();
TimeField.setText(f1.format(today)+ " " + f2.format(today));
}
public String myTan (String s)
{
double d=Double.parseDouble(s);
if(d==90)
throw new DivideByZeroException("非法输入");
return formatResult(Math.tan(d*PI/180));
}
public String mySqrt(String s)
{
double d=Double.parseDouble(s);
if(d<0)
throw new DivideByZeroException("非法输入");
return formatResult(Math.sqrt(d));
}
public static void main(String []args)
{
Calcular c=new Calcular();
c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -