CalculatorModel.java
05 | public class CalculatorModel { |
10 | private int calculationValue; |
12 | public void addTwoNumbers( int firstNumber, int secondNumber){ |
14 | calculationValue = firstNumber + secondNumber; |
18 | public int getCalculationValue(){ |
20 | return calculationValue; |
CalculatorView.java
07 | import java.awt.event.ActionListener; |
11 | public class CalculatorView extends JFrame{ |
13 | private JTextField firstNumber = new JTextField( 10 ); |
14 | private JLabel additionLabel = new JLabel( "+" ); |
15 | private JTextField secondNumber = new JTextField( 10 ); |
16 | private JButton calculateButton = new JButton( "Calculate" ); |
17 | private JTextField calcSolution = new JTextField( 10 ); |
23 | JPanel calcPanel = new JPanel(); |
25 | this .setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
26 | this .setSize( 600 , 200 ); |
28 | calcPanel.add(firstNumber); |
29 | calcPanel.add(additionLabel); |
30 | calcPanel.add(secondNumber); |
31 | calcPanel.add(calculateButton); |
32 | calcPanel.add(calcSolution); |
40 | public int getFirstNumber(){ |
42 | return Integer.parseInt(firstNumber.getText()); |
46 | public int getSecondNumber(){ |
48 | return Integer.parseInt(secondNumber.getText()); |
52 | public int getCalcSolution(){ |
54 | return Integer.parseInt(calcSolution.getText()); |
58 | public void setCalcSolution( int solution){ |
60 | calcSolution.setText(Integer.toString(solution)); |
67 | void addCalculateListener(ActionListener listenForCalcButton){ |
69 | calculateButton.addActionListener(listenForCalcButton); |
75 | void displayErrorMessage(String errorMessage){ |
77 | JOptionPane.showMessageDialog( this , errorMessage); |
CalculatorController.java
01 | import java.awt.event.ActionEvent; |
02 | import java.awt.event.ActionListener; |
07 | public class CalculatorController { |
09 | private CalculatorView theView; |
10 | private CalculatorModel theModel; |
12 | public CalculatorController(CalculatorView theView, CalculatorModel theModel) { |
13 | this .theView = theView; |
14 | this .theModel = theModel; |
20 | this .theView.addCalculateListener( new CalculateListener()); |
23 | class CalculateListener implements ActionListener{ |
25 | public void actionPerformed(ActionEvent e) { |
27 | int firstNumber, secondNumber = 0 ; |
35 | firstNumber = theView.getFirstNumber(); |
36 | secondNumber = theView.getSecondNumber(); |
38 | theModel.addTwoNumbers(firstNumber, secondNumber); |
40 | theView.setCalcSolution(theModel.getCalculationValue()); |
44 | catch (NumberFormatException ex){ |
46 | System.out.println(ex); |
48 | theView.displayErrorMessage( "You Need to Enter 2 Integers" ); |
MVCCalculator.java
01 | public class MVCCalculator { |
03 | public static void main(String[] args) { |
05 | CalculatorView theView = new CalculatorView(); |
07 | CalculatorModel theModel = new CalculatorModel(); |
09 | CalculatorController theController = new CalculatorController(theView,theModel); |
11 | theView.setVisible( true ); |
- See more at: http://www.newthinktank.com/2013/02/mvc-java-tutorial/#sthash.uSKCsfhx.dpuf
No comments:
Post a Comment