java - calculator


看了几天JAVA书了,闲着闲着没事就写了个简单的计算器


没有一个好的计划总是这么忙,忙到我好多天都没去碰题目了,真的要好好复习了


贴个代码:

package bao;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class Window extends JFrame {
	
	int flag = 0;
	int csum = 0;
	char ch;
	double num;
	
//	JPanel body = new JPanel();
	Window() {
		setLayout(new BorderLayout());
		
		JPanel p1 = new JPanel();
		JTextField tex_1 = new JTextField("", 20);
		
		p1.add(tex_1);
		
		JPanel p2 = new JPanel();
		p2.setLayout(new GridLayout(4, 0, 4, 6));
		
//		Icon iImage = new ImageIcon("abc.jpg");
		JButton b1 = new JButton("1");
		
		p2.add(b1);
		JButton b2 = new JButton("2");
		p2.add(b2);
		JButton b3 = new JButton("3");
		p2.add(b3);
		JButton b4 = new JButton("+");
		p2.add(b4);
		JButton b5 = new JButton("4");
		p2.add(b5);
		JButton b6 = new JButton("5");
		p2.add(b6);
		JButton b7 = new JButton("6");
		p2.add(b7);
		JButton b8 = new JButton("-");
		p2.add(b8);
		JButton b9 = new JButton("7");
		p2.add(b9);
		JButton b10 = new JButton("8");
		p2.add(b10);
		JButton b11 = new JButton("9");
		p2.add(b11);
		JButton b12 = new JButton("*");
		p2.add(b12);
		JButton b13 = new JButton(".");
		p2.add(b13);
		JButton b14 = new JButton("0");
		p2.add(b14);
		JButton b15 = new JButton("=");
		p2.add(b15);
		JButton b16 = new JButton("/");
		p2.add(b16);
		
		b1.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if(flag == 1) tex_1.setText("");
				flag = 0;
				String str = tex_1.getText();
				tex_1.setText(str+"1");
				
			}
		});
		
		b2.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if(flag == 1) tex_1.setText("");
				flag = 0;
				String str = tex_1.getText();
				tex_1.setText(str+"2");
				
			}
		});
		
		b3.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if(flag == 1) tex_1.setText("");
				flag = 0;
				String str = tex_1.getText();
				tex_1.setText(str+"3");
				
			}
		});
		
		b4.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				String str = tex_1.getText();
				num = Double.parseDouble(str);
				tex_1.setText("");
				ch = '+';
			}
		});
		
		
		b5.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if(flag == 1) tex_1.setText("");
				flag = 0;
				String str = tex_1.getText();
				tex_1.setText(str+"4");
				
			}
		});
		
		b6.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if(flag == 1) tex_1.setText("");
				flag = 0;
				
				String str = tex_1.getText();
				tex_1.setText(str+"5");
				
			}
		});
		
		b7.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if(flag == 1) tex_1.setText("");
				flag = 0;
				String str = tex_1.getText();
				tex_1.setText(str+"6");
				
			}
		});
		
		b8.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				String str = tex_1.getText();
				num = Double.parseDouble(str);
				tex_1.setText("");
				ch = '-';
			}
		});
		
		b9.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if(flag == 1) tex_1.setText("");
				flag = 0;
				String str = tex_1.getText();
				tex_1.setText(str+"7");
				
			}
		});
		
		b10.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if(flag == 1) tex_1.setText("");
				flag = 0;
				String str = tex_1.getText();
				tex_1.setText(str+"8");
				
			}
		});
		
		b11.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if(flag == 1) tex_1.setText("");
				flag = 0;
				String str = tex_1.getText();
				tex_1.setText(str+"9");
				
			}
		});
		
		b12.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				String str = tex_1.getText();
				num = Double.parseDouble(str);
				tex_1.setText("");
				ch = '*';
			}
		});
		
		b13.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				String str = tex_1.getText();
				tex_1.setText(str+".");
				
			}
		});
		
		b14.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if(flag == 1) tex_1.setText("");
				flag = 0;
				String str = tex_1.getText();
				tex_1.setText(str+"0");
				
			}
		});
		
		b15.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				String str = tex_1.getText();
				double tmp = Double.parseDouble(str);
				double ans = 0;
				if(ch == '+') {
					ans = num + tmp;
				}
				else if(ch == '-') {
					ans = num - tmp;
				}
				else if(ch == '*') {
					ans = num * tmp;
				}
				else if(ch == '/') {
					ans = num / tmp;
				}
				tex_1.setText("");
				tex_1.setText(""+ans);
				flag = 1;
				num = 0;
			}
		});
		
		b16.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				String str = tex_1.getText();
				num = Double.parseDouble(str);
				tex_1.setText("");
				ch = '/';
			}
		});
		
		
		
//		p2.add(new JButton("1"));
		
		
		add("North", p1);
		add("South", p2);
	}
	
	
}


public class calculator {
	public static void main(String args[]) {
		Window window = new Window();
		window.setTitle("calculator");
		window.pack();
		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		window.setVisible(true);
	}
}














你可能感兴趣的:(java)