import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator implements ActionListener
{
String s="",s1;
double d1,d2;
JFrame jf = new JFrame("å°è®¡ç®å¨by Graduate") ;
JTextField tf = new JTextField();
public void init()//å®ç°è®¡ç®å¨çé¢
{
Container c=jf.getContentPane();
tf.setHorizontalAlignment(JTextField.RIGHT);//ææ¬æ¡
c.add(tf,"North");
JPanel pn3 = new JPanel(new BorderLayout());
c.add(pn3,"Center");
JPanel pn2 = new JPanel();//åè½é®çé¢ï¼æ¸
é¤é®åå
³éé®ï¼
pn2.setLayout(new BorderLayout());
JPanel pn1 = new JPanel();//è¿ç®çé¢
pn1.setLayout(new GridLayout(4,4));
pn3.add(pn2,"North");
pn3.add(pn1);
//设置æé®
JButton b = new JButton("CLEAR");
b.setToolTipText("请ææ¸
é¤é®!");//设置æ¸
é¶é®
b.setForeground(Color.RED);//设置åä½é¢è²
b.setBackground(Color.YELLOW);//设置èæ¯è²
b.addActionListener(this);
pn2.add(b,"Center");
b = new JButton("OFF");
b.setToolTipText("请æéåºé®!");//设置offé®ï¼ç¹å»éåºåºç¨ç¨åºb.addActionListener(this);
b.setForeground(Color.RED);//åä½é¢è²
b.setBackground(Color.ORANGE);//èæ¯è²
pn2.add(b,"East");
b = new JButton("1");//add butten 1
b.addActionListener(this);
pn1.add(b);
b = new JButton("2");//add butten 2
b.addActionListener(this);
pn1.add(b);
b = new JButton("3");//add butten 3
b.addActionListener(this);
pn1.add(b);
b = new JButton("+");//add butten +
b.setForeground(Color.BLUE);//设置åä½é¢è²
b.addActionListener(this);
pn1.add(b);
b = new JButton("4");//add butten 4
b.addActionListener(this);
pn1.add(b);
b = new JButton("5");//add butten 5
b.addActionListener(this);
pn1.add(b);
b = new JButton("6");//add button 6
b.addActionListener(this);
pn1.add(b);
b = new JButton("-");//add button -
b.setForeground(Color.BLUE);//设置åä½é¢è²
b.addActionListener(this);
pn1.add(b);
b = new JButton("7");//add button 7
b.addActionListener(this);
pn1.add(b);
b = new JButton("8");//add button 8
b.addActionListener(this);
pn1.add(b);
b = new JButton("9");//add button 9
b.addActionListener(this);
pn1.add(b);
b = new JButton("*");//add button *
b.setForeground(Color.BLUE);//设置åä½é¢è²
b.addActionListener(this);
pn1.add(b);
b = new JButton("0");//add button 0
b.addActionListener(this);
pn1.add(b);
b = new JButton(".");//add button .
b.addActionListener(this);
pn1.add(b);
b = new JButton("=");//add button =
b.setForeground(Color.RED);//设置åä½é¢è²
b.addActionListener(this);
pn1.add(b);
b = new JButton("\\");//add button \
b.setForeground(Color.BLUE);//设置åä½é¢è²
b.addActionListener(this);
pn1.add(b);
jf.setSize(300,300);//设置大å°
jf.setVisible(true);//设置为å¯è§
}
//å¤çæé®æä¸æ¶çå¨ä½ï¼è¿è¡ç¸åºçå¤ç
public void actionPerformed(ActionEvent e)
{
String command = e.getActionCommand();
tf.setText(tf.getText()+command);
if(command.equals("CLEAR")) //æ¸
é¶é® æä¸æ¶è¿ååå§ç¶æ
{
s1=null;
s="";
tf.setText("");//è®°å½è¾å
¥å¼çåéæ¸
空
}
else if(command.equals("OFF")) System.exit(0);//offé® å
³éåºç¨ç¨åº
else if(!command.equals("*")&&!command.equals("\\")
&&!command.equals("+")&&!command.equals("-")
&&!command.equals("="))//å¤æè¾å
¥æ¯å¦ä¸ºæ°å
{
if(s1==null)//å¤æè¾å
¥æ¯å¦ä¸ºç¬¬ä¸ä¸ª
s1 = command;
else s1+=command;
d1 = new Double(s1).doubleValue();//å符串å转æ¢ä¸ºå精度å,è¿åè¾å
¥æ°å
try
{
if(s.equals("+")) d1 = d1+d2;//å æ³è¿ç®
else if(s.equals("-")) d1 = d2-d1;//åæ³è¿ç®
else if(s.equals("*")) d1 = d1*d2;//ä¹æ³è¿ç®
else if(s.equals("\\"))d1 = d2/d1;//é¤æ³è¿ç®
}
catch(Exception ex)
{
tf.setText("Error");//é误æ¾ç¤º"Error"
System.out.println(ex.getMessage());
}
}
else if(!command.equals("=")) //å¤æè¾å
¥æ¯å¦ä¸º+ - * \
{
s = command;
s1 = null;
d2 = d1;
}
else//è¾å
¥=æ¶,æ¾ç¤ºè¿ç®ç»æ
{
tf.setText(tf.getText()+d1);
}
}
public static void main(String [] args)
{
new Calculator().init();
}
}
温馨提示:内容为网友见解,仅供参考