java编写的计算器

import java.awt.*;
import java.awt.event.*;
public class JSQ extends Frame implements ActionListener
{
int x=0,y=0,z=0,sum=0;
Font f=new Font("Courier",Font.BOLD+Font.ITALIC,20);//BOLD 加粗,ITALIC 斜体
Font fl=new Font("Courier",Font.BOLD+Font.ITALIC,25);
Font f2=new Font("宋体",Font.ITALIC,15);
Font f3=new Font("Couier",Font.ITALIC,15);
MenuBar mb=new MenuBar();
Menu mf=new Menu("File");
Menu me=new Menu("Edit");
Menu mv=new Menu("View");
Menu mh=new Menu("Help");
MenuItem mfn=new MenuItem("New");
MenuItem mfo=new MenuItem("Open");
MenuItem mfe=new MenuItem("Exit");
MenuItem mee=new MenuItem("Note");
MenuItem mec=new MenuItem("Copy");
MenuItem mep=new MenuItem("Paste");
TextField tf=new TextField();
Button one=new Button("1");
Button two=new Button("2");
Button three=new Button("3");
Button four=new Button("4");
Button five=new Button("5");
Button six=new Button("6");
Button seven=new Button("7");
Button eight=new Button("8");
Button nine=new Button("9");
Button zero=new Button("0");
Button add=new Button("+");
Button jian=new Button("-");
Button cheng=new Button("*");
Button chu=new Button("/");
Button deng=new Button("=");
Button ce=new Button("CE");
Panel p=new Panel();
public JSQ()
{
this.setTitle("My Jisuanqi");
this.setBounds(200,200,220,200);
this.setEnabled(true);
this.setResizable(false);
this.setMenuBar(mb);
this.add(p);
mb.add(mf);
mb.add(mv);
mb.add(me);
mb.add(mh);
mf.add(mfn);
mf.add(mfo);
mf.addSeparator();
mf.add(mfe);
me.add(mec);
me.add(mep);
me.add(mee);
mf.setFont(f2);
mfn.setFont(f2);
mfe.setFont(f2);
mee.addActionListener(this);
mfe.addActionListener(this);
mfn.addActionListener(this);
this.addWindowListener(new Adp());//Listener(new Adp());
this.add(tf,BorderLayout.NORTH);
p.setLayout(new GridLayout(4,4));//网格布局管理器3行5列
p.add(one);
three.setFont(f);
four.setFont(f);
one.setFont(f);
five.setFont(f);
six.setFont(f);
seven.setFont(f);
eight.setFont(f);
nine.setFont(f);
zero.setFont(f);
add.setFont(fl);
jian.setFont(fl);
cheng.setFont(fl);
chu.setFont(fl);
ce.setFont(fl);
deng.setFont(fl);
one.addActionListener(this);
two.addActionListener(this);
three.addActionListener(this);
four.addActionListener(this);
five.addActionListener(this);
six.addActionListener(this);
seven.addActionListener(this);
eight.addActionListener(this);
nine.addActionListener(this);
zero.addActionListener(this);
add.addActionListener(this);
jian.addActionListener(this);
cheng.addActionListener(this);
chu.addActionListener(this);
ce.addActionListener(this);
deng.addActionListener(this);
add.setBackground(Color.DARK_GRAY);
jian.setBackground(Color.DARK_GRAY);
cheng.setBackground(Color.DARK_GRAY);
chu.setBackground(Color.DARK_

GRAY);
ce.setBackground(Color.DARK_GRAY);
deng.setBackground(Color.DARK_GRAY);
// one.setBounds(10,10,10,10);
// one.setSize(10,10);
// one.setBackground(Color.CYAN);
p.add(two);
two.setFont(f);
p.add(three);
p.add(four);
p.add(five);
p.add(six);
p.add(seven);
p.add(eight);
p.add(nine);
p.add(zero);
p.add(add);
p.add(jian);
p.add(cheng);
p.add(chu);
p.add(ce);
p.add(deng);
p.setVisible(true);
this.setVisible(true);
}
public static void main(String[]args)
{
new JSQ();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==mfn){
new JSQ();
// new TextAreaEx();
}else if(e.getSource()==mfe){
this.dispose();
}
if(e.getSource()==mee){
new TextArea();
}
if(e.getSource()==zero)
{
tf.setFont(f3);
tf.setText(tf.getText().trim()+0);
}else if(e.getSource()==one){
tf.setFont(f3);
tf.setText(tf.getText().trim()+1);
}else if(e.getSource()==two){
tf.setFont(f3);
tf.setText(tf.getText().trim()+2);
}else if(e.getSource()==three){
tf.setFont(f3);
tf.setText(tf.getText().trim()+3);
}else if(e.getSource()==four){
tf.setFont(f3);
tf.setText(tf.getText().trim()+4);
}else if(e.getSource()==five){
tf.setFont(f3);
tf.setText(tf.getText().trim()+5);
}else if(e.getSource()==six){
tf.setFont(f3);
tf.setText(tf.getText().trim()+6);
}else if(e.getSource()==seven){
tf.setFont(f3);
tf.setText(tf.getText().trim()+7);
}else if(e.getSource()==eight){
tf.setFont(f3);
tf.setText(tf.getText().trim()+8);
}else if(e.getSource()==nine){
tf.setFont(f3);
tf.setText(tf.getText().trim()+9);
}else if(tf.getText().trim().length()<=0){
return ;
}else if (e.getSource()==add){
z=0;
x=Integer.parseInt(tf.getText().trim());
tf.setText("");
}else if(e.getSource()==jian){
z=1;
x=Integer.parseInt(tf.getText().trim());
tf.setText("");
}else if(e.getSource()==cheng){
z=2;
x=Integer.parseInt(tf.getText().trim());
tf.setText("");
}else if(e.getSource()==chu){
z=3;
x=Integer.parseInt(tf.getText().trim());
tf.setText("");
}else if(e.getSource()==deng){
y=Integer.parseInt(tf.getText().trim());
switch(z)
{
case 0:sum=x+y;break;
case 1:sum=x-y;break;
case 2:sum=x*y;break;
case 3:/*(double)*/sum=x/y;break;

}
tf.setFont(f3);
tf.setText(sum+"");
}else if(e.getSource()==ce){
tf.setText("");
}

}
}
class Adp extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
class TextAreaEx extends Frame implements TextListener,ActionListener
{
TextArea ta=new TextArea();
TextArea tb=new TextArea();
Font f=new Font("Courier",Font.BOLD+Font.ITALIC,30);//BOLD 加粗
public TextAreaEx()
{
this.setTitle("TextArea");
this.setLayout(new GridLayout(2,1));
this.addWindowListener(new Ad

p());//add adapter
this.add(ta);
ta.setFont(f);
ta.addTextListener(this);
this.add(tb);
tb.setFont(f);
tb.setForeground(Color.red);
this.setBounds(100,100,400,400);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)//achieve actionPerformed
{
System.out.println("efefe");
}
public void textValueChanged(TextEvent e)
{
tb.setText(ta.getText());
//ta.setText(tb.getText());
}
// public static void main(String args[])
// {
// new TextAreaEx();
// }
}

相关文档
最新文档