Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. package com.company;// CalcView.java - View component
  2. // Presentation only. No user actions.
  3. // Fred Swartz -- December 2004
  4.  
  5. import java.awt.*;
  6. import javax.swing.*;
  7. import java.awt.event.*;
  8.  
  9. class CalcView extends JFrame {
  10. //... Constants
  11. private static final String INITIAL_VALUE = "0";
  12.  
  13. //... Components
  14. private JTextField m_userInputTf = new JTextField(5);
  15. private JTextField m_totalTf = new JTextField(20);
  16. private JButton m_multiplyBtn = new JButton("Multiply");
  17. private JButton m_clearBtn = new JButton("Clear");
  18. private JButton m_addBtn = new JButton(("Add"));
  19.  
  20. private CalcModel m_model;
  21.  
  22. //======================================================= constructor
  23. /** Constructor */
  24. CalcView(CalcModel model) {
  25. //... Set up the logic
  26. m_model = model;
  27. m_model.setValue(INITIAL_VALUE);
  28.  
  29. //... Initialize components
  30. m_totalTf.setText(m_model.getValue());
  31. m_totalTf.setEditable(false);
  32.  
  33. //... Layout the components.
  34. JPanel content = new JPanel();
  35. JPanel buttons = new JPanel();
  36. content.setLayout(new FlowLayout());
  37. content.add(new JLabel("Input"));
  38. content.add(m_userInputTf);
  39. buttons.setLayout(new BoxLayout(buttons,BoxLayout.Y_AXIS));
  40. buttons.add(m_multiplyBtn);
  41. buttons.add(m_addBtn);
  42. content.add(buttons);
  43. content.setLayout(new FlowLayout());
  44. content.add(new JLabel("Total"));
  45. content.add(m_totalTf);
  46. content.add(m_clearBtn);
  47.  
  48. //... finalize layout
  49. this.setContentPane(content);
  50. this.pack();
  51.  
  52. this.setTitle("Simple Calc - MVC");
  53. // The window closing event should probably be passed to the
  54. // Controller in a real program, but this is a short example.
  55. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  56. }
  57.  
  58. void reset() {
  59. m_totalTf.setText(INITIAL_VALUE);
  60. }
  61.  
  62. String getUserInput() {
  63. return m_userInputTf.getText();
  64. }
  65.  
  66. void setTotal(String newTotal) {
  67. m_totalTf.setText(newTotal);
  68. }
  69.  
  70. void showError(String errMessage) {
  71. JOptionPane.showMessageDialog(this, errMessage);
  72. }
  73.  
  74. void addMultiplyListener(ActionListener mal) {
  75. m_multiplyBtn.addActionListener(mal);
  76. }
  77.  
  78. void addAddListener(ActionListener mal) {
  79. m_addBtn.addActionListener(mal);
  80. }
  81.  
  82. void addClearListener(ActionListener cal) {
  83. m_clearBtn.addActionListener(cal);
  84. }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement