All pastes #629625 Raw Edit

Miscellany

public java v1 · immutable
·published 2007-07-22 14:19 UTC
rendered paste body
package calc;import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Calculator extends JFrame implements ActionListener{	boolean repeat;	JFrame frame;	JTextArea txt;		String[] namesX= {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};	JButton bt0,bt1,bt2,bt3,bt4,bt5,bt6,bt7,bt8,bt9;	JButton[] btNums = { bt0,bt1,bt2,bt3,bt4,bt5,bt6,bt7,bt8,bt9 };		String[] namesY = {"C","BSpace", "=", "+", "-", "*", "/"};	JButton btC,btBSpace,btEquals,btPlus,btMinus,btUmnoj,btDelen;	JButton[] btOther = { btC,btBSpace,btEquals,btPlus,btMinus,btUmnoj,btDelen };		void doFrame(){		frame = new JFrame("Caclculator");		frame.setDefaultCloseOperation(EXIT_ON_CLOSE);		frame.setSize(150,300);		frame.setLayout(new FlowLayout());				txt = new JTextArea(1,10);		frame.getContentPane().add(txt);		for (int i = 0; i < btNums.length; i++){			btNums[i] = new JButton(namesX[i]);			frame.getContentPane().add(btNums[i]);			btNums[i].addActionListener(this);		}		for (int j = 0; j < btOther.length; j++){			btOther[j] = new JButton(namesY[j]);			frame.getContentPane().add(btOther[j]);			btOther[j].addActionListener(this);		}				doCenter(frame);	}	void doCenter(JFrame frame){		Toolkit tool = Toolkit.getDefaultToolkit();		Dimension temp = tool.getScreenSize();		int hScreen = temp.height;		int wScreen = temp.width;				int hFrame = frame.getHeight();		int wFrame = frame.getWidth();				int h = ( hScreen - hFrame ) / 2;		int w = ( wScreen - wFrame ) / 2;				frame.setLocation(w, h);				frame.setVisible(true);	}	public void actionPerformed(ActionEvent e){		if ( (((JButton)(e.getSource())).getText()).equals("1") ||			 (((JButton)(e.getSource())).getText()).equals("2") ||			 (((JButton)(e.getSource())).getText()).equals("3") ||			 (((JButton)(e.getSource())).getText()).equals("4") ||			 (((JButton)(e.getSource())).getText()).equals("5") ||			 (((JButton)(e.getSource())).getText()).equals("6") ||			 (((JButton)(e.getSource())).getText()).equals("7") ||			 (((JButton)(e.getSource())).getText()).equals("8") ||			 (((JButton)(e.getSource())).getText()).equals("9") ||			 (((JButton)(e.getSource())).getText()).equals("0")  ) {						txt.append(((JButton)(e.getSource())).getText());		} else if ( (((JButton)(e.getSource())).getText()).equals("C")) {			txt.setText("");		} else if ( (((JButton)(e.getSource())).getText()).equals("BSpace")) {			if (txt.getText().equals("")){				txt.setText("");							} else {				String s = txt.getText();				String s1 = s.substring(0, s.length()-1);				txt.setText(s1);			 }		} else if ( (((JButton)(e.getSource())).getText()).equals("+")) {			boolean repeat = false;			String s = txt.getText();			int x = Integer.parseInt(s);			txt.setText("");				if (repeat=false) {					int z = doAct1(x);					this.repeat = true;					if (repeat = true){						int u = doAct2(x);						int res = u + z;						txt.setText(String.valueOf(res));						this.repeat = false;					}				} 									}		}	int doAct1(int x){				return x;	}	int doAct2(int x){		return x;	}			public static void main(String[] args){		Calculator calc = new Calculator();		calc.doFrame();			}}dsadsa