学习java一个月做了个计算器,付代码(望指点)


学习java一个月做了个计算器,付代码(望指点)
由于是新手,所以设计的代码也没有什么设计模式和设计思想的说法了,希望大家多多指教。

import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.Rectangle;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;



public class CalMode extends JFrame{

	private static final long serialVersionUID = 1L;

	private JPanel jPanel = null;

	private JPanel jContentPane = null;

	private JPanel jPanel1 = null;

	private JTextField jTextField = null;

	private JPanel jPanel2 = null;

	private JButton jButton3 = null;

	private JButton jButton6 = null;

	private JButton jButton7 = null;

	private JPanel jPanel21 = null;

	private JButton jButton8 = null;

	private JButton jButton9 = null;

	private JButton jButton10 = null;

	private JButton jButton11 = null;

	private JButton jButton12 = null;

	private JPanel jPanel3 = null;

	private JButton jButton13 = null;

	private JButton jButton14 = null;

	private JButton jButton15 = null;

	private JButton jButton16 = null;

	private JButton jButton17 = null;

	private JPanel jPanel4 = null;

	private JButton jButton19 = null;

	private JButton jButton18 = null;

	private JButton jButton20 = null;

	private JButton jButton22 = null;

	private JButton jButton21 = null;

	private JPanel jPanel5 = null;

	private JButton jButton = null;

	private JButton jButton2 = null;

	private JButton jButton1 = null;

	private JButton jButton24 = null;

	private JButton jButton23 = null;

	/**
	 * This is the default constructor
	 */
	public CalMode() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(309, 286);
		this.setContentPane(getJPanel());
		this.setResizable(false);
		this.setTitle("计算器");
		this.setVisible(true);
	}

	
	private JPanel getJPanel() {
		if (jPanel == null) {
			jPanel = new JPanel();
			jPanel.setLayout(null);
			jPanel.add(getJContentPane(), null);
		}
		return jPanel;
	}

	/**
	 * This method initializes jContentPane	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			GridLayout gridLayout = new GridLayout();
			gridLayout.setRows(6);
			gridLayout.setVgap(6);
			jContentPane = new JPanel();
			jContentPane.setLayout(gridLayout);
			jContentPane.setBounds(new Rectangle(7, 7, 287, 247));
			jContentPane.add(getJPanel1(), null);
			jContentPane.add(getJPanel2(), null);
			jContentPane.add(getJPanel21(), null);
			jContentPane.add(getJPanel3(), null);
			jContentPane.add(getJPanel4(), null);
			jContentPane.add(getJPanel5(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jPanel1	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			GridLayout gridLayout1 = new GridLayout();
			gridLayout1.setRows(1);
			jPanel1 = new JPanel();
			jPanel1.setLayout(gridLayout1);
			jPanel1.add(getJTextField(), null);
		}
		return jPanel1;
	}

	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField() {
		if (jTextField == null) {
			jTextField = new JTextField();
			jTextField.setHorizontalAlignment(JTextField.RIGHT);
			jTextField.setFont(new Font("Dialog", Font.PLAIN, 18));
			jTextField.setText("0");
		}
		return jTextField;
	}

	/**
	 * This method initializes jPanel2	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel2() {
		if (jPanel2 == null) {
			GridLayout gridLayout2 = new GridLayout();
			gridLayout2.setRows(1);
			gridLayout2.setHgap(3);
			jPanel2 = new JPanel();
			jPanel2.setLayout(gridLayout2);
			jPanel2.add(getJButton3(), null);
			jPanel2.add(getJButton6(), null);
			jPanel2.add(getJButton7(), null);
		}
		return jPanel2;
	}

	/**
	 * This method initializes jButton3	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton3() {
		if (jButton3 == null) {
			jButton3 = new JButton();
			jButton3.setText("BACK");
		}
		return jButton3;
	}

	/**
	 * This method initializes jButton6	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton6() {
		if (jButton6 == null) {
			jButton6 = new JButton();
			jButton6.setText("CE");
		}
		return jButton6;
	}

	/**
	 * This method initializes jButton7	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton7() {
		if (jButton7 == null) {
			jButton7 = new JButton();
			jButton7.setText("C");
		}
		return jButton7;
	}

	/**
	 * This method initializes jPanel21	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel21() {
		if (jPanel21 == null) {
			GridLayout gridLayout3 = new GridLayout();
			gridLayout3.setRows(1);
			gridLayout3.setHgap(3);
			jPanel21 = new JPanel();
			jPanel21.setLayout(gridLayout3);
			jPanel21.add(getJButton8(), null);
			jPanel21.add(getJButton9(), null);
			jPanel21.add(getJButton10(), null);
			jPanel21.add(getJButton11(), null);
			jPanel21.add(getJButton12(), null);
		}
		return jPanel21;
	}

	/**
	 * This method initializes jButton8	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton8() {
		if (jButton8 == null) {
			jButton8 = new JButton();
			jButton8.setText("7");
		}
		return jButton8;
	}

	/**
	 * This method initializes jButton9	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton9() {
		if (jButton9 == null) {
			jButton9 = new JButton();
			jButton9.setText("8");
		}
		return jButton9;
	}

	/**
	 * This method initializes jButton10	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton10() {
		if (jButton10 == null) {
			jButton10 = new JButton();
			jButton10.setText("9");
		}
		return jButton10;
	}

	/**
	 * This method initializes jButton11	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton11() {
		if (jButton11 == null) {
			jButton11 = new JButton();
			jButton11.setText("/");
		}
		return jButton11;
	}

	/**
	 * This method initializes jButton12	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton12() {
		if (jButton12 == null) {
			jButton12 = new JButton();
			jButton12.setText("√");
		}
		return jButton12;
	}

	/**
	 * This method initializes jPanel3	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel3() {
		if (jPanel3 == null) {
			GridLayout gridLayout4 = new GridLayout();
			gridLayout4.setRows(1);
			gridLayout4.setHgap(3);
			jPanel3 = new JPanel();
			jPanel3.setLayout(gridLayout4);
			jPanel3.add(getJButton13(), null);
			jPanel3.add(getJButton14(), null);
			jPanel3.add(getJButton15(), null);
			jPanel3.add(getJButton16(), null);
			jPanel3.add(getJButton17(), null);
		}
		return jPanel3;
	}

	/**
	 * This method initializes jButton13	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton13() {
		if (jButton13 == null) {
			jButton13 = new JButton();
			jButton13.setText("4");
		}
		return jButton13;
	}

	/**
	 * This method initializes jButton14	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton14() {
		if (jButton14 == null) {
			jButton14 = new JButton();
			jButton14.setText("5");
		}
		return jButton14;
	}

	/**
	 * This method initializes jButton15	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton15() {
		if (jButton15 == null) {
			jButton15 = new JButton();
			jButton15.setText("6");
		}
		return jButton15;
	}

	/**
	 * This method initializes jButton16	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton16() {
		if (jButton16 == null) {
			jButton16 = new JButton();
			jButton16.setText("*");
		}
		return jButton16;
	}

	/**
	 * This method initializes jButton17	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton17() {
		if (jButton17 == null) {
			jButton17 = new JButton();
			jButton17.setText("x^2");
		}
		return jButton17;
	}

	/**
	 * This method initializes jPanel4	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel4() {
		if (jPanel4 == null) {
			GridLayout gridLayout5 = new GridLayout();
			gridLayout5.setRows(1);
			gridLayout5.setHgap(3);
			jPanel4 = new JPanel();
			jPanel4.setLayout(gridLayout5);
			jPanel4.add(getJButton19(), null);
			jPanel4.add(getJButton18(), null);
			jPanel4.add(getJButton20(), null);
			jPanel4.add(getJButton22(), null);
			jPanel4.add(getJButton21(), null);
		}
		return jPanel4;
	}

	/**
	 * This method initializes jButton19	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton19() {
		if (jButton19 == null) {
			jButton19 = new JButton();
			jButton19.setText("1");
		}
		return jButton19;
	}

	/**
	 * This method initializes jButton18	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton18() {
		if (jButton18 == null) {
			jButton18 = new JButton();
			jButton18.setText("2");
		}
		return jButton18;
	}

	/**
	 * This method initializes jButton20	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton20() {
		if (jButton20 == null) {
			jButton20 = new JButton();
			jButton20.setText("3");
		}
		return jButton20;
	}

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

	/**
	 * This method initializes jButton21	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton21() {
		if (jButton21 == null) {
			jButton21 = new JButton();
			jButton21.setText("1/x");
		}
		return jButton21;
	}

	/**
	 * This method initializes jPanel5	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel5() {
		if (jPanel5 == null) {
			GridLayout gridLayout6 = new GridLayout();
			gridLayout6.setRows(1);
			gridLayout6.setHgap(3);
			jPanel5 = new JPanel();
			jPanel5.setLayout(gridLayout6);
			jPanel5.add(getJButton(), null);
			jPanel5.add(getJButton2(), null);
			jPanel5.add(getJButton1(), null);
			jPanel5.add(getJButton24(), null);
			jPanel5.add(getJButton23(), null);
		}
		return jPanel5;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setText("0");
		}
		return jButton;
	}

	/**
	 * This method initializes jButton2	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton2() {
		if (jButton2 == null) {
			jButton2 = new JButton();
			jButton2.setText("+/-");
		}
		return jButton2;
	}

	/**
	 * This method initializes jButton1	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setText(".");
		}
		return jButton1;
	}

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

	/**
	 * This method initializes jButton23	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton23() {
		if (jButton23 == null) {
			jButton23 = new JButton();
			jButton23.setText("=");
		}
		return jButton23;
	}


	

			












	private boolean start =true;//判断是否为数字的开始
	private double result=0;//保存运算结果
	private String lastCommand="=";  //  @jve:decl-index=0:




	private class InputAction implements ActionListener {//数字的输入

		public void actionPerformed(ActionEvent event) {

			String input = event.getActionCommand(); 

			if (start) {
				getJTextField().setText(""); 
				start = false; 

			}

			if (!input.equals("+/-")) {
				if (input.equals("BACK")) {//退格
					String str = getJTextField().getText(); 
					if (str.length() > 0&&!str.equals("0")) 
						getJTextField().setText(str.substring(0, str.length() - 1)); 
					if(str.length()==1||str.equals("")){
						getJTextField().setText("0");
						start = true;
					}
				} else if (input.equals("CE") || input.equals("C")) {//归零 
					getJTextField().setText("0"); 
					start = true; 
					result = 0;

				} else 	if(input=="."){//小数点问题
					if(getJTextField().getText().indexOf(".")==-1){
						getJTextField().setText(getJTextField().getText() + input);
						if(getJTextField().getText().startsWith(".")){//小数点前没有零的情况
							getJTextField().setText("0" + getJTextField().getText());
						}
					} 

				}else{
					if(!getJTextField().getText().equals("0")){

						getJTextField().setText(getJTextField().getText() + input);

					}else{
						getJTextField().setText(input);
					}
				}


			}else{//数字前加负号的问题处理
				if(!((getJTextField().getText().equals("0")||(getJTextField().getText().equals(""))))){
					if(getJTextField().getText().startsWith("-")){
						getJTextField().setText(getJTextField().getText().substring(1, getJTextField().getText().length())); 

					}else{				 
						getJTextField().setText("-" + getJTextField().getText()); 				
					}
				}else{
					getJTextField().setText("0");
				}
			}


		}
	}





	private class CmdAction implements ActionListener { 
		public void actionPerformed(ActionEvent e) { 
			String command = e.getActionCommand(); 
			if(command.equals("+")|command.equals("-")|command.equals("/")|command.equals("=")|command.equals("*")){
				if (start) { 
					lastCommand = command; 
				} else { 
					calculate(Double.parseDouble(getJTextField().getText()));
					lastCommand = command; 
					start = true; 
			
			}
			}else{
				lastCommand = command; 
				calculate(Double.parseDouble(getJTextField().getText()));
				start = true; 
	
			}
		} 
	} 

	public void calculate(double x) { 
		if (lastCommand.equals("+")) 
			result += x; 
		else if (lastCommand.equals("-")) 
			result -= x; 
		else if (lastCommand.equals("*")) 
			result *= x; 
		else if (lastCommand.equals("/")) 
			if(x!=0){
				result /= x;
			}else{
				getJTextField().setText("分母不能为零!");
				return;
			}
		else if (lastCommand.equals("=")) 
			result = x; 
		else if (lastCommand.equals("√"))
			if(x>=0){
				result = Math.sqrt(x);
			}else{
				getJTextField().setText("被开根数不能为负数!");
				return;
			}
		else if(lastCommand.equals("x^2"))
			result = x*x;
		else if(x!=0){
			result = 1/x;
		}else{
			getJTextField().setText("分母不能为零!");
			return;
		}
		getJTextField().setText("" + result); 
	} 

	




	InputAction input = new InputAction();  //  @jve:decl-index=0:
	CmdAction cmd = new CmdAction();  //  @jve:decl-index=0:

	public void addAction(){           //按钮监听
		this.getJButton8().addActionListener(input);
		this.getJButton9().addActionListener(input);
		this.getJButton10().addActionListener(input);
		this.getJButton13().addActionListener(input);
		this.getJButton14().addActionListener(input);
		this.getJButton15().addActionListener(input);
		this.getJButton18().addActionListener(input);
		this.getJButton19().addActionListener(input);
		this.getJButton20().addActionListener(input);
		this.getJButton().addActionListener(input);
		this.getJButton2().addActionListener(input);
		this.getJButton1().addActionListener(input);
		this.getJButton6().addActionListener(input);
		this.getJButton7().addActionListener(input);
		this.getJButton3().addActionListener(input);
		this.getJButton24().addActionListener(cmd);
		this.getJButton11().addActionListener(cmd);
		this.getJButton16().addActionListener(cmd);
		this.getJButton22().addActionListener(cmd);
		this.getJButton23().addActionListener(cmd);
		this.getJButton12().addActionListener(cmd);
		this.getJButton17().addActionListener(cmd);
		this.getJButton21().addActionListener(cmd);


	}

	
	
	

		public static void main(String[] args) {
			CalMode mod = new CalMode();
			mod.addAction();
			mod.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		
}
}  //  @jve:decl-index=0:visual-constraint="10,10"

你可能感兴趣的:(java,设计模式,windows,swing)