193 lines
5.1 KiB
Java
193 lines
5.1 KiB
Java
/**
|
|
* <PRE>
|
|
* CalculatorGUI.java
|
|
*
|
|
* Revisions: 1.1 Mar. 14, 2001
|
|
* Added minor modifications to calculation behavior. Added
|
|
* some documentation.
|
|
* 1.0 Jan. 11, 2001
|
|
* Created the CalculatorGUI class
|
|
*
|
|
* </PRE>
|
|
*
|
|
* @author <A HREF="mailto:demox@cc.gatech.edu">Luke A. Olbrish</A>
|
|
* @version Version 1.0, Jan. 11, 2001
|
|
*/
|
|
|
|
// I must import java libraries in order to work with swing.
|
|
import java.awt.*;
|
|
import java.awt.event.*;
|
|
import javax.swing.*;
|
|
|
|
public class CalculatorGUI
|
|
{
|
|
/**
|
|
* The main frame of the program. This will contain all of the other
|
|
* gui components.
|
|
*/
|
|
private JFrame guiFrame = null;
|
|
|
|
/**
|
|
* This panel will be the only component directy added to the frame.
|
|
* Instead, this panel will be where all other gui components will be added.
|
|
*/
|
|
private Container contentPane = null;
|
|
|
|
/**
|
|
* This label will be used as the display for the numbers of the calculator.
|
|
* The label will be placed in the top of the contentPane.
|
|
*/
|
|
private JLabel numberLabel = null;
|
|
|
|
/**
|
|
* This panel is where the operator and digit buttons will be placed. It
|
|
* will be the main content of the contentPane.
|
|
*/
|
|
private JPanel buttonPanel = null;
|
|
|
|
/**
|
|
* This panel will contain the clear and equal buttons. It will be in the
|
|
* lower section of the content pane.
|
|
*/
|
|
private JPanel operationPanel = null;
|
|
|
|
/**
|
|
* This array, which will be of size 10. It will hold 10 digit buttons.
|
|
*/
|
|
private JButton[] numberedButtons = null;
|
|
|
|
/**
|
|
* This array, which will be of size 6. It will hold the operations.
|
|
*/
|
|
private JButton[] operationButtons = null;
|
|
|
|
/**
|
|
* This is the control for the program. It will handle all events that
|
|
* occur in this gui.
|
|
*/
|
|
private EventHandler event = null;
|
|
|
|
// Constructors
|
|
|
|
public CalculatorGUI()
|
|
{
|
|
doMakeObjects();
|
|
doAddObjects();
|
|
doCustomizations();
|
|
doEventHandling();
|
|
|
|
// This sets the size of the screen based on the preferred size of the
|
|
// GUI components.
|
|
guiFrame.pack();
|
|
guiFrame.setVisible( true );
|
|
}// end of constructor()
|
|
|
|
// Constructor Helpers
|
|
|
|
/**
|
|
* This function will create all of the components that are needed for the
|
|
* gui. It should initialize the guiFrame, contentPane, buttonPanel,
|
|
* operationPanel, numberLabel, numberedButtons, and operationButtons.
|
|
* You should also set your layouts here.
|
|
*/
|
|
public void doMakeObjects()
|
|
{
|
|
// ### Add your code here. ####
|
|
guiFrame.getContentPane().setLayout(new BorderLayout());
|
|
|
|
// ### Done ###
|
|
}// end of doMakeObjects()
|
|
|
|
/**
|
|
* This function is where you should add the components. You should add
|
|
* the numberedButtons and +, -, *, / to buttonPanel. Clr and = should
|
|
* be added to operationPanel. numberLabel should be added to the top of
|
|
* the contentPane, buttonPanel to the center, and operationPanel to the
|
|
* bottom.
|
|
*/
|
|
public void doAddObjects()
|
|
{
|
|
guiFrame.setContentPane( contentPane );
|
|
|
|
// ### Add your code here. ####
|
|
|
|
// ### Done ###
|
|
}// end of doAddObjects()
|
|
|
|
/**
|
|
* This is where you should set the background colors for the buttons.
|
|
* You should also increase the label's font size and prevent all the
|
|
* buttons from having their focus be painted.
|
|
*/
|
|
public void doCustomizations()
|
|
{
|
|
// ### Add your code here. ####
|
|
|
|
// ### Done ###
|
|
}// end of doCustomizations()
|
|
|
|
public void doEventHandling()
|
|
{
|
|
// Add the event handler that will monitor events from this GUI.
|
|
event = new EventHandler( this );
|
|
|
|
// Make event respond to button presses on the numbers from the GUI.
|
|
for( int x = 0; x < 10; x++ )
|
|
numberedButtons[x].addActionListener( event );
|
|
|
|
// Make event respond to button presses on the other buttons from the
|
|
// GUI.
|
|
for( int x = 0; x < 6; x++ )
|
|
operationButtons[x].addActionListener( event );
|
|
|
|
// This is a specialized way of creating a class that will handle
|
|
// window frame events. More on this in the next lab...
|
|
guiFrame.addWindowListener( new WindowAdapter()
|
|
{
|
|
public void windowClosing( WindowEvent we )
|
|
{
|
|
System.exit( 0 );
|
|
}// end of windowClosing( WindowEvent )
|
|
});// end of WindowAdapter inner class
|
|
}// end fo doEventHandling()
|
|
|
|
// Accessors
|
|
|
|
/**
|
|
* This function should return the text from the numberLabel.
|
|
*
|
|
* @return the contents of numberLabel.
|
|
*/
|
|
public String getDisplay()
|
|
{
|
|
// ### Add your code here. ####
|
|
return numberLabel.getText();
|
|
// ### Done ###
|
|
}// end of getDisplay()
|
|
|
|
// Modifiers
|
|
|
|
/**
|
|
* This function should set the text in numberLabel.
|
|
*
|
|
* @param value the string to set as the text for numberLabel
|
|
*/
|
|
public void setDisplay( String value )
|
|
{
|
|
// ### Add your code here. ####
|
|
numberLabel.setText(value);
|
|
// ### Done ###
|
|
}// end of setDisplay( String )
|
|
|
|
/**
|
|
* The driver for the program.
|
|
*
|
|
* @param args a String array of arguements.
|
|
*/
|
|
public static void main( String[] args )
|
|
{
|
|
CalculatorGUI gui = new CalculatorGUI();
|
|
}// end of main( String[] )
|
|
|
|
}// end of class CalculatorGUI
|