Create a java based GUI calculator as shown below ...
Implement only the following functionalities ...
Implement only the following functionalities ...
Solution:
/*Java GUI based calculator. * Written by Shaeed Khan * Date-22 Oct 2013 * Version- 1.0 * */ import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.Font; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.SwingUtilities; public class Calculator { public static void main(String[] args){ final CalculatorShaeed cal = new CalculatorShaeed(); SwingUtilities.invokeLater(new Runnable() { public void run() { //Calling the gui thread cal.makeGUI(); } }); }//End main } //Calculator class class CalculatorShaeed extends JFrame{ //Variables double x,y,result; boolean first; boolean second,ch; char action; JTextField screen; //Constructor public CalculatorShaeed() { super("Simple Calculator - SHAEED"); x=0; y=0; result=0; first=true; second=true; ch=false; action='A'; } //Method for GUI public void makeGUI(){ //Configuring the GUI setLayout(new FlowLayout()); setVisible(true); setSize(320,220); setDefaultCloseOperation(EXIT_ON_CLOSE); setResizable(false); //Buttons JButton b0 = new JButton("0"); JButton b1 = new JButton("1"); JButton b2 = new JButton("2"); JButton b3 = new JButton("3"); JButton b4 = new JButton("4"); JButton b5 = new JButton("5"); JButton b6 = new JButton("6"); JButton b7 = new JButton("7"); JButton b8 = new JButton("8"); JButton b9 = new JButton("9"); JButton bMul = new JButton("*"); JButton bDiv = new JButton("/"); JButton bPlus = new JButton("+"); JButton bMinus = new JButton("-"); JButton bPoint = new JButton("."); JButton bEqual = new JButton("="); JButton bPM = new JButton("+/-"); JButton bKSP = new JButton("BKSP"); JButton bCE = new JButton("CE"); JButton bCLR = new JButton("CLR"); //Screen screen = new JTextField(10); screen.setText("Shaeed-Cal"); screen.setHorizontalAlignment(JTextField.RIGHT); screen.setFont(new Font("Arial", Font.PLAIN, 28)); JLabel scr = new JLabel(); scr.add(screen); //Adding the keys to Keypad JPanel key = new JPanel(); key.setLayout(new GridLayout(5,4)); key.add(bKSP); key.add(bCE); key.add(bCLR); key.add(bPM); key.add(b7); key.add(b8); key.add(b9); key.add(bDiv); key.add(b4); key.add(b5); key.add(b6); key.add(bMul); key.add(b1); key.add(b2); key.add(b3); key.add(bMinus); key.add(b0); key.add(bPoint); key.add(bEqual); key.add(bPlus); //Adding to the frame scr.setVisible(true); key.setVisible(true); add(screen,BorderLayout.NORTH); add(key,BorderLayout.CENTER); //Screen handler screen.addKeyListener(new KeyListener() { public void keyTyped(KeyEvent event) { event.consume(); } public void keyPressed(KeyEvent arg0) { } public void keyReleased(KeyEvent arg0) { } });//End Screen handler //Keypad handlers //0 key b0.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { if(first){ screen.setText(null); first=false; screen.setText(screen.getText()+"0"); } else screen.setText(screen.getText()+"0"); } });//End 0 key //1 key b1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { if(first){ screen.setText(null); first=false; screen.setText(screen.getText()+"1"); } else screen.setText(screen.getText()+"1"); } });//End 1 key //2 key b2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { if(first){ screen.setText(null); first=false; screen.setText(screen.getText()+"2"); } else screen.setText(screen.getText()+"2"); } });//End 2 key //3 key b3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { if(first){ screen.setText(null); first=false; screen.setText(screen.getText()+"3"); } else screen.setText(screen.getText()+"3"); } });//End 3 key //4 key b4.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { if(first){ screen.setText(null); first=false; screen.setText(screen.getText()+"4"); } else screen.setText(screen.getText()+"4"); } });//End 4 key //5 key b5.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { if(first){ screen.setText(null); first=false; screen.setText(screen.getText()+"5"); } else screen.setText(screen.getText()+"5"); } });//End 5 key //6 key b6.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { if(first){ screen.setText(null); first=false; screen.setText(screen.getText()+"6"); } else screen.setText(screen.getText()+"6"); } });//End 6 key //7 key b7.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { if(first){ screen.setText(null); first=false; screen.setText(screen.getText()+"7"); } else screen.setText(screen.getText()+"7"); } });//End 7 key //8 key b8.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { if(first){ screen.setText(null); first=false; screen.setText(screen.getText()+"8"); } else screen.setText(screen.getText()+"8"); } });//End 8 key //9 key b9.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { if(first){ screen.setText(null); first=false; screen.setText(screen.getText()+"9"); } else screen.setText(screen.getText()+"9"); } });//End 9 key // . key bPoint.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { if ((screen.getText().contains("."))==false) { if (first) { screen.setText(null); first = false; screen.setText(screen.getText() + "0."); } else if (screen.getText().isEmpty()) { screen.setText("0."); } else { screen.setText(screen.getText() + "."); } } } });//End . key //Plus key bPlus.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { first=true; if(second==false) calculate(); second=false; action='+'; x=Double.parseDouble(screen.getText()); } });//End plus key //minus key bMinus.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { first=true; if(second==false) calculate(); second=false; action='-'; x=Double.parseDouble(screen.getText()); } });//End minus key //Mul key bMul.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { first=true; if(second==false) calculate(); second=false; action='*'; x=Double.parseDouble(screen.getText()); } });//End mul key //Division key bDiv.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { first=true; if(second==false) calculate(); second=false; action='/'; x=Double.parseDouble(screen.getText()); } });//End division key //+/- key bPM.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { if ((screen.getText().contains("-"))==false && (screen.getText().contains("+"))==false) { if (first) { screen.setText(null); first = false; screen.setText("-"+screen.getText()); } else if (screen.getText().isEmpty()) { screen.setText("-"); } else { screen.setText("-"+screen.getText()); } } else if ((screen.getText().contains("+"))==true){ String temp=screen.getText().replace('+', '-'); screen.setText(temp); } else { String temp=screen.getText().replace('-', '+'); screen.setText(temp); } } });//End +/- key //Equal key bEqual.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { calculate(); } });//End equal key //Backspace key bKSP.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { String s1,s2; s2=""; s1=screen.getText(); for(int i=0;i<s1.length()-1;i++){ s2+=s1.charAt(i); } screen.setText(null); screen.setText(s2); } });//End backspace key //CLR key bCLR.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { screen.setText(null); } });//End CLR key //CE key bCE.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { x=0; y=0; result=0; first=true; second=true; ch=false; action='A'; screen.setText(null); screen.setText("Shaeed-Cal"); } });//End CE key } //Calculating answers void calculate(){ first=true; if(second==true && ch==true){ } else y=Double.parseDouble(screen.getText()); switch (action) { case '+': result = x + y; break; case '-': result = x - y; break; case '*': result = x * y; break; case '/': result = x / y; break; default: result=y; break; } screen.setText(String.valueOf((float)result)); if(second==false) ch=true; second=true; x=Double.parseDouble(screen.getText()); } }
No comments:
Post a Comment