This will not compile . Only aids in understanding
// Follow some coding standards and naming conventions
//
// File description and notes.
AWT imports
// import java.awt.*;
Swing imports
// import javax.swing.*;
public class
{
// public variables
... like ...
final int MAX_INPUT_LENGTH = 20;
final int INPUT_MODE = 0;
final int RESULT_MODE = 1;
final int ERROR_MODE = 2;
int displayMode;
... ...
// public Component related declarations.
private JMenu jmenuFile, jmenuHelp;
private JMenuItem jmenuitemExit, jmenuitemAbout;
...like...
// Constructor
public
{
// initialize the Java components
... like ...
jmenuFile = new JMenu("File");
jmenuFile.setFont(f121);
jmenuFile.setMnemonic(KeyEvent.VK_F);
...
jlbOutput = new JLabel("0");
jlbOutput.setHorizontalTextPosition(JLabel.RIGHT);
jlbOutput.setBackground(Color.WHITE);
jlbOutput.setOpaque(true);
...
// Add components to frame
getContentPane().add(jlbOutput, BorderLayout.NORTH);
jbnButtons = new JButton[23];
GridLayout(int rows, int cols, int hgap, int vgap)
JPanel jplButtons = new JPanel(); // container for Jbuttons
// Looped creation of widgets
// Create numeric Jbuttons
for (int i=0; i<=9; i++)
{
// set each Jbutton label to the value of index
jbnButtons[i] = new JButton(String.valueOf(i));
}
// One after another widget creation
// Create operator Jbuttons
jbnButtons[10] = new JButton("+/-");
jbnButtons[11] = new JButton(".");
jbnButtons[12] = new JButton("=");
jbnButtons[13] = new JButton("/");
jbnButtons[14] = new JButton("*");
...
// setting layout stuff
jplBackSpace.setLayout(new GridLayout(1, 1, 2, 2));
jbnButtons[20] = new JButton("Backspace");
jplBackSpace.add(jbnButtons[20]);
jplControl = new JPanel();
jplControl.setLayout(new GridLayout(1, 2, 2 ,2));
// KEY STATEMENT
// Add components to frame
getContentPane().add(jplMaster, BorderLayout.SOUTH);
requestFocus();
// ADDING ACTION LISTENERS
//activate ActionListener
for (int i=0; i
jbnButtons[i].addActionListener(this);
}
jmenuitemAbout.addActionListener(this);
jmenuitemExit.addActionListener(this);
clearAll();
//add WindowListener for closing frame and ending program
addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e)
{
System.exit(0);
}
}
);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} //End of Contructor Calculator
// Perform action
public void actionPerformed(ActionEvent e){
double result = 0;
if(e.getSource() == jmenuitemAbout){
JDialog dlgAbout = new CustomABOUTDialog(this, "About Java Swing Calculator", true);
dlgAbout.setVisible(true);
}else if(e.getSource() == jmenuitemExit){
System.exit(0);
}
// Search for the button pressed until end of array or key found
for (int i=0; i
{
if(e.getSource() == jbnButtons[i])
{
switch(i)
{
case 0:
addDigitToDisplay(i);
break;
case 1:
addDigitToDisplay(i);
break;
// Logic goes here ....
case 16: // +
processOperator("+");
break;
case 17: // sqrt
if (displayMode != ERROR_MODE)
{
try
{
if (getDisplayString().indexOf("-") == 0)
displayError("Invalid input for function!");
case 22: // C
clearAll();
break;
}
}
}
}
// Setters and method implementations.
// Error handling routines.
} // End of class