写的不太好
package Reckoner;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
public class Caculate {
String[]buttons={"1","2","3","+","←","4","5","6","-","C","7","8","9","*","01",".","0","=","/","02"};
public Caculate(){
}
JPanel buttonPanel1=new JPanel();
JPanel panelResult=new JPanel();
JOptionPane jop = new JOptionPane("出错了");
JLabel expression=new JLabel();
JLabel result=new JLabel();
JPanel panel=new JPanel(new GridLayout(2,1));
String eps = "";
String res = "";
int judge=0;
JMenuBar menuBar=new JMenuBar();
JMenu menu=new JMenu("菜单");
JMenuItem menuItem1=new JMenuItem("历史记录");
JMenuItem menuItem2=new JMenuItem("清空文件");
JMenuItem menuItem3=new JMenuItem("退出");
File file=new File("记录.txt");
JDialog historyDialog=new JDialog();
JTextArea historyTextArea=new JTextArea();
FileOutputStream fileOutputStream;
public void Show()
{
JFrame window=new JFrame("AIIT计算器");
window.setSize(500,400);
window.setLocationRelativeTo(null);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buttonPanel1.setLayout(new GridLayout(4,5));
window.setLayout(new GridLayout(2,1));
try {
file.createNewFile();
} catch (IOException e) {
throw new RuntimeException(e);
}
initButton();
initAll();
initmenuBar();
window.setJMenuBar(menuBar);
window.add(panel);
window.add(buttonPanel1);
window.setVisible(true);
}
public void initmenuBar()
{
menuItem1.addActionListener(new MenuListener());
menu.add(menuItem1);
menuItem2.addActionListener(new MenuListener());
menu.add(menuItem2);
menuItem3.addActionListener(new MenuListener());
menu.add(menuItem3);
menuBar.add(menu);
}
public void initButton()
{
for(String index:buttons)
{
JButton button=new JButton(index);
button.addActionListener(new ButtonListener());
buttonPanel1.add(button);
}
}
public void initAll()
{
panel.add(expression);
panel.add(result);
}
public void compute()
{
double num1=0,num2 = 0;
num1=Double.parseDouble(eps.split(" ")[0]);
num2=Double.parseDouble(eps.split(" ")[2]);
if(eps.split(" ")[1].equals("+"))
{
res=String.valueOf(num1+num2);
}
else if(eps.split(" ")[1].equals("-"))
{
res=String.valueOf(num1-num2);
}
else if(eps.split(" ")[1].equals("*"))
{
res=String.valueOf(num1*num2);
}
else if(eps.split(" ")[1].equals("/"))
{
res=String.valueOf(num1/num2);
}
}
public class MenuListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
String x=e.getActionCommand();
switch (x)
{
case "历史记录":
try {
FileInputStream fis = new FileInputStream("记录.txt");
int length = (int) file.length();
byte[] b = new byte[length];
fis.read(b);
String xx = new String(b);
historyTextArea.setText(xx);
historyDialog.add(historyTextArea);
historyDialog.setSize(300, 400);
historyDialog.setLocationRelativeTo(null);
historyDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
historyTextArea.setEditable(false);
historyDialog.setVisible(true);
fis.close();
}
catch (FileNotFoundException ex) {
throw new RuntimeException(ex);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
break;
case "清空文件":
try {
fileOutputStream=new FileOutputStream("记录.txt");
eps="";
byte[] b =eps.getBytes() ;
fileOutputStream.write(b);
} catch (FileNotFoundException ex) {
throw new RuntimeException(ex);
}
catch (IOException ex) {
throw new RuntimeException(ex);
}
break;
}
}
}
public class ButtonListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e) {
String x=e.getActionCommand();
String i=x;
switch(x)
{
case "1":
case "2":
case "3":
case "4":
case "5":
case "6":
case "7":
case "8":
case "9":
case "0":
if(!res.equals(""))
{
eps="";
res="";
}
if(x.equals(i+""))
{
eps+=i;
expression.setText(eps);
break;
}
case "+":
case "-":
case "*":
case "/":
char lastChar = eps.charAt(eps.length() - 1);
if(lastChar==' ')
{
jop.showMessageDialog(null, "出错了", "错误", JOptionPane.ERROR_MESSAGE);
eps=" ";
expression.setText(eps);
}
else{
i = " " + x + " ";
eps += i;
expression.setText(eps);
}
break;
case "←":
if (eps.length() > 0) {
eps = eps.substring(0, eps.length() - 1);
expression.setText(eps);
}
break;
case "C":
expression.setText("");
break;
case "=":
compute();
try {
fileOutputStream=new FileOutputStream("记录.txt",true);
String epss=eps+"="+res+"\n";
byte[] b =epss.getBytes();
fileOutputStream.write(b);
} catch (FileNotFoundException ex) {
throw new RuntimeException(ex);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
eps="";
result.setText(res);
break;
}
}
}
public static void main(String[] args)
{
new Caculate().Show();
}
}