import javax.swing.*; import java.awt.event.*; import java.awt.Font; import java.awt.Color; import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.FlowLayout; public class Calculator extends javax.swing.JPanel implements ActionListener { private CalculatorController controller = null; private JLabel display = null; static final String EMPTYSTR = " "; static final String L_CE = "CE"; static final String C_CE = "-1"; static final int I_CE = -1; static final String L_C = "C"; static final String C_C = "-2"; static final int I_C = -2; static final String L_SIGN = "+/-"; static final String C_SIGN = "-3"; static final int I_SIGN = -3; static final String L_POINT = "."; static final String C_POINT = "-4"; static final int I_POINT = -4; static final String L_PUSH = "PUSH"; static final String C_PUSH = "-5"; static final int I_PUSH = -5; static final String L_POP = "POP"; static final String C_POP = "-6"; static final int I_POP = -6; static final String L_DIV = "/"; static final String C_DIV = "-7"; static final int I_DIV = -7; static final String L_MUL = "*"; static final String C_MUL = "-8"; static final int I_MUL = -8; static final String L_SUB = "-"; static final String C_SUB = "-9"; static final int I_SUB = -9; static final String L_ADD = "+"; static final String C_ADD = "-10"; static final int I_ADD = -10; static final Font defaultKeyFont = new Font( "SansSerif", Font.BOLD, 18 ); static final Font defaultDisplayFont = new Font( "MonoSpaced", Font.BOLD, 18 ); public static boolean isDigit( int eT ) { return( eT >= 0 && eT <= 9 ); } public static boolean isOp( int eT ) { return( eT <= I_PUSH && eT >= I_ADD ); } class Keypad extends JPanel { Keypad() { setLayout( new GridLayout( 0, 5, 2, 2 )); setBackground( Color.blue ); setBorder( BorderFactory.createLineBorder( Color.blue, 2 ) ); setFont( defaultKeyFont ); add( new KeyButton( L_PUSH, C_PUSH, Color.green, Calculator.this )); add( new KeyButton( "7", Color.black, Calculator.this )); add( new KeyButton( "8", Color.black, Calculator.this )); add( new KeyButton( "9", Color.black, Calculator.this )); add( new KeyButton( L_DIV, C_DIV, Color.red, Calculator.this )); add( new KeyButton( L_POP, C_POP, Color.green, Calculator.this )); add( new KeyButton( "4", Color.black, Calculator.this )); add( new KeyButton( "5", Color.black, Calculator.this )); add( new KeyButton( "6", Color.black, Calculator.this )); add( new KeyButton( L_MUL, C_MUL, Color.red, Calculator.this )); add( new KeyButton( L_CE, C_CE, Color.green, Calculator.this )); add( new KeyButton( "1", Color.black, Calculator.this )); add( new KeyButton( "2", Color.black, Calculator.this )); add( new KeyButton( "3", Color.black, Calculator.this )); add( new KeyButton( L_SUB, C_SUB, Color.red, Calculator.this )); add( new KeyButton( L_C, C_C, Color.green, Calculator.this )); add( new KeyButton( "0", Color.black, Calculator.this )); add( new KeyButton( L_SIGN, C_SIGN, Color.black, Calculator.this )); add( new KeyButton( L_POINT, C_POINT, Color.black, Calculator.this )); add( new KeyButton( L_ADD, C_ADD, Color.red, Calculator.this )); } } class KeyButton extends JButton { KeyButton( String l, ActionListener a ) { super( l ); setBackground( Color.lightGray ); addActionListener( a ); } KeyButton( String l, Color f, ActionListener a ) { this( l, a ); setForeground( f ); } KeyButton( String l, String c, Color f, ActionListener a ) { this( l, a ); setForeground( f ); setActionCommand( c ); } } public Calculator() { setBackground( Color.blue ); setLayout( new BorderLayout(4, 4) ); setBorder( BorderFactory.createLineBorder( Color.blue, 2 ) ); JPanel displayPanel = new JPanel(); displayPanel.setBorder( BorderFactory.createLineBorder( Color.black, 2 ) ); displayPanel.setBackground( Color.white ); displayPanel.setLayout( new FlowLayout( FlowLayout.RIGHT )); display = new JLabel( EMPTYSTR ); display.setFont( defaultDisplayFont ); display.setForeground( Color.black ); display.setHorizontalAlignment( SwingConstants.RIGHT ); displayPanel.add( display ); add( displayPanel, BorderLayout.NORTH ); add( new Keypad(), BorderLayout.CENTER ); } public void setController( CalculatorController cc ) { controller = cc; } public CalculatorController getController() { return controller; } public void setDisplay( String s ) { display.setText( s ); } public void clearDisplay() { display.setText( EMPTYSTR ); } public void actionPerformed( ActionEvent e ) { if ( controller != null ) controller.actionPerformed( e ); } }