// Calculator 1 // We have a clear idea about intended functionality, // so design the basic layout first. import javax.swing.* ; import java.awt.* ; import java.awt.event.* ; /** * The Calculator as an embedable panel */ public class Calculator extends JPanel implements ActionListener { private JTextField display ; // first row private JButton mc ; private JButton mplus ; private JButton mminus ; private JButton mr ; // second row private JButton clear ; private JButton sign ; private JButton divide ; private JButton times ; // third row private JButton b7 ; private JButton b8 ; private JButton b9 ; private JButton subtract ; // fourth row private JButton b4 ; private JButton b5 ; private JButton b6 ; private JButton add ; // fifth row private JButton b1 ; private JButton b2 ; private JButton b3 ; private JButton result ; // sixth row private JButton b0 ; private JButton dp ; public Calculator(){ display = new JTextField("NOT YET FUNCTIONAL") ; display.setFont(new Font("monospsced",Font.PLAIN,22)) ; display.setForeground(Color.red) ; JPanel keyPad = new JPanel() ; // arrange button/keys here // mc = new JButton("MC") ; mplus = new JButton("M+") ; mminus = new JButton("M-") ; mr = new JButton("MR") ; // clear = new JButton("C") ; sign = new JButton("+/-") ; divide = new JButton("/") ; times = new JButton("*") ; // b7 = new JButton("7") ; b8 = new JButton("8") ; b9 = new JButton("9") ; subtract = new JButton("-") ; // b4 = new JButton("4") ; b5 = new JButton("5") ; b6 = new JButton("6") ; add = new JButton("+") ; // b1 = new JButton("1") ; b2 = new JButton("2") ; b3 = new JButton("3") ; result = new JButton("=") ; // b0 = new JButton("0") ; dp = new JButton(".") ; keyPad.setLayout(new GridLayout(6,4)) ; keyPad.add(mc) ; keyPad.add(mplus) ; keyPad.add(mminus) ; keyPad.add(mr) ; keyPad.add(clear) ; keyPad.add(sign) ; keyPad.add(divide) ; keyPad.add(times) ; keyPad.add(b7) ; keyPad.add(b8) ; keyPad.add(b9) ; keyPad.add(subtract) ; keyPad.add(b4) ; keyPad.add(b5) ; keyPad.add(b6) ; keyPad.add(add) ; keyPad.add(b1) ; keyPad.add(b2) ; keyPad.add(b3) ; keyPad.add(result) ; keyPad.add(b0) ; keyPad.add(dp) ; this.setLayout(new BorderLayout()) ; this.add("North", display) ; this.add("Center", keyPad) ; } public void actionPerformed(ActionEvent ae) { // specify actions here } /** * Run the calculator from the command line * or directly from the jar. */ public static void main(String[] args) { JFrame frame = new JFrame("Calculator") ; frame.setSize(300,375) ; frame.setLocation(500,500) ; frame.getContentPane().add(new Calculator()) ; frame.setVisible(true) ; frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE) ; // exitExam() } }