计算器
/**
*
*/
package Lyt;
import javax.swing.JFrame;
/**
* @author Administrator
*
*/
public class lyt {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
TextPanel frame=new TextPanel();
frame.setTitle("计算器");
frame.setSize(172, 180);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
/**
*
*/
package Lyt;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
* @author Administrator
*
*/
public class TextPanel extends JFrame{
/**
* @param args
*/
JTextField l1=new JTextField("");
JButton b0=new JButton("0");
JButton b1=new JButton("1");
JButton b2=new JButton("2");
JButton b3=new JButton("3");
JButton b4=new JButton("4");
JButton b5=new JButton("5");
JButton b6=new JButton("6");
JButton b7=new JButton("7");
JButton b8=new JButton("8");
JButton b9=new JButton("9");
JButton b10=new JButton("+");
JButton b11=new JButton("-");
JButton b12=new JButton("*");
JButton b13=new JButton("/");
JButton b14=new JButton("=");
JButton b15=new JButton(".");
double []sum0=new double[2];
double sum=0;
int i=0,n=0;String str="";
public TextPanel()
{
for(int j=0;j<2;j++)
sum0[j]=0;
JPanel p1=new JPanel();
p1.setLayout(new GridLayout(4,4));
p1.add(b1);
p1.add(b2);
p1.add(b3);
p1.add(b10);
p1.add(b4);
p1.add(b5);
p1.add(b6);
p1.add(b11);
p1.add(b7);
p1.add(b8);
p1.add(b9);
p1.add(b12);
p1.add(b15);
p1.add(b0);
p1.add(b14);
p1.add(b13);
add(p1,BorderLayout.EAST);
add(l1,BorderLayout.NORTH);
b1.addActionListener(new ButtonListener());
b2.addActionListener(new ButtonListener());
b3.addActionListener(new ButtonListener());
b4.addActionListener(new ButtonListener());
b5.addActionListener(new ButtonListener());
b6.addActionListener(new ButtonListener());
b7.addActionListener(new ButtonListener());
b8.addActionListener(new ButtonListener());
b9.addActionListener(new ButtonListener());
b10.addActionListener(new ButtonListener());
b11.addActionListener(new ButtonListener());
b12.addActionListener(new ButtonListener());
b13.addActionListener(new ButtonListener());
b14.addActionListener(new ButtonListener());
b15.addActionListener(new ButtonListener());
}
private class ButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==b1)
{str=str+"1";
l1.setText(String.format("%s", str));
sum0[i]=Double.parseDouble(l1.getText());
}
if(e.getSource()==b0)
{str=str+"0";
l1.setText(String.format("%s", str));
sum0[i]=Double.parseDouble(l1.getText());
}
if(e.getSource()==b2)
{str=str+"2";
l1.setText(String.format("%s", str));
sum0[i]=Double.parseDouble(l1.getText());
}
if(e.getSource()==b3)
{str=str+"3";
l1.setText(String.format("%s", str));
sum0[i]=Double.parseDouble(l1.getText());
}
if(e.getSource()==b4)
{str=str+"4";
l1.setText(String.format("%s", str));
sum0[i]=Double.parseDouble(l1.getText());
}
if(e.getSource()==b5)
{str=str+"5";
l1.setText(String.format("%s", str));
sum0[i]=Double.parseDouble(l1.getText());
}
if(e.getSource()==b6)
{str=str+"6";
l1.setText(String.format("%s", str));
sum0[i]=Double.parseDouble(l1.getText());
}
if(e.getSource()==b7)
{str=str+"7";
l1.setText(String.format("%s", str));
sum0[i]=Double.parseDouble(l1.getText());
}
if(e.getSource()==b8)
{str=str+"8";
l1.setText(String.format("%s", str));
sum0[i]=Double.parseDouble(l1.getText());
}
if(e.getSource()==b9)
{str=str+"9";
l1.setText(String.format("%s", str));
sum0[i]=Double.parseDouble(l1.getText());
}
if(e.getSource()==b10) //+
{
str="";
n=10;
i++;
l1.setText(String.format("%s", str));
//sum0[i++]=Double.parseDouble(l1.getText());
}
if(e.getSource()==b11) //-
{
n=11;
i++;
str="";
l1.setText(String.format("%s", str));
//sum0[i++]=Double.parseDouble(l1.getText());
}
if(e.getSource()==b12) //*
{
n=12;
i++;
str="";
l1.setText(String.format("%s", str));
//sum0[i++]=Double.parseDouble(l1.getText());
}
if(e.getSource()==b13) ///
{
n=13;
i++;
str="";
l1.setText(String.format("%s", str));
// sum0[i++]=Double.parseDouble(l1.getText());
}
if(e.getSource()==b14) //=
{
if(n==10)
sum=sum0[0]+sum0[1];
else if(n==11)
sum=sum0[0]-sum0[1];
else if(n==12)
sum=sum0[0]*sum0[1];
else if(n==13)
sum=sum0[0]*1.0/sum0[1];
l1.setText(String.format("%.2f", sum));
sum=0;
sum0[0]=0;
sum0[1]=0;
i=0;
str="";
}
if(e.getSource()==b15) //.
{
//sum0=Double.parseDouble(l1.getText());
str=str+".";
l1.setText(String.format("%s", str));
sum0[i]=Double.parseDouble(l1.getText());
}
}
}
}