import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.DecimalFormat;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class Calculator06 extends WindowAdapter implements ActionListener
{
JFrame f;
JMenuBar m = new JMenuBar(); // 菜单条
JMenu check = new JMenu("模式"); // 菜单1
JMenu help = new JMenu("帮助"); // 菜单2
JMenuItem check1 = new JMenuItem("&标准型"); // 菜单1的菜单项
JMenuItem check2 = new JMenuItem("科学型"); // 菜单1的菜单项
JMenuItem help1 = new JMenuItem("关于作者"); // 菜单2的菜单项
JMenuItem help2 = new JMenuItem("关于计算器"); // 菜单2的菜单项
JTextField text;
JRadioButton two,eight,ten,sixteen;
JButton[] b=new JButton[16];
JButton bPlus,bMinus,bMulti,bDiv,bX2,bX3,bDot,bNegative,bEqual;
JButton bSin,bCos,bLog,bLn,bFactorial,bSqrt,bReciprocal;
JButton bBackspace,bClean;
JPanel p1,p2,p3,p4,p5,p6;
double dResult=0;
double dNowInput=0;
double now=10; //当前进制数
double Num; //记录数字按键对应数值
int i,n=0; //记录小数位数
int[] aa=new int[100]; //记录小数对应数的值
int[] bb=new int[100];
int nOperation=1; // 记录运算符类型
boolean alreadyHaveDot=false; //是否有小数点
boolean alreadyClickedEqueal=false; //是否按下等于号
boolean isTempNowInput=false; //是否在计算出结果后直
boolean ntwo=false,neight=false,nten=true,nsixteen=false;//记录当前为何种进制
String snum; //数字对应的字符
DecimalFormat geshi=new DecimalFormat("###,###.#########");//格式化double型
public void calculator()
{
f=new JFrame("计算器");
f.setSize(420,320);
f.setLocation(200,200);
f.setBackground(Color.LIGHT_GRAY);
f.setResizable(false);
f.setLayout(new FlowLayout());
check.add(check1);
check.add(check2);
help.add(help1);
help.add(help2);
m.add(check);
m.add(help);
f.setJMenuBar(m);
check1.addActionListener(this);
check2.addActionListener(this);
help1.addActionListener(this);
help2.addActionListener(this);
p1=new JPanel(new FlowLayout()); //放置文本区
p2=new JPanel(new GridLayout(2,3,45,0)); //放置二,十,八,十六进制及Backspace,Clean
p3=new JPanel(new GridLayout(1,7,3,1)); //存放函数sin,cos,log,ln,n!,√,1/x
p4=new JPanel(new BorderLayout(5,5)); //放置p5,p6
p5=new JPanel(new GridLayout(4,6,5,5)); //放数字键及"±",".","x^2","x^3"
p6=new JPanel(new GridLayout(1,1,5,5)); //放置等于号
text=new JTextField(35); //存放显示区
text.setText("0");
text.setHorizontalAlignment(JTextField.RIGHT);
text.setEditable(false);
p1.add(text);
two=new JRadioButton("二进制");
eight=new JRadioButton("八进制");
ten=new JRadioButton("十进制",true);
sixteen=new JRadioButton("十六进制");
two.addActionListener(this);
eight.addActionListener(this);
ten.addActionListener(this);
sixteen.addActionListener(this);
ButtonGroup btg=new ButtonGroup();
btg.add(two);
btg.add(eight);
btg.add(ten);
btg.add(sixteen);
bBackspace=new JButton("Backspace");
bBackspace.setForeground(new Color(255,0,0));
bClean=new JButton("Clean");
bClean.setForeground(new Color(255,0,0));
bBackspace.addActionListener(this);
bClean.addActionListener(this);
p2.add(two);
p2.add(eight);
p2.add(sixteen);
p2.add(ten);
p2.add(bBackspace);
p2.add(bClean);
bSin=new JButton("sin");
bSin.setForeground(new Color(255,0,0));
bCos=new JButton("cos");
bCos.setForeground(new Color(255,0,0));
bLog=new JButton("log");
bLog.setForeground(new Color(255,0,0));
bLn=new JButton("ln");
bLn.setForeground(new Color(255,0,0));
bFactorial=new JButton("n!");
bFactorial.setForeground(new Color(255,0,0));
bSqrt=new JButton("√");
bSqrt.setForeground(new Color(255,0,0));
bReciprocal=new JButton("1/x");
bReciprocal.setForeground(new Color(255,0,0));
bSin.addActionListener(this);
bCos.addActionListener(this);
bLog.addActionListener(this);
bLn.addActionListener(this);
bFactorial.addActionListener(this);
bSqrt.addActionListener(this);
bReciprocal.addActionListener(this);
p3.add(bSin);
p3.add(bCos);
p3.add(bLog);
p3.add(bLn);
p3.add(bFactorial);
p3.add(bSqrt);
p3.add(bReciprocal);
for (i=0;i<16;i++){
b[i]=new JButton(Integer.toHexString(i));
b[i].setForeground(new Color(0,0,255));
b[i].addActionListener(this);
}
bNegative=new JButton("±");
bNegative.setForeground(new Color(0,0,255));
bDot=new JButton(".");
bDot.setForeground(new Color(0,0,255));
bX2=new JButton("x^2");
bX2.setForeground(new Color(0,0,255));
bX3=new JButton("x^3");
bX3.setForeground(new Color(0,0,255));
bPlus=new JButton("+");
bPlus.setForeground(new Color(255,0,0));
bMinus=new JButton("-");
bMinus.setForeground(new Color(255,0,0));
bMulti=new JButton("*");
bMulti.setForeground(new Color(255,0,0));
bDiv=new JButton("/");
bDiv.setForeground(new Color(255,0,0));
bEqual=new JButton("=");
bEqual.setForeground(new Color(255,0,0));
bPlus.addActionListener(this);
bMinus.addActionListener(this);
bMulti.addActionListener(this);
bDiv.addActionListener(this);
bDot.addActionListener(this);
bNegative.addActionListener(this);
bEqual.addActionListener(this);
bX2.addActionListener(this);
bX3.addActionListener(this);
p5.add(b[7]);p5.add(b[8]);p5.add(b[9]);p5.add(b[10]);p5.add(b[11]);p5.add(bPlus);
p5.add(b[4]);p5.add(b[5]);p5.add(b[6]);p5.add(b[12]);p5.add(b[13]);p5.add(bMinus);
p5.add(b[1]);p5.add(b[2]);p5.add(b[3]);p5.add(b[14]);p5.add(b[15]);p5.add(bMulti);
p5.add(b[0]);p5.add(bNegative);p5.add(bDot);p5.add(bX2);p5.add(bX3);p5.add(bDiv);
p6.add(bEqual);
p4.add(p5,BorderLayout.CENTER);
p4.add(p6,BorderLayout.EAST);
bSin.setVisible(false);
bCos.setVisible(false);
bLog.setVisible(false);
bLn.setVisible(false);
bFactorial.setVisible(false);
bSqrt.setVisible(false);
bReciprocal.setVisible(false);
for (i=10;i<16;i++)
b[i].setEnabled(false);
f.add(p1);
f.add(p2);
f.add(p3);
f.add(p4);
f.setVisible(true);
f.addWindowListener(this);
}
public void actionPerformed(ActionEvent e)
{
Object temp = e.getSource();
if (temp==help1){
JOptionPane.showMessageDialog(
f,"班级:***\n" + "姓名:***\n" +"学号:***","作者",-1);
}
if (temp==help2){
JOptionPane.showMessageDialog(
f,"Calculator06","计算器",1);
}
//标准型的界面
if (temp==check1){
ten.setSelected(true);
check1.setText("&标准型");
check2.setText("科学型");
bSin.setVisible(false);
bCos.setVisible(false);
bLog.setVisible(false);
bLn.setVisible(false);
bFactorial.setVisible(false);
bSqrt.setVisible(false);
bReciprocal.setVisible(false);
for (i=10;i<16;i++)
b[i].setEnabled(false);
two.setVisible
- 1
- 2
前往页