@GregBrandon
I took your advice and I made a MVC calculator. I was wondering if you could take a look at my design and if I was on track. I am still working on getting it to actually calculate something. All the buttons respond and print text on the JTextField but it is not calculating, I am sure I will figure it out. I just wanted to share, and if you happen to see what I am doing wrong that would be helpful of course.
model:
package calculator.MVC; import javax.swing.JButton; import javax.swing.JTextField; public class CalculatorModel { private int sum; private int number; private char opt; public CalculatorModel() { sum = 0; number = 0; } public void set(JButton idenifier, JTextField number) { //this will receive '+' '-' '/' etc opt = idenifier.getText().charAt(0); //getting value from JTextField and setting it as a integer this.number = Integer.parseInt(number.getText()); calculate(opt); } public void setNumber(int number) { this.number = number; } public int getSum() { return sum; } private void setSum(int sum) { this.sum = sum; } void calculate(char sign) { switch(sign) { case '+': sum += number; setSum(sum); break; case '-': sum -= number; setSum(sum); break; case '/': sum/= number; setSum(sum); break; case '*': sum *= number; setSum(sum); break; case '=': setSum(sum); break; case 'C': opt = 0; number = 0; sum = 0; setSum(sum); break; default: } } }
view:
package calculator.MVC; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.GridLayout; import javax.swing.*; public class CalculatorView { JButton oneBtn, twoBtn, threeBtn, fourBtn, fiveBtn; JButton sixBtn,sevenBtn, eightBtn, nineBtn, zeroBtn; JButton addBtn, subtractBtn, multiplyBtn, divideBtn; JButton resetBtn, equalsBtn; JTextField textFeild; JFrame frame; JPanel numbersPanel, signsPanel; CalculatorView() { frame = new JFrame("Simple Calculator"); frame.setSize(300,300); frame.setLayout(new BorderLayout()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); textFeild = new JTextField(10); JPanel numbersPanel = new JPanel(); JPanel signsPanel = new JPanel(); numbersPanel = numberPan(numbersPanel); signsPanel = signPan(signsPanel); frame.add(textFeild,BorderLayout.PAGE_START); frame.add(numbersPanel,BorderLayout.CENTER); frame.add(signsPanel,BorderLayout.LINE_END); frame.setResizable(false); frame.setVisible(true); } //returns a matrix of buttons to represent the numbers //on the calculator JPanel numberPan(JPanel panel) { oneBtn = new JButton("1"); twoBtn = new JButton("2"); threeBtn = new JButton("3"); fourBtn= new JButton("4"); fiveBtn= new JButton("5"); sixBtn= new JButton("6"); sevenBtn= new JButton("7"); eightBtn= new JButton("8"); nineBtn= new JButton("9"); zeroBtn= new JButton("0"); panel.add(sevenBtn); panel.add(eightBtn); panel.add(nineBtn); panel.add(fourBtn); panel.add(fiveBtn); panel.add(sixBtn); panel.add(oneBtn); panel.add(twoBtn); panel.add(threeBtn); panel.add(zeroBtn); panel.setLayout(new GridLayout(3,4)); return panel; } //return a horizontal pattern of buttons to represent //the mathematical signs of the calculator JPanel signPan(JPanel panel) { addBtn= new JButton("+"); addBtn.setPreferredSize(new Dimension(10,10)); subtractBtn= new JButton("-"); multiplyBtn= new JButton("*"); divideBtn= new JButton("/"); resetBtn= new JButton("C"); equalsBtn= new JButton("="); panel.add(resetBtn); panel.add(addBtn); panel.add(subtractBtn); panel.add(multiplyBtn); panel.add(divideBtn); panel.add(equalsBtn); panel.setLayout(new GridLayout(3,3)); return panel; } void setTextValue(int value) { //sets textField to String value textFeild.setText(Integer.toString(value)); } //returns number currently in TextBox int getTextFeildValue() { int value = Integer.parseInt(textFeild.getText()); return value; } }
controller:
main:package calculator.MVC; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class CalculatorController { CalculatorModel model; CalculatorView view; CalculatorController(CalculatorModel model,CalculatorView view) { this.model = model; this.view = view; //setting action listeners for each button in the view class view.addBtn.addActionListener(new Actions()); view.subtractBtn.addActionListener(new Actions()); view.multiplyBtn.addActionListener(new Actions()); view.divideBtn.addActionListener(new Actions()); view.equalsBtn.addActionListener(new Actions()); view.resetBtn.addActionListener(new Actions()); view.oneBtn.addActionListener(new Actions()); view.twoBtn.addActionListener(new Actions()); view.threeBtn.addActionListener(new Actions()); view.fourBtn.addActionListener(new Actions()); view.fiveBtn.addActionListener(new Actions()); view.sixBtn.addActionListener(new Actions()); view.sevenBtn.addActionListener(new Actions()); view.eightBtn.addActionListener(new Actions()); view.nineBtn.addActionListener(new Actions()); view.zeroBtn.addActionListener(new Actions()); } //inner class that will handle action listeners class Actions implements ActionListener { @Override public void actionPerformed(ActionEvent e) { try{ if(e.getSource() == view.addBtn) { //sends button and textField to model class //to handle calculations model.set(view.addBtn, view.textFeild); //sets JTextFeild the sum from model.set view.setTextValue(model.getSum()); } else if(e.getSource() == view.subtractBtn) { model.set(view.subtractBtn, view.textFeild); view.setTextValue(model.getSum()); } else if(e.getSource() == view.divideBtn) { model.set(view.divideBtn, view.textFeild); view.setTextValue(model.getSum()); } else if(e.getSource() == view.multiplyBtn) { model.set(view.multiplyBtn, view.textFeild); view.setTextValue(model.getSum()); } else if(e.getSource() == view.equalsBtn) { model.set(view.equalsBtn, view.textFeild); view.setTextValue(model.getSum()); } else if(e.getSource() == view.resetBtn) { model.set(view.resetBtn, view.textFeild); view.setTextValue(model.getSum()); } //now handing number buttons else if(e.getSource() == view.oneBtn) { model.setNumber(1); view.setTextValue(1); } else if(e.getSource() == view.twoBtn) { model.setNumber(2); view.setTextValue(2); } else if(e.getSource() == view.threeBtn) { model.setNumber(3); view.setTextValue(3); } else if(e.getSource() == view.fourBtn) { model.setNumber(4); view.setTextValue(4); } else if(e.getSource() == view.fiveBtn) { model.setNumber(5); view.setTextValue(5); } else if(e.getSource() == view.sixBtn) { model.setNumber(6); view.setTextValue(6); } else if(e.getSource() == view.sevenBtn) { model.setNumber(7); view.setTextValue(7); } else if(e.getSource() == view.eightBtn) { model.setNumber(8); view.setTextValue(8); } else if(e.getSource() == view.nineBtn) { model.setNumber(9); view.setTextValue(9); } }catch(Exception ex) { System.out.println("There was an ERROR! "+ex); } } } }
package calculator.MVC; public class CalculatorMain { public static void main(String[] args) { CalculatorModel model = new CalculatorModel(); CalculatorView view = new CalculatorView(); new CalculatorController(model,view); } }