Bài Tập Cơ Bản Đây_Calc

jb_hoangluan

Member
10/10/10
82
0
6
Đây là 1 câu trong bài thi của lớp mình:

Post cho anh em tham khảo ^^

Trong đó, có 1 vài lỗi ACE hoàn thiện dùm mình nhá....@_@ làm biến quá....
PHP:
package cal;

import javax.swing.SwingUtilities;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.GridBagLayout;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JTextField;
import java.awt.GridBagConstraints;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import java.awt.Insets;
import java.awt.Font;
import javax.swing.JMenuItem;

public class Calculator extends JFrame implements ActionListener {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	//---Variable
	final int DoDai_Max = 20;
	final int DL_BanDau= 0;
	final int Ket_Qua = 1;
	final int BaoLoi = 2;
	
	boolean XoaSo_KeTiep;
	String Thao_Tac_Cuoi;  //  @jve:decl-index=0:
	
	double lastNumber;
	int HienThi;
	
	
	private JMenuBar jJMenuBar = null;
	private JMenu mnuView = null;
	private JMenu mnuEdit = null;
	private JMenu mnuHelp = null;
	private JTextField txtKq = null;
	private JButton btnMC = null;
	private JButton btnBack = null;
	private JButton btn7 = null;
	private JButton btn4 = null;
	private JButton btn1 = null;
	private JButton btn0 = null;
	private JButton btnMR = null;
	private JButton btnCE = null;
	private JButton btn8 = null;
	private JButton btn5 = null;
	private JButton btn2 = null;
	private JButton btnMS = null;
	private JButton btnC = null;
	private JButton btn9 = null;
	private JButton btn6 = null;
	private JButton btn3 = null;
	private JButton btnM1 = null;
	private JButton btnCongTru = null;
	private JButton btnChia = null;
	private JButton btnNhan = null;
	private JButton btnTru = null;
	private JButton btnCong = null;
	private JButton btnPhay = null;
	private JButton btnM2 = null;
	private JButton btnCan = null;
	private JButton btnPhanTram = null;
	private JButton btnDao = null;
	private JButton btnBang = null;
	private JMenuItem mniAbout = null;
	/**
	 * @param args
	 */
	/**
	 * This method initializes jJMenuBar	
	 * 	
	 * @return javax.swing.JMenuBar	
	 */
	private JMenuBar getJJMenuBar() {
		if (jJMenuBar == null) {
			jJMenuBar = new JMenuBar();
			jJMenuBar.add(getMnuView());
			jJMenuBar.add(getMnuEdit());
			jJMenuBar.add(getMnuHelp());
		}
		return jJMenuBar;
	}
	/**
	 * This method initializes mnuView	
	 * 	
	 * @return javax.swing.JMenu	
	 */
	private JMenu getMnuView() {
		if (mnuView == null) {
			mnuView = new JMenu();
			mnuView.setText("View");
		}
		return mnuView;
	}
	/**
	 * This method initializes mnuEdit	
	 * 	
	 * @return javax.swing.JMenu	
	 */
	private JMenu getMnuEdit() {
		if (mnuEdit == null) {
			mnuEdit = new JMenu();
			mnuEdit.setText("Edit");
		}
		return mnuEdit;
	}
	/**
	 * This method initializes mnuHelp	
	 * 	
	 * @return javax.swing.JMenu	
	 */
	private JMenu getMnuHelp() {
		if (mnuHelp == null) {
			mnuHelp = new JMenu();
			mnuHelp.setText("Help");
			mnuHelp.add(getMniAbout());
		}
		return mnuHelp;
	}
	/**
	 * This method initializes txtKq	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getTxtKq() {
		if (txtKq == null) {
			txtKq = new JTextField();
			txtKq.setHorizontalAlignment(JTextField.RIGHT);
			txtKq.setBackground(Color.white);
			txtKq.setText("0");
			txtKq.setFont(new Font("Times New Roman", Font.PLAIN, 18));
			txtKq.setEditable(false);
		}
		return txtKq;
	}
	/**
	 * This method initializes btnMC	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnMC() {
		if (btnMC == null) {
			btnMC = new JButton();
			btnMC.setText("MC");
			btnMC.addActionListener(this);
		}
		return btnMC;
	}
	/**
	 * This method initializes btnBack	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnBack() {
		if (btnBack == null) {
			btnBack = new JButton();
			btnBack.setIcon(new ImageIcon(getClass().getResource("/images/Back.png")));
			btnBack.addActionListener(this);
		}
		return btnBack;
	}
	/**
	 * This method initializes btn7	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtn7() {
		if (btn7 == null) {
			btn7 = new JButton();
			btn7.setText("7");
			btn7.addActionListener(this);
		}
		return btn7;
	}
	/**
	 * This method initializes btn4	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtn4() {
		if (btn4 == null) {
			btn4 = new JButton();
			btn4.setText("4");
			btn4.addActionListener(this);
		}
		return btn4;
	}
	/**
	 * This method initializes btn1	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtn1() {
		if (btn1 == null) {
			btn1 = new JButton();
			btn1.setText("1");
			btn1.addActionListener(this);
		}
		return btn1;
	}
	/**
	 * This method initializes btn0	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtn0() {
		if (btn0 == null) {
			btn0 = new JButton();
			btn0.setText("0");
			btn0.addActionListener(this);
		}
		return btn0;
	}
	/**
	 * This method initializes btnMR	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnMR() {
		if (btnMR == null) {
			btnMR = new JButton();
			btnMR.setText("MR");
			btnMR.addActionListener(this);
		}
		return btnMR;
	}
	/**
	 * This method initializes btnCE	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnCE() {
		if (btnCE == null) {
			btnCE = new JButton();
			btnCE.setText("CE");
			btnCE.addActionListener(this);
		}
		return btnCE;
	}
	/**
	 * This method initializes btn8	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtn8() {
		if (btn8 == null) {
			btn8 = new JButton();
			btn8.setText("8");
			btn8.addActionListener(this);
		}
		return btn8;
	}
	/**
	 * This method initializes btn5	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtn5() {
		if (btn5 == null) {
			btn5 = new JButton();
			btn5.setText("5");
			btn5.addActionListener(this);
		}
		return btn5;
	}
	/**
	 * This method initializes btn2	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtn2() {
		if (btn2 == null) {
			btn2 = new JButton();
			btn2.setText("2");
			btn2.addActionListener(this);
		}
		return btn2;
	}
	/**
	 * This method initializes btnMS	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnMS() {
		if (btnMS == null) {
			btnMS = new JButton();
			btnMS.setText("MS");
			btnMS.addActionListener(this);
		}
		return btnMS;
	}
	/**
	 * This method initializes btnC	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnC() {
		if (btnC == null) {
			btnC = new JButton();
			btnC.setText("C");
		}
		return btnC;
	}
	/**
	 * This method initializes btn9	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtn9() {
		if (btn9 == null) {
			btn9 = new JButton();
			btn9.setText("9");
			btn9.addActionListener(this);
		}
		return btn9;
	}
	/**
	 * This method initializes btn6	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtn6() {
		if (btn6 == null) {
			btn6 = new JButton();
			btn6.setText("6");
			btn6.addActionListener(this);
		}
		return btn6;
	}
	/**
	 * This method initializes btn3	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtn3() {
		if (btn3 == null) {
			btn3 = new JButton();
			btn3.setText("3");
			btn3.addActionListener(this);
		}
		return btn3;
	}
	/**
	 * This method initializes btnM1	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnM1() {
		if (btnM1 == null) {
			btnM1 = new JButton();
			btnM1.setText("M+");
			btnM1.addActionListener(this);
		}
		return btnM1;
	}
	/**
	 * This method initializes btnCongTru	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnCongTru() {
		if (btnCongTru == null) {
			btnCongTru = new JButton();
			btnCongTru.setText("+/-");
			btnCongTru.addActionListener(this);
		}
		return btnCongTru;
	}
	/**
	 * This method initializes btnChia	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnChia() {
		if (btnChia == null) {
			btnChia = new JButton();
			btnChia.setText("/");
			btnChia.addActionListener(this);
		}
		return btnChia;
	}

	/**
	 * This method initializes btnNhan	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnNhan() {
		if (btnNhan == null) {
			btnNhan = new JButton();
			btnNhan.setText("*");
			btnNhan.addActionListener(this);
		}
		return btnNhan;
	}

	/**
	 * This method initializes btnTru	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnTru() {
		if (btnTru == null) {
			btnTru = new JButton();
			btnTru.setText("-");
			btnTru.addActionListener(this);
		}
		return btnTru;
	}

	/**
	 * This method initializes btnCong	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnCong() {
		if (btnCong == null) {
			btnCong = new JButton();
			btnCong.setText("+");
			btnCong.addActionListener(this);
		}
		return btnCong;
	}

	/**
	 * This method initializes btnPhay	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnPhay() {
		if (btnPhay == null) {
			btnPhay = new JButton();
			btnPhay.setText(".");
			btnPhay.addActionListener(this);
		}
		return btnPhay;
	}
	/**
	 * This method initializes btnM2	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnM2() {
		if (btnM2 == null) {
			btnM2 = new JButton();
			btnM2.setText("M-");
			btnM2.addActionListener(this);
		}
		return btnM2;
	}
	/**
	 * This method initializes btnCan	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnCan() {
		if (btnCan == null) {
			btnCan = new JButton();
			btnCan.setText("Sqrt");
			btnCan.addActionListener(this);
		}
		return btnCan;
	}
	/**
	 * This method initializes btnPhanTram	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnPhanTram() {
		if (btnPhanTram == null) {
			btnPhanTram = new JButton();
			btnPhanTram.setText("%");
			btnPhanTram.addActionListener(this);
		}
		return btnPhanTram;
	}
	/**
	 * This method initializes btnDao	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnDao() {
		if (btnDao == null) {
			btnDao = new JButton();
			btnDao.setText("1/x");
			btnDao.addActionListener(this);
		}
		return btnDao;
	}
	/**
	 * This method initializes btnBang	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnBang() {
		if (btnBang == null) {
			btnBang = new JButton();
			btnBang.setText("=");
			btnBang.setFont(new Font("Dialog", Font.BOLD, 14));
			btnBang.addActionListener(this);
		}
		return btnBang;
	}
	/**
	 * This method initializes mniAbout	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getMniAbout() {
		if (mniAbout == null) {
			mniAbout = new JMenuItem();
			mniAbout.setText("About");
			mniAbout.addActionListener(new java.awt.event.ActionListener() {
				@SuppressWarnings("deprecation")
				public void actionPerformed(java.awt.event.ActionEvent e) {
					About ab = new About();
					ab.setMaximumSize(getMaximumSize());
					
					ab.show();
				}
			});
		}
		return mniAbout;
	}
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				Calculator thisClass = new Calculator();
				thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				thisClass.setVisible(true);
			}
		});
	}

	/**
	 * This is the default constructor
	 */
	public Calculator() {///----------Ke thua
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(333, 352);
		this.setResizable(false);
		this.setJMenuBar(getJJMenuBar());
		this.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/images/Calc.png")));
		this.setContentPane(getJContentPane());
		this.setTitle("Calculator");
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			GridBagConstraints gridBagConstraints32 = new GridBagConstraints();
			gridBagConstraints32.gridx = 4;
			gridBagConstraints32.fill = GridBagConstraints.BOTH;
			gridBagConstraints32.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints32.gridheight = 2;
			gridBagConstraints32.gridy = 6;
			GridBagConstraints gridBagConstraints31 = new GridBagConstraints();
			gridBagConstraints31.gridx = 4;
			gridBagConstraints31.fill = GridBagConstraints.BOTH;
			gridBagConstraints31.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints31.gridy = 5;
			GridBagConstraints gridBagConstraints30 = new GridBagConstraints();
			gridBagConstraints30.gridx = 4;
			gridBagConstraints30.fill = GridBagConstraints.BOTH;
			gridBagConstraints30.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints30.gridy = 4;
			GridBagConstraints gridBagConstraints29 = new GridBagConstraints();
			gridBagConstraints29.gridx = 4;
			gridBagConstraints29.fill = GridBagConstraints.BOTH;
			gridBagConstraints29.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints29.gridy = 3;
			GridBagConstraints gridBagConstraints28 = new GridBagConstraints();
			gridBagConstraints28.gridx = 4;
			gridBagConstraints28.fill = GridBagConstraints.BOTH;
			gridBagConstraints28.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints28.gridy = 1;
			GridBagConstraints gridBagConstraints27 = new GridBagConstraints();
			gridBagConstraints27.gridx = 2;
			gridBagConstraints27.fill = GridBagConstraints.BOTH;
			gridBagConstraints27.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints27.gridy = 7;
			GridBagConstraints gridBagConstraints26 = new GridBagConstraints();
			gridBagConstraints26.gridx = 3;
			gridBagConstraints26.fill = GridBagConstraints.BOTH;
			gridBagConstraints26.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints26.gridy = 7;
			GridBagConstraints gridBagConstraints25 = new GridBagConstraints();
			gridBagConstraints25.gridx = 3;
			gridBagConstraints25.fill = GridBagConstraints.BOTH;
			gridBagConstraints25.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints25.gridy = 6;
			GridBagConstraints gridBagConstraints24 = new GridBagConstraints();
			gridBagConstraints24.gridx = 3;
			gridBagConstraints24.fill = GridBagConstraints.BOTH;
			gridBagConstraints24.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints24.gridy = 5;
			GridBagConstraints gridBagConstraints23 = new GridBagConstraints();
			gridBagConstraints23.gridx = 3;
			gridBagConstraints23.fill = GridBagConstraints.BOTH;
			gridBagConstraints23.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints23.gridy = 4;
			GridBagConstraints gridBagConstraints22 = new GridBagConstraints();
			gridBagConstraints22.gridx = 3;
			gridBagConstraints22.fill = GridBagConstraints.BOTH;
			gridBagConstraints22.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints22.gridy = 3;
			GridBagConstraints gridBagConstraints21 = new GridBagConstraints();
			gridBagConstraints21.gridx = 3;
			gridBagConstraints21.fill = GridBagConstraints.BOTH;
			gridBagConstraints21.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints21.gridy = 1;
			GridBagConstraints gridBagConstraints20 = new GridBagConstraints();
			gridBagConstraints20.gridx = 2;
			gridBagConstraints20.fill = GridBagConstraints.BOTH;
			gridBagConstraints20.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints20.gridy = 6;
			GridBagConstraints gridBagConstraints19 = new GridBagConstraints();
			gridBagConstraints19.gridx = 2;
			gridBagConstraints19.fill = GridBagConstraints.BOTH;
			gridBagConstraints19.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints19.gridy = 5;
			GridBagConstraints gridBagConstraints18 = new GridBagConstraints();
			gridBagConstraints18.gridx = 2;
			gridBagConstraints18.fill = GridBagConstraints.BOTH;
			gridBagConstraints18.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints18.gridy = 4;
			GridBagConstraints gridBagConstraints17 = new GridBagConstraints();
			gridBagConstraints17.gridx = 2;
			gridBagConstraints17.fill = GridBagConstraints.BOTH;
			gridBagConstraints17.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints17.gridy = 3;
			GridBagConstraints gridBagConstraints16 = new GridBagConstraints();
			gridBagConstraints16.gridx = 2;
			gridBagConstraints16.fill = GridBagConstraints.BOTH;
			gridBagConstraints16.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints16.gridy = 1;
			GridBagConstraints gridBagConstraints14 = new GridBagConstraints();
			gridBagConstraints14.gridx = 1;
			gridBagConstraints14.fill = GridBagConstraints.BOTH;
			gridBagConstraints14.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints14.gridy = 6;
			GridBagConstraints gridBagConstraints13 = new GridBagConstraints();
			gridBagConstraints13.gridx = 1;
			gridBagConstraints13.fill = GridBagConstraints.BOTH;
			gridBagConstraints13.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints13.gridy = 5;
			GridBagConstraints gridBagConstraints12 = new GridBagConstraints();
			gridBagConstraints12.gridx = 1;
			gridBagConstraints12.fill = GridBagConstraints.BOTH;
			gridBagConstraints12.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints12.gridy = 4;
			GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
			gridBagConstraints11.gridx = 1;
			gridBagConstraints11.fill = GridBagConstraints.BOTH;
			gridBagConstraints11.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints11.gridy = 3;
			GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
			gridBagConstraints7.gridx = 1;
			gridBagConstraints7.fill = GridBagConstraints.BOTH;
			gridBagConstraints7.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints7.gridy = 1;
			GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
			gridBagConstraints6.gridx = 0;
			gridBagConstraints6.fill = GridBagConstraints.BOTH;
			gridBagConstraints6.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints6.gridwidth = 2;
			gridBagConstraints6.gridy = 7;
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.gridx = 0;
			gridBagConstraints5.fill = GridBagConstraints.BOTH;
			gridBagConstraints5.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints5.gridy = 6;
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.gridx = 0;
			gridBagConstraints4.fill = GridBagConstraints.BOTH;
			gridBagConstraints4.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints4.gridy = 5;
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.gridx = 0;
			gridBagConstraints3.fill = GridBagConstraints.BOTH;
			gridBagConstraints3.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints3.gridy = 4;
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.gridx = 0;
			gridBagConstraints2.fill = GridBagConstraints.BOTH;
			gridBagConstraints2.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints2.gridy = 3;
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.gridx = 0;
			gridBagConstraints1.fill = GridBagConstraints.BOTH;
			gridBagConstraints1.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints1.gridy = 1;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.fill = GridBagConstraints.BOTH;
			gridBagConstraints.gridy = 0;
			gridBagConstraints.weightx = 1.0;
			gridBagConstraints.gridwidth = 5;
			gridBagConstraints.weighty = 0.5;
			gridBagConstraints.gridheight = 1;
			gridBagConstraints.insets = new Insets(10, 5, 10, 5);
			gridBagConstraints.gridx = 0;
			jContentPane = new JPanel();
			jContentPane.setLayout(new GridBagLayout());
			jContentPane.add(getTxtKq(), gridBagConstraints);
			jContentPane.add(getBtnMC(), gridBagConstraints1);
			jContentPane.add(getBtnBack(), gridBagConstraints2);
			jContentPane.add(getBtn7(), gridBagConstraints3);
			jContentPane.add(getBtn4(), gridBagConstraints4);
			jContentPane.add(getBtn1(), gridBagConstraints5);
			jContentPane.add(getBtn0(), gridBagConstraints6);
			jContentPane.add(getBtnMR(), gridBagConstraints7);
			jContentPane.add(getBtnCE(), gridBagConstraints11);
			jContentPane.add(getBtn8(), gridBagConstraints12);
			jContentPane.add(getBtn5(), gridBagConstraints13);
			jContentPane.add(getBtn2(), gridBagConstraints14);
			jContentPane.add(getBtnMS(), gridBagConstraints16);
			jContentPane.add(getBtnC(), gridBagConstraints17);
			jContentPane.add(getBtn9(), gridBagConstraints18);
			jContentPane.add(getBtn6(), gridBagConstraints19);
			jContentPane.add(getBtn3(), gridBagConstraints20);
			jContentPane.add(getBtnM1(), gridBagConstraints21);
			jContentPane.add(getBtnCongTru(), gridBagConstraints22);
			jContentPane.add(getBtnChia(), gridBagConstraints23);
			jContentPane.add(getBtnNhan(), gridBagConstraints24);
			jContentPane.add(getBtnTru(), gridBagConstraints25);
			jContentPane.add(getBtnCong(), gridBagConstraints26);
			jContentPane.add(getBtnPhay(), gridBagConstraints27);
			jContentPane.add(getBtnM2(), gridBagConstraints28);
			jContentPane.add(getBtnCan(), gridBagConstraints29);
			jContentPane.add(getBtnPhanTram(), gridBagConstraints30);
			jContentPane.add(getBtnDao(), gridBagConstraints31);
			jContentPane.add(getBtnBang(), gridBagConstraints32);
		}
		return jContentPane;
	}

PHP:
	public void actionPerformed(ActionEvent e) {
		
		Object obj = e.getSource();
		if(obj == btn0)
			Them_So_Moi_Nhap(0);
		if(obj == btn1)
			Them_So_Moi_Nhap(1);				
		if(obj == btn2)
			Them_So_Moi_Nhap(2);
		if(obj == btn3)
			Them_So_Moi_Nhap(3);
		if(obj == btn4)
			Them_So_Moi_Nhap(4);
		if(obj == btn5)
			Them_So_Moi_Nhap(5);
		if(obj == btn6)
			Them_So_Moi_Nhap(6);
		if(obj == btn7)
			Them_So_Moi_Nhap(7);
		if(obj == btn8)
			Them_So_Moi_Nhap(8);
		if(obj == btn9)
			Them_So_Moi_Nhap(9);
		if(obj == btnCong)
			XuLy("+");
		if(obj == btnTru)
			XuLy("-");
		if(obj == btnNhan)
			XuLy("*");
		if(obj == btnChia)
			XuLy("/");
		if(obj == btnBang)
			processEquals();
		if(obj == btnCongTru)
			Doi_Dau();
		if(obj == btnPhay)
			So_ThapPhan();
		if(obj == btnCan)
			Tinh_Can();
		if(obj == btnPhanTram)
			PhanTram();
		if(obj == btnBack)
			Back();
		if(obj == btnC)
			clearAll();
		if(obj == btnDao)
			NghichDao();
	}

	private void NghichDao() {
		double result = 1 / get_GiaTriNhap();
		set_Text(String.valueOf(result));
	}
	private void clearAll() {
		
		set_Text("0");
		Thao_Tac_Cuoi = "0";
		lastNumber = 0;
		HienThi = DL_BanDau;	
	}
	private void Back() {
		if (HienThi !=BaoLoi){
            set_Text(get_Text().substring(0, get_Text().length() - 1));

            if (get_Text().length() < 1)
                    set_Text("0");
		}
	}
	private void PhanTram() {
		
		double result = 0;
		if (HienThi != BaoLoi){
            try	{
                    result = get_GiaTriNhap() / 100;
                    HienThi_KQ(result);
            }

            catch(Exception ex)	{
                    XuLy_Loi("Sai kiểu dữ liệu");
                    HienThi = BaoLoi;
            }
		}
	}
	private void Tinh_Can() {
		
		double result = 0;
        if (HienThi != BaoLoi)
        {
           try
                {
                    if (get_Text().indexOf("-") == 0)
                        XuLy_Loi("Sai kieu du lieu");

                    result = Math.sqrt(get_GiaTriNhap());
                    HienThi_KQ(result);
                }
                catch(Exception ex)
                {
                        XuLy_Loi("Sai kiểu dữ liệu");
                        HienThi = BaoLoi;
                }
        }	
	}
	private void So_ThapPhan() {
		
		HienThi = DL_BanDau;
		if (XoaSo_KeTiep)
			set_Text("");

		String inputString = get_Text();
	
		//Tao so thap phan
		if (inputString.indexOf(".") < 0)
			set_Text(new String(inputString + "."));
	}
	private void Doi_Dau() {
		
		if (HienThi == DL_BanDau)
		{
			String input = get_Text();

			if (input.length() > 0 && !input.equals("0"))
			{
				if (input.indexOf("-") == 0)
					set_Text(input.substring(1));

				else
					set_Text("-" + input);
			}
			
		}

		else if (HienThi == Ket_Qua)
		{
			double numberInDisplay = get_GiaTriNhap();
		
			if (numberInDisplay != 0)
				HienThi_KQ(-numberInDisplay);
		}	
	}
	private void processEquals() {
		double result = 0;
		if(HienThi != BaoLoi){
			try {
				result = Thuat_Toan();
				HienThi_KQ(result);
			} catch (Exception e) {
				XuLy_Loi("Không Chia Được Cho 0");
			}
			Thao_Tac_Cuoi = "0";
		}	
	}
	private void XuLy_Loi(String ThongBao) {
		set_Text(ThongBao);
		lastNumber = 0;
		HienThi = Ket_Qua;
		XoaSo_KeTiep = true;
		
	}
PHP:
	private void XuLy(String ThuatToan) {
		if(HienThi != BaoLoi){
			
			double numberInDisPlay = get_GiaTriNhap();
			
			if(!Thao_Tac_Cuoi.equals("0")){///------------------Lỗi tại đây
				try {
					double result = Thuat_Toan();
					HienThi_KQ(result);
					lastNumber = result;
					
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
			else
				lastNumber = numberInDisPlay;
			
			XoaSo_KeTiep = true;
			Thao_Tac_Cuoi = ThuatToan;
		}
	}
	private void HienThi_KQ(double result) {
		set_Text(Double.toString(result));
		lastNumber = result;
		HienThi = Ket_Qua;
		XoaSo_KeTiep = true;
	}
	private double Thuat_Toan() throws Exception {
		double result = 0;
		double numbetDisplay = get_GiaTriNhap();
		
		if(Thao_Tac_Cuoi.equals("/")){
			if (numbetDisplay == 0) 
				throw(new Exception());
			result = lastNumber / numbetDisplay;
		}
		if(Thao_Tac_Cuoi.equals("*"))
			result = lastNumber * numbetDisplay;
		if(Thao_Tac_Cuoi.equals("-"))
			result = lastNumber - numbetDisplay;
		if(Thao_Tac_Cuoi.equals("+"))
			result = lastNumber + numbetDisplay;
		return result;
	}
	private double get_GiaTriNhap() {
		String input = txtKq.getText();
		return Double.parseDouble(input);
	}
	private void Them_So_Moi_Nhap(int SoMoi) {
		if(XoaSo_KeTiep)
			set_Text("");
		String input = get_Text();
		if(input.indexOf("0") == 0){
			
			input = input.substring(1);
		}
		if((!input.equals("0")|| SoMoi > 0) && input.length() < DoDai_Max){
			set_Text(input + SoMoi);
		}
		HienThi = DL_BanDau;
		XoaSo_KeTiep = false;
	}
	private String get_Text() {
		return txtKq.getText();
	}
	private void set_Text(String string) {
		txtKq.setText(string);	
	}
}