📄 jisuanqi.java
字号:
// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) ansi
// Source File Name: MainFrame.java
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class JiSuanQi extends JFrame implements ActionListener
{
int able;
//JFrame frame;
JMenuBar bar;
JPanel panel;
JPanel panel1;
JPanel panel2;
JPanel panel3;
JTextField textAnswer;
JButton lKH;
JButton rKH;
JButton BackSpace;
JButton clear;
JButton bAdd;
JButton bSub;
JButton bMul;
JButton bDiv;
JButton benter;
JButton b[];
JMenu mFile;
JMenu mHelp;
JMenuItem iopen;
JMenuItem iexit;
JMenuItem iabout;
JMenuItem ihelp;
public JiSuanQi()
{
super("计算器");
able = 0;
setBounds(100, 100, 300, 300);
panel = new JPanel();
panel1 = new JPanel();
GridLayout gridlayout = new GridLayout(5, 4, 5, 5);
panel.setLayout(new BorderLayout());
panel1.setLayout(gridlayout);
textAnswer = new JTextField(15);
textAnswer.setText("");
textAnswer.setEditable(false);
textAnswer.setBackground(new Color(255, 255, 255));
textAnswer.setBounds(0, 0, 200, 20);
lKH = new JButton(" (");
rKH = new JButton(" )");
BackSpace = new JButton("←");
clear = new JButton("C");
bAdd = new JButton("+");
bSub = new JButton("_");
bMul = new JButton("*");
bDiv = new JButton("/");
benter = new JButton("=");
b = new JButton[10];
for(int i = 0; i < b.length; i++)
b[i] = new JButton(Integer.toString(i));
panel1.add(lKH);
panel1.add(rKH);
panel1.add(BackSpace);
panel1.add(clear);
panel1.add(b[7]);
panel1.add(b[8]);
panel1.add(b[9]);
panel1.add(bAdd);
panel1.add(b[4]);
panel1.add(b[5]);
panel1.add(b[6]);
panel1.add(bSub);
panel1.add(b[1]);
panel1.add(b[2]);
panel1.add(b[3]);
panel1.add(bMul);
panel1.add(b[0]);
panel1.add(benter);
panel1.add(bDiv);
panel.add(textAnswer, "North");
panel.add(panel1, "Center");
getContentPane().add(panel);
bar = new JMenuBar();
setJMenuBar(bar);
mFile = new JMenu("文件");
iopen = new JMenuItem("批处理。。。");
iexit = new JMenuItem("退出");
mFile.add(iopen);
mFile.add(iexit);
bar.add(mFile);
mHelp = new JMenu("帮助");
iabout = new JMenuItem("关于");
ihelp = new JMenuItem("帮助主题");
mHelp.add(iabout);
mHelp.add(ihelp);
bar.add(mHelp);
lKH.addActionListener(this);
rKH.addActionListener(this);
BackSpace.addActionListener(this);
clear.addActionListener(this);
bAdd.addActionListener(this);
bSub.addActionListener(this);
bMul.addActionListener(this);
bDiv.addActionListener(this);
benter.addActionListener(this);
b[1].addActionListener(this);
b[2].addActionListener(this);
b[3].addActionListener(this);
b[4].addActionListener(this);
b[5].addActionListener(this);
b[6].addActionListener(this);
b[7].addActionListener(this);
b[8].addActionListener(this);
b[9].addActionListener(this);
b[0].addActionListener(this);
iopen.addActionListener(this);
ihelp.addActionListener(this);
iabout.addActionListener(this);
setVisible(true);
}
//取得输入框中的最后一个字符
public char getLastChar()
{
String s = textAnswer.getText();
int i = s.length() - 1;
if(i == -1)
{
return '@';
} else
{
char c = s.charAt(i);
return c;
}
}
//判断输入框中有没有运算符,如果有返回true,否则返回false
public boolean getright()
{
String s = textAnswer.getText();
for(int i = 0; i < s.length(); i++)
if((s.charAt(i) == '+' || s.charAt(i) == '-' || s.charAt(i) == '*' || s.charAt(i) == '/') && getLastChar() != '+' && getLastChar() != '-' && getLastChar() != '*' && getLastChar() != '/')
return true;
return false;
}
public void actionPerformed(ActionEvent actionevent)
{
Object obj = actionevent.getSource();
//如果输入的是数字,将新输入的数字和已经存在的数字拼接成一个数
for(int i = 0; i <= 9; i++)
if(obj == b[i] && getLastChar() != ')')
textAnswer.setText(textAnswer.getText() + Integer.toString(i));
if(obj == iopen)
{
JOptionPane.showMessageDialog(panel, "输入文件为in.txt");
try
{
new File9();
}
catch(Exception exception) { }
JOptionPane.showMessageDialog(panel, "已经生成:输入文件in.txt,输出文件out.txt");
}
if(obj == iabout)
JOptionPane.showMessageDialog(panel, "小组作业:七班第5小组完成");
if(obj == ihelp)
JOptionPane.showMessageDialog(panel, "小组作业:七班第5小组完成");
//如果文本框中的最后字符不是运算符,并且当前点击了运算符,将点击的运算符拼接上
if(getLastChar() != '+' && getLastChar() != '-' && getLastChar() != '*' && getLastChar() != '/' && getLastChar() != '@')
{
if(obj == bAdd)
textAnswer.setText(textAnswer.getText() + "+");
if(obj == bSub)
textAnswer.setText(textAnswer.getText() + "-");
if(obj == bMul)
textAnswer.setText(textAnswer.getText() + "*");
if(obj == bDiv)
textAnswer.setText(textAnswer.getText() + "/");
if(obj == rKH && able > 0 && getLastChar() != '(')
{
textAnswer.setText(textAnswer.getText() + ")");
able--;
}
}
if(obj == lKH && (getLastChar() == '+' || getLastChar() == '-' || getLastChar() == '*' || getLastChar() == '/' || getLastChar() == '@'))
{
textAnswer.setText(textAnswer.getText() + "(");
able++;
}
if(obj == clear)
textAnswer.setText("");
if(obj == BackSpace)
{
String s = textAnswer.getText();
textAnswer.setText("");
for(int j = 0; j < s.length() - 1; j++)
{
char c = s.charAt(j);
textAnswer.setText(textAnswer.getText() + c);
}
}
//点击等号后进行计算
if(obj == benter && getLastChar() != '@' && able == 0 && getright())
{
textAnswer.setText(textAnswer.getText() + "=");
Arithmetic arithmetic = new Arithmetic(textAnswer.getText());
textAnswer.setText(arithmetic.end);
}
}
public static void main(String args[])
{
new JiSuanQi();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -