📄 ca.java
字号:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
public class ca {
public static void main(String[] s){
new go() ;
}
}
class go extends JFrame {
public JFrame fm;
public JPanel p,p1,p2,p3,p4,p5;//p是总体嵌套面板,p1为按钮面板嵌套面板,p2,p3,p4,p5的功能见下
public JTextField textShow;//显示栏
public JMenuBar menu;//菜单栏
public JMenu editMenu;//编辑
public JMenu viewMenu;//查看
public JMenu helpMenu;//帮助
public JTextArea help;//跳出的帮助对话框
public JScrollPane helpwindow;//创建一个空的(无视口的视图)JScrollPane,需要时水平和垂直滚动条都可显示
public JMenuItem copyItem;//复制
public JMenuItem pasteItem;//粘贴
public JMenuItem tItem;//标准型
public JMenuItem sItem;//科学型
public JMenuItem helptopic;//帮助主题
public JMenuItem copyright;//关于计算器
public JButton b1,b2,b3,b4,b5,b6,b7;
public JButton b[],bn1[];//b[]存放数字键及"+/-",".",bn1存放数字符号键
public String signal1[]={"9","8","7","6","5","4","3","2","1","0","+/-","."};//利用这两个字符数组实现对按钮命名
public String signal2[]={"*","/","-","+","sqrt","%","1/x","="};
public double memory=0.0,result=0.0;//memory记录运算中间值,连续运算时用到,result记录结果
public int clickflag=1;//一旦出现非法操作,clickflag=0,则继续按键无意义
public int signflag;//数字前的正负号标志
public int dotflag=0;//小数点是否按下标志
public String copy;
public int oper=-1,pre=-1;//pre用来记下前一运算符,oper用来记下当前操作符
public int operflage=0;//运算符标志,再次进行其他运算时,协助清屏
public JLabel lb1 = new JLabel("已使用时间:");//计时器提示
public JLabel lb2= new JLabel();//计时器显示时间
public int time=0;
public Thread t;
public go()
{
fm=new JFrame("Java计算器");
textShow=new JTextField(22);//显示框
textShow.setText("");
p=new JPanel();
fm.getContentPane().add(p);
p.setLayout(new BorderLayout());
menu=new JMenuBar();//菜单栏:编辑,查看,帮助均添加于此
editMenu=new JMenu("编辑");
editMenu.addActionListener(new Handler());
viewMenu=new JMenu("查看");
viewMenu.addActionListener(new Handler());
helpMenu=new JMenu("帮助");
helpMenu.addActionListener(new Handler());
copyItem=new JMenuItem("复制 Ctrl+C");
copyItem.addActionListener(new Handler());
pasteItem= new JMenuItem("粘贴 Ctrl+V");
pasteItem.addActionListener(new Handler());
editMenu.add(copyItem);
editMenu.add(pasteItem);
tItem=new JMenuItem("*标准型(T)");
sItem=new JMenuItem("科学型(S)");
sItem.addActionListener(new Handler());
viewMenu.add(tItem);
viewMenu.add(sItem);
helptopic=new JMenuItem("帮助主题");
helptopic.addActionListener(new Handler());
copyright=new JMenuItem("关于计算器");
copyright.addActionListener(new Handler());
helpMenu.add(helptopic);
helpMenu.addSeparator();
helpMenu.add(copyright);
help=new JTextArea(5,20);//跳出的帮助窗口
helpwindow=new JScrollPane(help);
help.append("执行步骤说明:\n");//追加指定字符,即在帮助窗口中的文字
help.append("*先输入运算数1:\n");
help.append("*输入运算符:\n");
help.append("*再输入运算数2\n");
help.append("*单击 ' = ' 输出结果\n");
help.append("注:也可进行连续运算.\n");
menu.add(editMenu);
menu.add(viewMenu);
menu.add(helpMenu);
p.add(menu,BorderLayout.NORTH);//将菜单栏添加到北面即面板上面
p.add(textShow,BorderLayout.CENTER);//显示栏添加在中部
p1=new JPanel();//p1为总按钮面板
p1.setLayout(new BorderLayout(30,30));
p.add(p1,BorderLayout.SOUTH);//将p1这个总按钮面板添加至p的南面
b1=new JButton("Backspace");
b1.addActionListener(new Handler());
b2=new JButton("CE");
b2.addActionListener(new Handler());
b3=new JButton("C");
b3.addActionListener(new Handler());
p2=new JPanel();//p2为放置"Backspace","CE","C"以及计时器的面板,嵌套在p1中,居北
p2.setLayout(new FlowLayout(FlowLayout.CENTER));
lb1.setForeground(Color.blue);//设置显示计时字符的颜色
lb2.setForeground(Color.blue);
p2.add(lb1);
p2.add(lb2);
lb2.setText("");
t = new Thread(new Handler());
t.start();
p2.add(b1);
p2.add(b2);
p2.add(b3);
p1.add(p2,BorderLayout.NORTH);
b=new JButton[12];
p3=new JPanel();//p3为放置数字键以及"+/-","."的面板,也嵌套在p1中,居中
p3.setLayout(new GridLayout(4,3));
for(int i=0;i<12;i++)//为按钮赋名称并设置监听
{
b[i]=new JButton(signal1[i]);
b[i].addActionListener(new Handler());
p3.add(b[i]);
}
p1.add(p3,BorderLayout.CENTER);
p4=new JPanel();//p4为放置"MC","MR","MS","M+"的面板,也嵌套在p1中,居西
p4.setLayout(new GridLayout(4,1));
b4=new JButton("MC");
b4.addActionListener(new Handler());
p4.add(b4);
b5=new JButton("MR");
b5.addActionListener(new Handler());
p4.add(b5);
b6=new JButton("MS");
b6.addActionListener(new Handler());
p4.add(b6);
b7=new JButton("M+");
b7.addActionListener(new Handler());
p4.add(b7);
p1.add(p4,BorderLayout.WEST);
p5=new JPanel();//p5为放置运算符的面板,也嵌套在p1中,居东
p5.setLayout(new GridLayout(4,2));
bn1=new JButton[8];
for(int i=0;i<8;i++)
{
bn1[i]=new JButton(signal2[i]);
bn1[i].addActionListener(new Handler());
p5.add(bn1[i]);
}
p1.add(p5,BorderLayout.EAST);
fm.setSize(390,265);
fm.setVisible(true);
}
class Handler implements ActionListener,Runnable//监听类
{
public void run()
{
while(true)
{
try{
lb2.setText(String.valueOf(time++));
Thread.sleep(1000);
}catch(Exception e){}
}
}
public void actionPerformed(ActionEvent e){
try{
if(e.getSource()==b1)//按下'Backspace'键
{
String s=textShow.getText();
textShow.setText("");
for(int i=0;i<s.length()-1;i++)
{
char a=s.charAt(i);
textShow.setText(textShow.getText()+a);
}
}
if(e.getSource()==b2)//按下'CE'键
{
textShow.setText("");
clickflag=1;
operflage=0;
}
if(e.getSource()==b3)//按下'C'键
{
memory=0;
result=0;
textShow.setText("");
clickflag=1;
operflage=0;
}
if(e.getSource()==copyItem)//复制
{
copy=textShow.getText();
}
if(e.getSource()==pasteItem)//粘贴
{
textShow.setText(textShow.getText()+copy);
}
if(e.getSource()==sItem)//科学型
{
JOptionPane.showMessageDialog (fm, "对不起,此项功能更新中...");
}
if(e.getSource()==helptopic)//帮助主题
{
JOptionPane.showMessageDialog(p,helpwindow);
}
if(e.getSource()==copyright)//关于计算器
{
JOptionPane.showMessageDialog(p,"开发者:刘倩(计应0504)");
}
for(int i=0;i<=9;i++)
{
if(e.getSource()==b[i]&&clickflag==1)//按下数字键
{
if(operflage==0)
textShow.setText(textShow.getText()+Integer.toString(9-i));
else{
textShow.setText("");
textShow.setText(textShow.getText()+Integer.toString(9-i));
operflage=0;
}
}
}
if(e.getSource()==b[10]&&clickflag==1)//按下'-/+'键
{
signflag=1;
String s=textShow.getText();
if(signflag==1)
{
if(s.charAt(0)=='-')
{
textShow.setText("");
for(int i=1;i<s.length();i++)
{
char a=s.charAt(i);
textShow.setText(textShow.getText()+a);
}
}
else
textShow.setText('-'+s);
}
}
if(e.getSource()==b[11]&&clickflag==1)//按下小数点
{
if(textShow.getText().length()==0)
dotflag=1;
for (int i = 0; i < textShow.getText().length(); i++)
if ('.' == textShow.getText().charAt(i)) {//检查屏幕字符中是否已有小数点
dotflag= 1;
break;
}
if (dotflag == 0)
textShow.setText(textShow.getText() + ".");
}
if(e.getSource()==bn1[4]&&clickflag==1)//开方运算
{
operflage=1;
String s=textShow.getText();
if(s.charAt(0)=='-')
{
JOptionPane.showMessageDialog (fm, "负数不能开根号...");
clickflag=0;
}
else
textShow.setText(Double.toString(java.lang.Math.sqrt(Double.parseDouble(textShow.getText()))));
}
if(e.getSource()==bn1[6]&&clickflag==1)//倒数运算
{
operflage=1;
String s=textShow.getText();
if(s.charAt(0)=='0'&&textShow.getText().length() == 1)
{
JOptionPane.showMessageDialog (fm, "0不能求倒数...");
clickflag=0;
}
else
{
s = Double.toString(1 / Double.parseDouble(textShow.getText()));
textShow.setText(s);
}
}
if(e.getSource()==bn1[5]&&clickflag==1){//两数相除后,按此键,进行取余操作
operflage=1;
String s=textShow.getText();
if(s.charAt(0)=='0')
textShow.setText("0");
else
{
memory = Double.parseDouble(textShow.getText());
pre = -1;
result %= Double.parseDouble(textShow.getText());
textShow.setText(Double.toString(result));
}
}
if ( (e.getSource() == bn1[3] || e.getSource() == bn1[2] || e.getSource() == bn1[0] ||
e.getSource() == bn1[1]) && clickflag==1) //加减乘除的运算
{
if (e.getSource() == bn1[3]) {//加操作
switch (pre) {
case 0:
result += Double.parseDouble(textShow.getText());
break;
case 1:
result -= Double.parseDouble(textShow.getText());
break;
case 2:
result *= Double.parseDouble(textShow.getText());
break;
case 3:
if (Double.parseDouble(textShow.getText()) == 0) {
textShow.setText("除数不能为零");
clickflag = 0;
}
else
result /= Double.parseDouble(textShow.getText());
break;
default:
result = Double.parseDouble(textShow.getText());
}
textShow.setText("");
pre =0;
oper = 0;
}
if (e.getSource() == bn1[2]) {//减操作
switch (pre) {
case 0:
result += Double.parseDouble(textShow.getText());
break;
case 1:
result -= Double.parseDouble(textShow.getText());
break;
case 2:
result *= Double.parseDouble(textShow.getText());
break;
case 3:
if (Double.parseDouble(textShow.getText()) == 0) {
textShow.setText("除数不能为零");
clickflag= 0;
}
else
result /= Double.parseDouble(textShow.getText());
break;
default:
result = Double.parseDouble(textShow.getText());
}
textShow.setText("");
pre = 1;
oper = 1;
}
if (e.getSource() == bn1[0]) {//乘操作
switch (pre) {
case 0:
result += Double.parseDouble(textShow.getText());
break;
case 1:
result -= Double.parseDouble(textShow.getText());
break;
case 2:
result *= Double.parseDouble(textShow.getText());
break;
case 3:
if (Double.parseDouble(textShow.getText()) == 0) {
textShow.setText("除数不能为零");
clickflag= 0;
}
else
result /= Double.parseDouble(textShow.getText());
break;
default:
result = Double.parseDouble(textShow.getText());
}
textShow.setText("");
pre = 2;
oper = 2;
}
if (e.getSource() == bn1[1]) {//除操作
switch (pre) {
case 0:
result += Double.parseDouble(textShow.getText());
break;
case 1:
result -= Double.parseDouble(textShow.getText());
break;
case 2:
result *= Double.parseDouble(textShow.getText());
break;
case 3:
if (Double.parseDouble(textShow.getText()) == 0) {
textShow.setText("除数不能为零");
clickflag= 0;
}
else
result /= Double.parseDouble(textShow.getText());
break;
default:
result = Double.parseDouble(textShow.getText());
}
textShow.setText("");
pre = 3;
oper= 3;
}
}
if (e.getSource() == bn1[7] && clickflag==1) {//按下“=”
operflage=1;//按下等号的记录标识符,有助于清屏
if (pre == 1000) {//连按等号情况
if (oper == 0) {
result += memory;
textShow.setText(Double.toString(result));
}
if (oper == 1) {
result -= memory;
textShow.setText(Double.toString(result));
}
if (oper == 2) {
result *= memory;
textShow.setText(Double.toString(result));
}
if (oper == 3) {
if (Double.parseDouble(textShow.getText()) == 0) {
textShow.setText("除数不能为零");
clickflag= 0;
}
else {
result /= memory;
textShow.setText(Double.toString(result));
}
}
}
else {
memory = Double.parseDouble(textShow.getText());
if (oper == 0) {
pre = -1;
result += Double.parseDouble(textShow.getText());
textShow.setText(Double.toString(result));
}
if (oper == 1) {
pre = -1;
result -= Double.parseDouble(textShow.getText());
textShow.setText(Double.toString(result));
}
if (oper == 2) {
pre = -1;
result *= Double.parseDouble(textShow.getText());
textShow.setText(Double.toString(result));
}
if (oper == 3) {
pre = -1;
if (Double.parseDouble(textShow.getText()) == 0) {
textShow.setText("除数不能为零");
clickflag= 0;
}
else {
result /= Double.parseDouble(textShow.getText());
textShow.setText(Double.toString(result));
}
}
}
pre =1000;
}
if ( (e.getSource() == b4 || e.getSource() == b5 || e.getSource() == b6 ||
e.getSource() == b7) && clickflag==1) {
JOptionPane.showMessageDialog (fm, "对不起,此项功能更新中...");
}
}
catch(Exception e1){
JOptionPane.showMessageDialog (fm, "操作非法!");
clickflag=0;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -