📄 calc.java
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
public class Calc extends JFrame implements ActionListener
{
/**
*
*/
private static final long serialVersionUID = 1L;
private Vector <Double> numbers = null;
private Vector <Integer> actions = null;
private Container c;
private int dottedHez = 1;
private JButton plus;
private JButton minus;
private JButton mult;
private JButton div;
private JButton point;
private JButton sign;
private JButton calc;
private Double sum =0.0;
int [] flags = new int [9];
/*
* this are the array indexes:
* plus
* minus
* mult
* div
* sign
* dot
* calc
* number
*/
private Double temp = 0.0;
JButton[] numArray = new JButton [10];
private JTextArea output = new JTextArea(1,100);
public Calc()
{
super("calculator");
setSize (300,200);
zeroes();
numbers = new Vector<Double>(1);
actions = new Vector<Integer>(1);
c= getContentPane();
c.setLayout(new FlowLayout(FlowLayout.LEFT, 20, 20));
createButtons();
c.add(output,0);
setVisible(true);
}
public static void main(String[] args)
{
Calc myCalc = new Calc();
}
@Override
public void actionPerformed(ActionEvent e )
{
// TODO Auto-generated method stub
/*
* this are the array indexes:
* plus -0
* minus -1
* mult -2
* div -3
* sign -4
* dot -5
* calc -6
* number -7
*/
if (flags[7]==0)
output.setText("");
flags[7] =0;
Integer i =0;
for (i=0; i<10; i++)
{
if (e.getSource() == numArray[i])
{
output.append (i.toString());
if (flags[5] == 1)//it is with dot
{
dottedHez *=10;
if (temp>=0)
temp+=(i.doubleValue()/dottedHez);
else
temp-=(i.doubleValue()/dottedHez);
}
else
{
temp *= 10;
if (temp>= 0)
{
temp += i;
}
else
temp -= i;
}
flags[7] =1 ;
}
}
if ( e.getSource() == sign)
{
temp = 0 - temp;
output.setText(temp+"");
flags[7] = 1;
}
if ( e.getSource() == point)
{
output.append(".");
flags[5] =1;
flags[7] = 1;
}
if ( e.getSource() != point && flags [7]== 0)
{
dottedHez=1;
flags[5] = 0;
}
if (flags[7]==0 && flags [5] == 0)
{
numbers.addElement(temp);
temp =0.0;
calculate();
output.setText(sum+"");
}
if ( e.getSource() == plus)
{
flags[0] =1;
actions.add(1);
}
if ( e.getSource() == minus)
{
flags[1] =1;
actions.add(2);
}
if ( e.getSource() == div)
{
flags[3] = 1;
actions.add(4);
}
if ( e.getSource() == mult)
{
flags[2] =1;
actions.add(3);
}
if ( e.getSource() == calc)
{
calculate();
output.setText(sum+"");
}
}
private void createButtons()
{
Integer i =1;
for (i=0 ; i<10; i++)
numArray[i] = new JButton (i.toString());
for (i=0 ; i<10; i++)
c.add(numArray[i]);
for (i=0; i<10; i++)
{
numArray[i].addActionListener(this);
}
plus = new JButton ("+");
minus= new JButton ("-");
mult= new JButton ("*");;
div = new JButton ("/");
point= new JButton (".");
sign= new JButton ("+/-");
calc= new JButton ("=");
plus.addActionListener(this);
minus.addActionListener(this);
mult.addActionListener(this);
div.addActionListener(this);
point.addActionListener(this);
sign.addActionListener(this);
calc.addActionListener(this);
c.add(plus);
c.add(minus);
c.add(div);
c.add (mult);
c.add(point);
c.add (sign);
c.add (calc);
}
private void zeroes ()
{
for (int i =0 ; i <flags.length; i++)
{
flags[i] = 0;
}
}
private void calculate ()
{
int i=0;
int j=0;
sum = numbers.elementAt(i);
output.setText(numbers.size()+"");
for (i = 0; i< numbers.size()-1; i++)
{
switch (actions.elementAt(j))
{
case 1:
{
sum+=numbers.elementAt(i+1);
break;
}
case 2:
{
sum-=numbers.elementAt(i+1);
break;
}
case 3:
{
sum*=numbers.elementAt(i+1);
break;
}
case 4:
{
sum/=numbers.elementAt(i+1);
break;
}
}
j++;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -