// CalcView.java - View component // Presentation only. No user actions. No direct interaction with model. // Fred Swartz -- December 2004 // Paolo Bucci -- November 2009 (modified) import java.awt.FlowLayout; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; public class CalcView extends JFrame { //... Components private JTextField m_userInputTf = new JTextField(5); private JTextField m_totalTf = new JTextField(20); private JButton m_multiplyBtn = new JButton("Multiply"); private JButton m_clearBtn = new JButton("Clear"); //======================================================= constructor /** Constructor */ public CalcView() { //... Initialize components m_totalTf.setEditable(false); //... Layout the components. JPanel content = new JPanel(); content.setLayout(new FlowLayout()); content.add(new JLabel("Input")); content.add(m_userInputTf); content.add(m_multiplyBtn); content.add(new JLabel("Total")); content.add(m_totalTf); content.add(m_clearBtn); //... finalize layout this.setContentPane(content); this.pack(); this.setTitle("Simple Calc - MVC"); // The window closing event should probably be passed to the // Controller in a real program, but this is a short example. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public String getUserInput() { return m_userInputTf.getText(); } public void setTotal(String newTotal) { m_totalTf.setText(newTotal); } public void showError(String errMessage) { JOptionPane.showMessageDialog(this, errMessage); } public void addMultiplyListener(ActionListener mal) { m_multiplyBtn.addActionListener(mal); } public void addClearListener(ActionListener cal) { m_clearBtn.addActionListener(cal); } }