Cadev.java
import java.awt.*
import java.awt.event.*
import javax.swing.*
import java.util.LinkedList
import java.text.NumberFormat
import javax.swing.border.*
public class Cadev extends JFrame implements ActionListener
{
Nbtn nbtn[]
Obtn obtn[]
Button dotbtn,PlusOrMinusbtn,Backspacebtn,
recbtn,equbtn,clbtn
JPanel panel
JTextField resultShow
String Oper[]={"+","-","*","/"}
LinkedList link
boolean sfcequ=false
public Cadev()
{
super("计算器")
link=new LinkedList()
nbtn=new Nbtn[10]
for(int i=0i<=9i++)
{
nbtn[i]=new Nbtn(i)
nbtn[i].addActionListener(this)
}
obtn=new Obtn[4]
for(int i=0i<4i++)
{
obtn[i]=new Obtn(Oper[i])
obtn[i].addActionListener(this)
}
dotbtn=new Button(".")
PlusOrMinusbtn=new Button("+/-")
equbtn=new Button("=")
recbtn=new Button("1/x")
Backspacebtn=new Button("退格")
clbtn=new Button("C")
recbtn.setForeground(Color.blue)
dotbtn.setForeground(Color.blue)
PlusOrMinusbtn.setForeground(Color.blue)
equbtn.setForeground(Color.red)
Backspacebtn.setForeground(Color.red)
clbtn.setForeground(Color.red)
dotbtn.addActionListener(this)
PlusOrMinusbtn.addActionListener(this)
equbtn.addActionListener(this)
recbtn.addActionListener(this)
Backspacebtn.addActionListener(this)
clbtn.addActionListener(this)
resultShow=new JTextField(10)
resultShow.setHorizontalAlignment(JTextField.RIGHT)
resultShow.setFont(new Font("TimesRoman",Font.PLAIN,14))
resultShow.setBorder(new SoftBevelBorder(BevelBorder.LOWERED))
resultShow.setBackground(Color.white)
resultShow.setEditable(false)
panel=new JPanel()
panel.setLayout(new GridLayout(4,5))
panel.add(nbtn[1])
panel.add(nbtn[2])
panel.add(nbtn[3])
panel.add(obtn[0])
panel.add(clbtn)
panel.add(nbtn[4])
panel.add(nbtn[5])
panel.add(nbtn[6])
panel.add(obtn[1])
panel.add(Backspacebtn)
panel.add(nbtn[7])
panel.add(nbtn[8])
panel.add(nbtn[9])
panel.add(obtn[2])
panel.add(recbtn)
panel.add(nbtn[0])
panel.add(PlusOrMinusbtn)
panel.add(dotbtn)
panel.add(obtn[3])
panel.add(equbtn)
add(panel,BorderLayout.CENTER)
add(resultShow,BorderLayout.NORTH)
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{
System.exit(0)
}
})
setVisible(true)
setBounds(100,50,240,180)
setResizable(false)
validate()
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() instanceof Nbtn)
{
Nbtn b=(Nbtn)e.getSource()
if(link.size()==0)
{
int num=b.getNum()
link.add(""+num)
resultShow.setText(""+num)
sfcequ=false
}
else if(link.size()==1&&sfcequ==false)
{
int num=b.getNum()
String Number=(String)link.getFirst()
String s=Number.concat(""+num)
link.set(0,s)
resultShow.setText(s)
}
else if(link.size()==1&&sfcequ==true)
{
int num=b.getNum()
link.removeFirst()
link.add(""+num)
sfcequ=false
resultShow.setText(""+num)
}
else if(link.size()==2)
{
int num=b.getNum()
link.add(""+num)
resultShow.setText(""+num)
}
else if(link.size()==3)
{
int num=b.getNum()
String Number=(String)link.getLast()
String s=Number.concat(""+num)
link.set(2,s)
resultShow.setText(s)
}
}
else if(e.getSource() instanceof Obtn)
{
Obtn b=(Obtn)e.getSource()
if(link.size()==1)
{
String sign=b.getOper()
link.add(sign)
}
else if(link.size()==2)
{
String sign=b.getOper()
link.set(1,sign)
}
else if(link.size()==3)
{
String sign=b.getOper()
String num1=(String)link.getFirst()
String num2=(String)link.getLast()
String Oper=(String)link.get(1)
try
{
double n1=Double.parseDouble(num1)
double n2=Double.parseDouble(num2)
double n=0
if(Oper.equals("+"))
{
n=n1+n2
}
else if(Oper.equals("-"))
{
n=n1-n2
}
else if(Oper.equals("*"))
{
n=n1*n2
}
else if(Oper.equals("/"))
{
n=n1/n2
}
link.clear()
link.add(""+n)
link.add(sign)
resultShow.setText(""+n)
}
catch(Exception ee)
{
}
}
}
else if(e.getSource()==equbtn)
{
sfcequ=true
if(link.size()==1||link.size()==2)
{
String num=(String)link.getFirst()
resultShow.setText(""+num)
}
else if(link.size()==3)
{
String num1=(String)link.getFirst()
String num2=(String)link.getLast()
String sign=(String)link.get(1)
try
{
double n1=Double.parseDouble(num1)
double n2=Double.parseDouble(num2)
double n=0
if(sign.equals("+"))
{
n=n1+n2
}
else if(sign.equals("-"))
{
n=n1-n2
}
else if(sign.equals("*"))
{
n=n1*n2
}
else if(sign.equals("/"))
{
n=n1/n2
}
resultShow.setText(""+n)
link.set(0,""+n)
link.removeLast()
link.removeLast()
}
catch(Exception ee)
{
}
}
}
else if(e.getSource()==dotbtn)
{
if(link.size()==0)
{
sfcequ=false
}
else if(link.size()==1)
{
String dot=dotbtn.getLabel()
String num=(String)link.getFirst()
String s=null
if(num.indexOf(dot)==-1)
{
s=num.concat(dot)
link.set(0,s)
}
else
{
s=num
}
link.set(0,s)
resultShow.setText(s)
}
else if(link.size()==3)
{
String dot=dotbtn.getLabel()
String num=(String)link.getLast()
String s=null
if(num.indexOf(dot)==-1)
{
s=num.concat(dot)
link.set(2,s)
}
else
{
s=num
}
resultShow.setText(s)
}
}
else if(e.getSource()==Backspacebtn)
{
if(link.size()==1)
{
String num=(String)link.getFirst()
if(num.length()>=1)
{
num=num.substring(0,num.length()-1)
link.set(0,num)
resultShow.setText(num)
}
else
{
link.removeLast()
resultShow.setText("0")
}
}
else if(link.size()==3)
{
String num=(String)link.getLast()
if(num.length()>=1)
{
num=num.substring(0,num.length()-1)
link.set(0,num)
resultShow.setText(num)
}
else
{
link.removeLast()
resultShow.setText("0")
}
}
}
else if(e.getSource()==PlusOrMinusbtn)
{
if(link.size()==1)
{
String num1=(String)link.getFirst()
try
{
double d=Double.parseDouble(num1)
d=-1*d
String str=String.valueOf(d)
link.set(0,str)
resultShow.setText(str)
}
catch(Exception ee)
{
}
}
else if(link.size()==3)
{
String num2=(String)link.getLast()
try
{
double d=Double.parseDouble(num2)
d=-1*d
String str=String.valueOf(d)
link.set(2,str)
resultShow.setText(str)
}
catch(Exception ee)
{
}
}
}
else if(e.getSource()==recbtn)
{
if(link.size()==1||link.size()==2)
{
String num1=(String)link.getFirst()
try
{
double d=Double.parseDouble(num1)
d=1.0/d
String str=String.valueOf(d)
link.set(0,str)
resultShow.setText(str)
}
catch(Exception ee)
{
}
}
else if(link.size()==3)
{
String num2=(String)link.getLast()
try
{
double d=Double.parseDouble(num2)
d=1.0/d
String str=String.valueOf(d)
link.set(0,str)
}
catch(Exception ee)
{
}
}
}
else if(e.getSource()==clbtn)
{
sfcequ=false
resultShow.setText("0")
link.clear()
}
}
public static void main(String agrs[])
{
new Cadev()
}
}
Nbtn.java
import java.awt.*
import java.awt.event.*
import javax.swing.*
public class Nbtn extends Button
{
int Num
public Nbtn(int n)
{
super(""+n)
Num=n
setForeground(Color.blue)
}
public int getNum()
{
return Num
}
}
Obtn.java
import java.awt.*
import java.awt.event.*
import javax.swing.*
public class Obtn extends Button
{
String Oper
public Obtn(String s)
{
super(s)
Oper=s
setForeground(Color.red)
}
public String getOper()
{
return Oper
}
}