import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class JiSuanQi {
public static void main(String[] args) {
JieMian j = new JieMian();
j.getJieMian();
}
}
class JieMian{
public static StringBuffer sb = new StringBuffer();//定义全局变量,存储要输入的数据
public static int flag ;
public static int flag2 ;
public static double y=-1;
public static double x=-1;
public static int sum;//如果没有按数字,直接按了符号键会抛出很多异常,所以添加一个标记用来记录键入数字的个数,只有在个数大于零的时候,各个符号键才运行。
public JTextField text=new JTextField();
public void getJieMian(){
JFrame frame = new JFrame("计算器") ;
frame.setResizable(false);//关键代码:设置窗体能否被任意拖动大小。
frame.setVisible(true);
frame.setSize(500,300);
frame.setLocation(300, 300);
frame.setLayout(null);//这一句是非常关键的,如果不加这一句,组件就会充满整个窗口!!!
text.setVisible(true);
text.setBackground(Color.lightGray);
text.setBounds(0,230,300,30);
JPanel p1 = new JPanel();
p1.setBounds(0, 0,200,300);
p1.setSize(300,180);
p1.setVisible(true);
p1.setLayout(new GridLayout(4,3,3,3));
final JButton but1 = new JButton("1");
p1.add(but1);
final JButton but2 = new JButton("2");
p1.add(but2);
final JButton but3 = new JButton("3");
p1.add(but3);
final JButton but4 = new JButton("4");
p1.add(but4);
final JButton but5 = new JButton("5");
p1.add(but5);
final JButton but6 = new JButton("6");
p1.add(but6);
final JButton but7 = new JButton("7");
p1.add(but7);
final JButton but8 = new JButton("8");
p1.add(but8);
final JButton but9 = new JButton("9");
p1.add(but9);
final JButton zero = new JButton("0");
final JButton dot = new JButton(".");
final JButton tui = new JButton("归零");
p1.add(zero);
p1.add(dot);
p1.add(tui);
JPanel p2 = new JPanel();
p2.setSize(300,50);
p2.setVisible(true);
p2.setBounds(0,180, 300, 50);
p2.setLayout(new GridLayout(1,5,3,3));
final JButton jia = new JButton("+");
final JButton jian = new JButton("-");
final JButton cheng = new JButton("×");
final JButton chu = new JButton("/");
final JButton deng = new JButton("=");
p2.add(jia);
p2.add(jian);
p2.add(cheng);
p2.add(chu);
p2.add(deng);
frame.add(p1);
frame.add(p2);
frame.add(text);
//下面就是运算部分的代码,进行监听器的添加和各个按钮具体功能的实现。
but1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sb = sb.append("1");
sum++;
x = Double.valueOf(sb.toString());
text.setText(""+sb);
}
});
but2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sb = sb.append("2");
sum++;
x = Double.valueOf(sb.toString());
text.setText(""+sb);
}
});
but3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
sb = sb.append("3");
sum++;
x = Double.valueOf(sb.toString());
text.setText(""+sb);
}
});
but4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sb = sb.append("4");
sum++;
x = Double.valueOf(sb.toString());
text.setText(""+sb);
}
});
but5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sb = sb.append("5");
sum++;
x = Double.valueOf(sb.toString());
text.setText(""+sb);
}
});
but6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sb = sb.append("6");
sum++;
x = Double.valueOf(sb.toString());
text.setText(""+sb);
}
});
but7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sb = sb.append("7");
sum++;
x = Double.valueOf(sb.toString());
text.setText(""+sb);
}
});
but8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sb = sb.append("8");
sum++;
x = Double.valueOf(sb.toString());
text.setText(""+sb);
}
});
but9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sb = sb.append("9");
sum++;
x = Double.valueOf(sb.toString());
text.setText(""+sb);
}
});
zero.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sb = sb.append("0");
sum++;
x = Double.valueOf(sb.toString());
text.setText(""+sb);
}
});
dot.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if ((e.getSource()==dot)&&(flag2!=7)&&(sb.length()>0)){
sb = sb.append(".");
flag2 = 7 ;
sum++;
x = Double.valueOf(sb.toString());
text.setText(""+sb);
}
}
});
tui.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(sb!=null){
sb = sb.delete(0, sb.length());
x = 0;
y = 0;
flag = 5;
flag2 = 6 ;
}
text.setText("0");
}
});
jia.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
while (sum!=0){
y= Double.valueOf(sb.toString());
x= y;
flag = 1 ;
sb.delete(0, sb.length());
text.setText("+");
sum=0;
}
}
});
jian.addActionListener (new ActionListener() {
public void actionPerformed (ActionEvent e) {
while(sum!=0){
x= Double.valueOf(sb.toString());
y= x;
flag= 2 ;
sb.delete(0, sb.length());
sum=0;
text.setText("-");
}
}
});
cheng.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
while(sum!=0){
x= Double.valueOf(sb.toString());
y= x;
flag = 3 ;
sb.delete(0, sb.length());
sum = 0 ;
text.setText("x");
}
}
});
chu.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
while (sum!=0){
x= Double.valueOf(sb.toString());
y= x;
flag = 4 ;
sb.delete(0, sb.length());
sum = 0 ;
text.setText("/");
};
}
});
deng.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
while(sum != 0){
if(sb.length()!=1)
x= Double.valueOf((sb.toString()));
sb.delete(0, sb.length());
sum = 0 ;
if(y<0)text.setText(""+x);
if(flag==1)
text.setText(""+(y+x));
if(flag==2)
text.setText(""+(y-x));
if(flag==3)
text.setText(""+(y*x));
if(flag==4){
if(x==0)
text.setText("被除数不能为零");
else text.setText(" "+(y/x));
}
flag = 6 ;
flag2 = 6 ;
}
}
});
}
}