// Calculator 3 // Add some functionality import javax.swing.* ; import java.awt.* ; import java.awt.event.* ; // import javax.swing.plaf.metal.* ; /** * The Calculator 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(int fontsize){ display = new JTextField("not fully functional") ; display.setHorizontalAlignment(JTextField.RIGHT) ; display.setFont(new Font("monospsced",Font.PLAIN,2*fontsize)) ; 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") ; char[] sign_change_unicode = {'\u2213'} ; sign = new JButton(new String(sign_change_unicode)) ; char[] divide_unicode = {'\u00F7'} ; //char[] divide_unicode = {'\u2204'} ; divide = new JButton(new String(divide_unicode)) ; times = new JButton("x") ; // 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(".") ; // Add action listener to buttons and set action commands b0.addActionListener(this) ; b0.setActionCommand("0") ; b1.addActionListener(this) ; b1.setActionCommand("1") ; clear.addActionListener(this) ; clear.setActionCommand("clear") ; dp.addActionListener(this) ; dp.setActionCommand("dp") ; /* // another way but a little obscure b1.setAction(new AbstractAction("1") { public void actionPerformed(ActionEvent a) { display.setText(display.getText()+"1") ; } }) ; // or b1 = new JButton(new AbstractAction("1") { ...}) ; // Ok when not many buttons. */ // set kepPad font Font font = new Font("sansserif",Font.PLAIN,fontsize) ; mc.setFont(font) ; mplus.setFont(font) ; mminus.setFont(font) ; mr.setFont(font) ; clear.setFont(font) ; sign.setFont(font) ; divide.setFont(font) ; times.setFont(font) ; b7.setFont(font) ; b8.setFont(font) ; b9.setFont(font) ; subtract.setFont(font) ; b4.setFont(font) ; b5.setFont(font) ; b6.setFont(font) ; add.setFont(font) ; mplus.setFont(font) ; b1.setFont(font) ; b2.setFont(font) ; b3.setFont(font) ; result.setFont(font) ; b0.setFont(font) ; dp.setFont(font) ; 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) ; System.out.println(mc.getFont().getSize()) ; } public void actionPerformed(ActionEvent ae) { String s = ae.getActionCommand() ; // specify actions here if (s.equals("0")) display.setText(display.getText()+s) ; else if (s.equals("1")) display.setText(display.getText()+s) ; else if (s.equals("clear")) display.setText("") ; else if (s.equals("dp")) display.setText(display.getText()+".") ; } /** * Run the calculator from command line. */ public static void main(String[] args) { String laf = UIManager.getSystemLookAndFeelClassName() ; System.out.println(laf) ; try { UIManager.setLookAndFeel(laf); //UIManager.setLookAndFeel(new MetalLookAndFeel()); } catch(Exception x) {} JFrame frame = new JFrame("Calculator") ; frame.setSize(275,350) ; frame.setLocation(500,500) ; frame.getContentPane().add(new Calculator(16)) ; frame.setVisible(true) ; frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE) ; } }