first commit
This commit is contained in:
275
CS1322/p6/Extra Credit/Calculator.java
Normal file
275
CS1322/p6/Extra Credit/Calculator.java
Normal file
@@ -0,0 +1,275 @@
|
||||
/**
|
||||
* CS1312 Lab #17 - The Model.
|
||||
* <PRE>
|
||||
* Calculator.java - A basic calculator.
|
||||
*
|
||||
* Revisions: 1.1 Mar. 14, 2001
|
||||
* Added minor modifications to calculation behavior. Added
|
||||
* some documentation.
|
||||
* 1.0 Jan. 11, 2001
|
||||
* Created the Calculator class
|
||||
*
|
||||
* </PRE>
|
||||
*
|
||||
* @author <A HREF="mailto:demox@cc.gatech.edu">Luke A. Olbrish</A>
|
||||
* @version Version 1.1, Mar. 14, 2001
|
||||
*/
|
||||
|
||||
public class Calculator
|
||||
{
|
||||
// Constants
|
||||
|
||||
/**
|
||||
* This constant denotes the use of the clear operation.
|
||||
*/
|
||||
public static final int CLR_OP = 0;
|
||||
|
||||
/**
|
||||
* This constant denotes the computation operation.
|
||||
*/
|
||||
public static final int EQL_OP = 1;
|
||||
|
||||
/**
|
||||
* This constant denotes that addition should be used.
|
||||
*
|
||||
* @see #DIV_OP
|
||||
* @see #MUL_OP
|
||||
* @see #SUB_OP
|
||||
*/
|
||||
public static final int ADD_OP = 2;
|
||||
|
||||
/**
|
||||
* This constant denotes that subtraction should be used.
|
||||
*
|
||||
* @see #ADD_OP
|
||||
* @see #DIV_OP
|
||||
* @see #MUL_OP
|
||||
*/
|
||||
public static final int SUB_OP = 3;
|
||||
|
||||
/**
|
||||
* This constant denotes that multiplication should be used.
|
||||
*
|
||||
* @see #ADD_OP
|
||||
* @see #DIV_OP
|
||||
* @see #SUB_OP
|
||||
*/
|
||||
public static final int MUL_OP = 4;
|
||||
|
||||
/**
|
||||
* This constant denotes that division should be used.
|
||||
*
|
||||
* @see #ADD_OP
|
||||
* @see #MUL_OP
|
||||
* @see #SUB_OP
|
||||
*/
|
||||
public static final int DIV_OP = 5;
|
||||
|
||||
// Data Block
|
||||
|
||||
/**
|
||||
* Status variable that determines if operator1 is in an initialized form.
|
||||
*
|
||||
* @see #isOperator2Set
|
||||
* @see #operator1
|
||||
*/
|
||||
private boolean isOperator1Set = false;
|
||||
|
||||
/**
|
||||
* The value of the first operator.
|
||||
*
|
||||
* @see #isOperator1Set
|
||||
* @see #operator2
|
||||
*/
|
||||
private int operator1 = Integer.MIN_VALUE;
|
||||
|
||||
/**
|
||||
* Status variable that determines if operator2 is in an initialized form.
|
||||
*
|
||||
* @see #isOperator1Set
|
||||
* @see #operator2
|
||||
*/
|
||||
private boolean isOperator2Set = false;
|
||||
|
||||
/**
|
||||
* The value of the second operator.
|
||||
*
|
||||
* @see #isOperator2Set
|
||||
* @see #operator1
|
||||
*/
|
||||
private int operator2 = Integer.MIN_VALUE;
|
||||
|
||||
/**
|
||||
* Status variable that determines if the operation is in an initialized
|
||||
* form.
|
||||
*
|
||||
* @see #operation
|
||||
*/
|
||||
private boolean isOperationSet = false;
|
||||
|
||||
/**
|
||||
* The value of the math operation.
|
||||
*
|
||||
* @see #isOperationSet
|
||||
*/
|
||||
private int operation = Integer.MIN_VALUE;
|
||||
|
||||
// Accessors
|
||||
|
||||
/**
|
||||
* Reports whether the operation has been set yet.
|
||||
*
|
||||
* @see #isOperator1Set
|
||||
* @see #isOperator2Set
|
||||
*/
|
||||
public boolean isOperationSet()
|
||||
{
|
||||
return this.isOperationSet;
|
||||
}// end of isOperationSet()
|
||||
|
||||
/**
|
||||
* Reports whether operation 1 has been set yet.
|
||||
*
|
||||
* @see #isOperationSet
|
||||
* @see #isOperator2Set
|
||||
*/
|
||||
public boolean isOperator1Set()
|
||||
{
|
||||
return this.isOperator1Set;
|
||||
}// end of isOperator1Set()
|
||||
|
||||
/**
|
||||
* Reports whether operation 2 has been set yet.
|
||||
*
|
||||
* @see #isOperationSet
|
||||
* @see #isOperator1Set
|
||||
*/
|
||||
public boolean isOperator2Set()
|
||||
{
|
||||
return this.isOperator2Set;
|
||||
}// end of isOperator2Set()
|
||||
|
||||
/**
|
||||
* Gets the calculation of the operators and operation if possible. If
|
||||
* only operator1 is initialized, then operator1 is returned. If
|
||||
* non of the operators or operation are initialized than an Arithmetic
|
||||
* Exception is thrown.
|
||||
* <BR><BR>
|
||||
*
|
||||
* @exception ArithmeticException
|
||||
* @return an integer representing the calculation of the operators and the
|
||||
* operation.
|
||||
*/
|
||||
public int getCalculation() throws ArithmeticException
|
||||
{
|
||||
int returnVal = Integer.MIN_VALUE;
|
||||
|
||||
// check to see if operators and operation are initialized.
|
||||
if(( !isOperationSet ) ||
|
||||
( !isOperator2Set ))
|
||||
{
|
||||
if( isOperator1Set )
|
||||
return operator1;
|
||||
else
|
||||
throw new ArithmeticException( "Not Enough Information" );
|
||||
}// end of if
|
||||
|
||||
// Do the proper operation based upon the operation constant.
|
||||
switch( operation )
|
||||
{
|
||||
case ADD_OP:
|
||||
returnVal = operator1 + operator2;
|
||||
break;
|
||||
case SUB_OP:
|
||||
returnVal = operator1 - operator2;
|
||||
break;
|
||||
case MUL_OP:
|
||||
returnVal = operator1 * operator2;
|
||||
break;
|
||||
case DIV_OP:
|
||||
returnVal = operator1 / operator2;
|
||||
break;
|
||||
default:
|
||||
throw new ArithmeticException( "Not Enough Information" );
|
||||
}// end of switch( int )
|
||||
|
||||
// reset the data
|
||||
isOperator1Set = false;
|
||||
operator1 = Integer.MIN_VALUE;
|
||||
|
||||
isOperator2Set = false;
|
||||
operator2 = Integer.MIN_VALUE;
|
||||
|
||||
isOperationSet = false;
|
||||
operation = 0;
|
||||
|
||||
return returnVal;
|
||||
}// end of getCalculation()
|
||||
|
||||
// Modifiers
|
||||
|
||||
/**
|
||||
* Sets the first operator. This function will also set a flag that
|
||||
* indicates that the operator has a valid value.
|
||||
* <BR><BR>
|
||||
*
|
||||
* @param operator1 the new value of the first operator
|
||||
*/
|
||||
public void setOperator1( int operator1 )
|
||||
{
|
||||
this.isOperator1Set = true;
|
||||
this.operator1 = operator1;
|
||||
}// end of setOperator1( int )
|
||||
|
||||
/**
|
||||
* Sets the second operator. This function will also set a flag that
|
||||
* indicates that the operator has a valid value.
|
||||
* <BR><BR>
|
||||
*
|
||||
* @param operator2 the new value of the second operator
|
||||
*/
|
||||
public void setOperator2( int operator2 )
|
||||
{
|
||||
this.isOperator2Set = true;
|
||||
this.operator2 = operator2;
|
||||
}// end of setOperator2( int )
|
||||
|
||||
/**
|
||||
* Sets the math operation. This function will also set a flag that
|
||||
* indicates that the operation has a valid value.
|
||||
* <BR><BR>
|
||||
*
|
||||
* @param operation the new value of the operation
|
||||
*/
|
||||
public void setOperation( int operation )
|
||||
{
|
||||
if(( !isOperator1Set ) ||
|
||||
(( this.operation >= ADD_OP ) &&
|
||||
( this.operation <= DIV_OP )))
|
||||
return;
|
||||
|
||||
this.isOperationSet = true;
|
||||
this.operation = operation;
|
||||
}// end of setOperation( int )
|
||||
|
||||
// Operational Functions
|
||||
|
||||
/**
|
||||
* This function clears all information from the calculator. It will reset
|
||||
* the calculator to the state that it was when it was first created.
|
||||
* <BR><BR>
|
||||
*/
|
||||
public void clearCalulator()
|
||||
{
|
||||
// Reset all information back to the default state.
|
||||
isOperator1Set = false;
|
||||
operator1 = Integer.MIN_VALUE;
|
||||
|
||||
isOperator2Set = false;
|
||||
operator2 = Integer.MIN_VALUE;
|
||||
|
||||
isOperationSet = false;
|
||||
operation = 0;
|
||||
}// end of clearCalculator
|
||||
|
||||
}// end of class Calculator
|
||||
192
CS1322/p6/Extra Credit/CalculatorGUI.java
Normal file
192
CS1322/p6/Extra Credit/CalculatorGUI.java
Normal file
@@ -0,0 +1,192 @@
|
||||
/**
|
||||
* <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
|
||||
207
CS1322/p6/Extra Credit/EventHandler.java
Normal file
207
CS1322/p6/Extra Credit/EventHandler.java
Normal file
@@ -0,0 +1,207 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* EventHandler.java
|
||||
*
|
||||
* Revisions: 1.1 Mar. 14, 2001
|
||||
* Added minor modifications to event handling behavior. Added
|
||||
* some documentation.
|
||||
* 1.0 Jan. 13, 2001
|
||||
* Created the EventHandler class
|
||||
*
|
||||
* </PRE>
|
||||
*
|
||||
* @author <A HREF="mailto:demox@cc.gatech.edu">Luke A. Olbrish</A>
|
||||
* @version Version 1.0, Jan. 13, 2001
|
||||
*/
|
||||
|
||||
import java.awt.event.*;
|
||||
|
||||
public class EventHandler implements ActionListener
|
||||
{
|
||||
// Data Block
|
||||
|
||||
/**
|
||||
* The actual gui that will be the view for this program.
|
||||
*/
|
||||
private CalculatorGUI gui = null;
|
||||
|
||||
/**
|
||||
* This is the model that will actually perform calculations.
|
||||
*/
|
||||
private Calculator calc = null;
|
||||
|
||||
/**
|
||||
* This state variable will be used to keep track of whether the number
|
||||
* on the display needs to be replaced by new digits.
|
||||
*/
|
||||
private boolean isNumberStarted = false;
|
||||
|
||||
// Constructors
|
||||
|
||||
/**
|
||||
* Creates a new event handler that is bound by the gui view thats passed
|
||||
* as a parameter.
|
||||
*
|
||||
* @param gui the view that this control connects to.
|
||||
*/
|
||||
public EventHandler( CalculatorGUI gui )
|
||||
{
|
||||
// If the gui is null, then this class is being used improperly and should
|
||||
// exit.
|
||||
if( gui == null )
|
||||
System.exit( 0 );
|
||||
|
||||
// Add the model and view to the control.
|
||||
this.gui = gui;
|
||||
this.calc = new Calculator();
|
||||
}// end of constructor( CalculatorGUI )
|
||||
|
||||
// Action Listener
|
||||
|
||||
/**
|
||||
* This function recieves an action event and interprets the event to take
|
||||
* the proper action. It receives all the button actions from
|
||||
* CalculatorGUI.
|
||||
*
|
||||
* @param ae the action event that needs to be processed.
|
||||
*/
|
||||
public void actionPerformed( ActionEvent ae )
|
||||
{
|
||||
// No action to process.
|
||||
if(( ae == null ) ||
|
||||
( ae.getActionCommand() == null ))
|
||||
return;
|
||||
|
||||
// Clear the calculator screen and model.
|
||||
if( ae.getActionCommand().equals( "Clr" ) )
|
||||
doClearCalculator();
|
||||
|
||||
// Find out if an operation was selected and handle it.
|
||||
switch( ae.getActionCommand().charAt(0) )
|
||||
{
|
||||
case '=':
|
||||
doCalculation();
|
||||
break;
|
||||
case '+':
|
||||
doSetOp( Calculator.ADD_OP );
|
||||
break;
|
||||
case '-':
|
||||
doSetOp( Calculator.SUB_OP );
|
||||
break;
|
||||
case '*':
|
||||
doSetOp( Calculator.MUL_OP );
|
||||
break;
|
||||
case '/':
|
||||
doSetOp( Calculator.DIV_OP );
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}// end of switch
|
||||
|
||||
// Find out if a digit was pressed
|
||||
if( Character.isDigit( ae.getActionCommand().charAt(0) ) )
|
||||
doAddDigit( ae.getActionCommand() );
|
||||
}// end of actionPerformed( ActionEvent )
|
||||
|
||||
// Operational Functions
|
||||
|
||||
/**
|
||||
* This function assists the EventHandler by doing the actions needed to
|
||||
* add a digit to the display.
|
||||
*
|
||||
* @see #actionPerformed
|
||||
* @param digit a string that should have a single digit contents.
|
||||
*/
|
||||
private void doAddDigit( String digit )
|
||||
{
|
||||
// If diplay needs to be cleared before the digit is added.
|
||||
if( !isNumberStarted )
|
||||
{
|
||||
gui.setDisplay( digit );
|
||||
isNumberStarted = true;
|
||||
}
|
||||
else
|
||||
gui.setDisplay( gui.getDisplay() + digit );
|
||||
}// end of doAddDigit( String )
|
||||
|
||||
/**
|
||||
* Clears the gui display and resets the calculator model.
|
||||
*
|
||||
* @see #actionPerformed
|
||||
*/
|
||||
private void doClearCalculator()
|
||||
{
|
||||
isNumberStarted = false;
|
||||
gui.setDisplay( " " );
|
||||
calc.clearCalulator();
|
||||
return;
|
||||
}// end of doClearCalculator()
|
||||
|
||||
/**
|
||||
* Attempts to do a calculation. GUI is updated based on whether a
|
||||
* calculation occurs and returns the calculation to the gui.
|
||||
*
|
||||
* @see #actionPerformed
|
||||
*/
|
||||
public void doCalculation()
|
||||
{
|
||||
// holds the parsed operator #2.
|
||||
int val = 0;
|
||||
|
||||
try
|
||||
{
|
||||
// Attempt to parse an int from the gui.
|
||||
val = Integer.parseInt( gui.getDisplay() );
|
||||
|
||||
// If preconditions are met, then attempt a calculation.
|
||||
if(( calc.isOperator1Set() ) &&
|
||||
( calc.isOperationSet() ))
|
||||
{
|
||||
// Calculate and update gui.
|
||||
calc.setOperator2( val );
|
||||
gui.setDisplay( "" + calc.getCalculation() );
|
||||
// Reset display to take in numbers.
|
||||
isNumberStarted = false;
|
||||
}// end of if
|
||||
}// end of try
|
||||
catch( NumberFormatException nfe )
|
||||
{
|
||||
calc.clearCalulator();
|
||||
gui.setDisplay( "Error!" );
|
||||
return;
|
||||
}// end of catch( NumberFormatException )
|
||||
catch( ArithmeticException ae )
|
||||
{
|
||||
calc.clearCalulator();
|
||||
gui.setDisplay( "Calc. Error!" );
|
||||
return;
|
||||
}// end of catch( ArithmeticException )
|
||||
}// end of doCalculation()
|
||||
|
||||
/**
|
||||
* Set the operation for the calculation. If the operation is already set,
|
||||
* then do a calculation then set the operation.
|
||||
*
|
||||
* @see #actionPerformed
|
||||
*/
|
||||
public void doSetOp( int operation )
|
||||
{
|
||||
try
|
||||
{
|
||||
// Do calculation if operation is already set.
|
||||
if( calc.isOperationSet() )
|
||||
doCalculation();
|
||||
|
||||
// Set the first operator and set the operation.
|
||||
calc.setOperator1( Integer.parseInt( gui.getDisplay() ));
|
||||
calc.setOperation( operation );
|
||||
isNumberStarted = false;
|
||||
}// end of try.
|
||||
catch( NumberFormatException nfe )
|
||||
{
|
||||
calc.clearCalulator();
|
||||
gui.setDisplay( "Error!" );
|
||||
}// catch( NumberFormatException )
|
||||
}// end of doSetOp( int )
|
||||
|
||||
}// end of class EventHandler
|
||||
336
CS1322/p6/Extra Credit/p6ex.intro.txt
Normal file
336
CS1322/p6/Extra Credit/p6ex.intro.txt
Normal file
@@ -0,0 +1,336 @@
|
||||
CS1322: Lab #15 - Spring 2002.
|
||||
|
||||
|
||||
Basic GUI Information
|
||||
========================================================================
|
||||
|
||||
It has probably been covered in class how Swing differs from AWT
|
||||
(Abstract Windowing Toolkit). The basic idea is that java handles its
|
||||
components more in Swing instead of relying so much on the underlying
|
||||
operating system. Swing offers an object oriented approach to GUI
|
||||
creation by having base classes for all of the GUI components and then
|
||||
extending those classes in order to make specialized components. All
|
||||
swing components (except JFrame) inherit from Object->Component->
|
||||
Container->JComponent (JFrame doesn't extend from JComponent). Open
|
||||
up the java API and look at some of these classes. Some of these
|
||||
function names might not make sense to you, but there are many like
|
||||
Component.isVisible that you can probably guess what they do. For a
|
||||
list of Swing components, goto the J's in the API. Some examples are
|
||||
JButton, JPanel, JLabel, JMenu, etc...
|
||||
|
||||
Now you have a basic idea that there are some classes that exist and
|
||||
you use these classes to make GUIs. To actually use these classes,
|
||||
you must import java.awt.* (for classes like Component) and
|
||||
javax.swing.* (for the Swing components). You will also need to
|
||||
import java.awt.event.*, but this will be covered in lab18. Importing
|
||||
is a way of including classes that are not normally loaded. All of
|
||||
your programs up to this point have not needed the GUI classes, so it
|
||||
was more efficient to not ask java to include the GUI files. Now that
|
||||
you need these classes, you must ask java to look for them. Here's an
|
||||
example of how you would include:
|
||||
|
||||
/**
|
||||
* My class, bla bla bla...
|
||||
*/
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
|
||||
public class MyGUI
|
||||
{
|
||||
....
|
||||
|
||||
The basic starting point for all Swing GUIs -- that are not applets --
|
||||
use JFrame. The JFrame class is the actual window. It will contain
|
||||
all other GUI components. The basic order of operations is to create
|
||||
a JFrame, add and customize the JFrame, and then make the JFrame
|
||||
visible. Some people choose to subclass from JFrame and then override
|
||||
the constructor when they create a GUI. While others choose to treat
|
||||
the JFrame as an independent component.
|
||||
|
||||
(These are "working" examples, but you will need to kill them with
|
||||
Ctrl-C, because we haven't specified a way to close them nicely. You
|
||||
need to type this at the command line in order to close the GUI.)
|
||||
|
||||
imports...
|
||||
|
||||
public class MyGUI1 extends JFrame
|
||||
{
|
||||
public MyGUI1()
|
||||
{
|
||||
super( "Hello cs1322 Students" );
|
||||
setSize( 240, 100 );
|
||||
setVisible( true );
|
||||
}
|
||||
|
||||
public static void main( String[] args )
|
||||
{
|
||||
MyGUI1 mg = new MyGUI1();
|
||||
}
|
||||
}
|
||||
|
||||
The other way:
|
||||
|
||||
public class MyGUI1
|
||||
{
|
||||
private JFrame frame;
|
||||
|
||||
public MyGUI1()
|
||||
{
|
||||
frame = new JFrame( "Hello cs1322 Students" );
|
||||
frame.setSize( 240, 100 );
|
||||
frame.setVisible( true );
|
||||
}
|
||||
|
||||
public static void main( String[] args )
|
||||
{
|
||||
MyGUI1 mg = new MyGUI1();
|
||||
}
|
||||
}
|
||||
|
||||
Both of these examples will create a window that is 240 pixels wide
|
||||
and 100 pixels long. The title will say "Hello cs1322 Students". The
|
||||
frame isn't visible until its set visible.
|
||||
|
||||
Now that there is a window, its time to be able to add and populate
|
||||
the window. All frames have a content pane. This content pane is a
|
||||
Container (or any subclass of Container).
|
||||
|
||||
frame = new JFrame();
|
||||
Container = frame.getContentPane();
|
||||
|
||||
or
|
||||
|
||||
frame = new JFrame();
|
||||
Container c = new JPanel();
|
||||
frame.setContentPane( c );
|
||||
|
||||
One of the important functions that exists in Container is setLayout(
|
||||
LayoutManager ). A LayoutManager is a class that implements
|
||||
LayoutManager and allows the programmer to layout components in an
|
||||
easy manner. Anyone can make a LayoutManager, but this info file will
|
||||
discuss how to use 4 common types. It would be to you advantage to
|
||||
try these examples.
|
||||
|
||||
BorderLayout:
|
||||
|
||||
public MyGUI1()
|
||||
{
|
||||
frame = new JFrame( "Hello" );
|
||||
Container c = new JPanel();
|
||||
frame.setContentPane( c );
|
||||
|
||||
c.setLayout( new BorderLayout() );
|
||||
c.add( new JButton( "CENTER" ) );
|
||||
// or c.add( new JButton( "CENTER" ), BorderLayout.CENTER );
|
||||
|
||||
c.add( new JButton( "NORTH" ), BorderLayout.NORTH );
|
||||
c.add( new JButton( "SOUTH" ), BorderLayout.SOUTH );
|
||||
c.add( new JButton( "EAST" ), BorderLayout.EAST );
|
||||
c.add( new JButton( "WEST" ), BorderLayout.WEST );
|
||||
|
||||
frame.setSize( 400, 400 );
|
||||
frame.setVisible( true );
|
||||
}
|
||||
|
||||
Creates this:
|
||||
|
||||
----------------------------------
|
||||
| NORTH |
|
||||
|----------------------------------|
|
||||
| | | |
|
||||
| | | |
|
||||
| EAST | CENTER | WEST |
|
||||
| | | |
|
||||
| | | |
|
||||
|----------------------------------|
|
||||
| SOUTH |
|
||||
----------------------------------
|
||||
|
||||
Basically, The BorderLayout allows you to have a main content area
|
||||
(CENTER) and allows for secondary content areas. Also, you can embed
|
||||
content areas:
|
||||
|
||||
public MyGUI1()
|
||||
{
|
||||
frame = new JFrame( "Hello" );
|
||||
Container c = new JPanel();
|
||||
JPanel embed = new JPanel();
|
||||
frame.setContentPane( c );
|
||||
|
||||
c.setLayout( new BorderLayout() );
|
||||
embed.setLayout( new BorderLayout() );
|
||||
c.add( embed, BorderLayout.CENTER );
|
||||
|
||||
embed.add( new JButton( "CENTER" ), BorderLayout.CENTER );
|
||||
embed.add( new JButton( "E. NORTH" ), BorderLayout.NORTH );
|
||||
embed.add( new JButton( "E. SOUTH" ), BorderLayout.SOUTH );
|
||||
embed.add( new JButton( "E. EAST" ), BorderLayout.EAST );
|
||||
embed.add( new JButton( "E. WEST" ), BorderLayout.WEST );
|
||||
|
||||
c.add( new JButton( "NORTH" ), BorderLayout.NORTH );
|
||||
c.add( new JButton( "SOUTH" ), BorderLayout.SOUTH );
|
||||
c.add( new JButton( "EAST" ), BorderLayout.EAST );
|
||||
c.add( new JButton( "WEST" ), BorderLayout.WEST );
|
||||
|
||||
frame.setSize( 500, 250 );
|
||||
frame.setVisible( true );
|
||||
}
|
||||
|
||||
(run the code to see what it actually looks like).
|
||||
|
||||
GridLayout:
|
||||
|
||||
public MyGUI1()
|
||||
{
|
||||
frame = new JFrame( "Hello" );
|
||||
Container c = new JPanel();
|
||||
frame.setContentPane( c );
|
||||
|
||||
c.setLayout( new GridLayout( 2, 3 ) );
|
||||
|
||||
c.add( new JButton( "1" ) );
|
||||
c.add( new JButton( "2" ) );
|
||||
c.add( new JButton( "3" ) );
|
||||
c.add( new JButton( "4" ) );
|
||||
c.add( new JButton( "5" ) );
|
||||
c.add( new JButton( "6" ) );
|
||||
|
||||
frame.setSize( 500, 250 );
|
||||
frame.setVisible( true );
|
||||
}
|
||||
|
||||
Creates this:
|
||||
|
||||
-----------------
|
||||
| | | |
|
||||
| 1 | 2 | 3 |
|
||||
| | | |
|
||||
|-----------------|
|
||||
| | | |
|
||||
| 4 | 5 | 6 |
|
||||
| | | |
|
||||
-----------------
|
||||
|
||||
c.add( new JButton( "1" ), 0 );
|
||||
c.add( new JButton( "2" ), 0 );
|
||||
c.add( new JButton( "3" ), 0 );
|
||||
c.add( new JButton( "4" ), 0 );
|
||||
c.add( new JButton( "5" ), 0 );
|
||||
c.add( new JButton( "6" ), 0 );
|
||||
|
||||
Creates this( it inserts into slot 0 and pushes everything back):
|
||||
|
||||
-----------------
|
||||
| | | |
|
||||
| 6 | 5 | 4 |
|
||||
| | | |
|
||||
|-----------------|
|
||||
| | | |
|
||||
| 3 | 2 | 1 |
|
||||
| | | |
|
||||
-----------------
|
||||
|
||||
The GridLayout allows the programmer to make a grid that scales in
|
||||
size and can have a component embedded into each grid location.
|
||||
|
||||
BoxLayout:
|
||||
|
||||
public MyGUI1()
|
||||
{
|
||||
frame = new JFrame( "Hello" );
|
||||
Container c = new JPanel();
|
||||
frame.setContentPane( c );
|
||||
|
||||
c.setLayout( new BoxLayout( c, BoxLayout.X_AXIS ) );
|
||||
|
||||
c.add( new JButton( "1" ) );
|
||||
c.add( new JButton( "2" ) );
|
||||
c.add( new JButton( "3" ) );
|
||||
c.add( new JButton( "4" ) );
|
||||
c.add( new JButton( "5" ) );
|
||||
c.add( new JButton( "6" ) );
|
||||
|
||||
frame.setSize( 500, 250 );
|
||||
frame.setVisible( true );
|
||||
}
|
||||
|
||||
Creates this:
|
||||
|
||||
-----------------------------------------------------
|
||||
| |
|
||||
| |
|
||||
| |
|
||||
|----------------------------------- |
|
||||
| | | | | | | |
|
||||
| 1 | 2 | 3 | 4 | 5 | 6 | |
|
||||
| | | | | | | |
|
||||
|----------------------------------- |
|
||||
| |
|
||||
| |
|
||||
| |
|
||||
-----------------------------------------------------
|
||||
|
||||
Basically, the BoxLayout adds components in order, but doesn't do the
|
||||
same kind of resizing like Border and Grid. Try BoxLayout.Y_AXIS
|
||||
too.
|
||||
|
||||
FlowLayout:
|
||||
|
||||
FlowLayout is the default layout used by components. To test this,
|
||||
take the code from BoxLayout and delete the line c.setLayout. Be sure
|
||||
to resize the window. You will notice how the flow layout just seems
|
||||
to add other components and places them in order. If your resize to a
|
||||
smaller window, some buttons move down to different lines.
|
||||
|
||||
JLabel, JButton, and Customizations
|
||||
========================================================================
|
||||
|
||||
A JLabel is a GUI Component that displays text, but does not allow the
|
||||
user to modify the information.
|
||||
|
||||
public MyGUI1()
|
||||
{
|
||||
frame = new JFrame( "Hello" );
|
||||
Container c = new JPanel();
|
||||
frame.setContentPane( c );
|
||||
|
||||
JLabel jl = new JLabel( "This is a test." );
|
||||
// Or
|
||||
// jl = new JLabel();
|
||||
// jl.setText( "This is a test." );
|
||||
|
||||
c.add( jl );
|
||||
|
||||
frame.setSize( 250, 250 );
|
||||
frame.setVisible( true );
|
||||
}
|
||||
|
||||
JLabels can have their information changed by the program through
|
||||
setText and you can also get the text through getText. Like all Swing
|
||||
components JLabels have all the functions from Component, Container,
|
||||
JComponent, as well as the functions JLabel adds. These functions
|
||||
allow for many customizations and options. for example this code
|
||||
below takes the default font from the Jlabel jl and increases the size
|
||||
by 2.5:
|
||||
|
||||
font = jl.getFont();
|
||||
font = font.deriveFont( (float)( font.getSize2D() * 2.5 ) );
|
||||
jl.setFont( font );
|
||||
|
||||
If you want to customize a java component, there usually is a very
|
||||
good chance that someone else has wanted that customization also.
|
||||
Check the API, the java tutorial, and search the internet.
|
||||
|
||||
The JButton is a GUI component that can be clicked and the click often
|
||||
signafies an action to the program (covered in lab 18). This lab is
|
||||
concerned with your ability to create, add to layouts, and customize
|
||||
JButtons.
|
||||
|
||||
JButton jb = new JButton();
|
||||
jb.setText( "My Button" );
|
||||
jb.setBackground( java.awt.Color.red );
|
||||
jb.setFocusPainted( false ); // Doesn't draw that box around the
|
||||
// button, when clicked.
|
||||
jb.setEnabled( false ); // disables the button.
|
||||
111
CS1322/p6/Extra Credit/p6ex.nfo.txt
Normal file
111
CS1322/p6/Extra Credit/p6ex.nfo.txt
Normal file
@@ -0,0 +1,111 @@
|
||||
CS1322: Lab #15 - Spring 2002.
|
||||
|
||||
Program Title: Complete the Calculator GUI.
|
||||
|
||||
Files Provided
|
||||
========================================================================
|
||||
|
||||
o lab15.nfo
|
||||
o CalculatorGUI.java - The View (the only file you edit).
|
||||
o EventHandler.java - The Control.
|
||||
o Calculator.java - The Model.
|
||||
|
||||
|
||||
Introduction
|
||||
========================================================================
|
||||
|
||||
Its time to learn about Graphical User Interfaces. In this lab, you
|
||||
will be using the Swing GUI library to create a user interface. It
|
||||
will focus on GUI layout and component creation. You are given a
|
||||
working EventHandler that will handle your button presses and a model
|
||||
that will actually do the calculations. You will need to Complete
|
||||
CalculatorGUI so that the GUI components are displayed to the
|
||||
specification stated later in this nfo file.
|
||||
|
||||
Excellent References:
|
||||
o The JAVA API
|
||||
http://java.sun.com/products/jdk/1.3/docs/api/index.html
|
||||
o The JAVA Tutorial
|
||||
http://java.sun.com/docs/books/tutorial/
|
||||
|
||||
Also, lab15.intro is an excellent source for learning besic GUI
|
||||
information. Anyone who has not done Swing GUI programming before
|
||||
should go through its examples (you are responsible for that
|
||||
information).
|
||||
|
||||
You will probably need to visit the API to properly finish this lab
|
||||
and are expected to be able to do this.
|
||||
|
||||
|
||||
The Project
|
||||
========================================================================
|
||||
|
||||
It is your job to create the components for a CalculatorGUI, so that
|
||||
one can use the calulator.
|
||||
|
||||
-----------------------
|
||||
|cs1322 Lab15 |X| The guiFrame should say "cs1322 is fun!"
|
||||
|-----------------------| The numberLabel should say "Calculator"
|
||||
| | There should be 10 numberedButtons
|
||||
| Calculator | numberedButtons[0 - 9] = "0" - "9"
|
||||
| | There should be 6 operationButtons
|
||||
|-----------------------| operationButtons[0] = "Clr"
|
||||
| | | | | operationButtons[1] = "="
|
||||
| 1 | 2 | 3 | + | operationButtons[2] = "+"
|
||||
| | | | | operationButtons[3] = "-"
|
||||
|-----------------------| operationButtons[4] = "*"
|
||||
| | | | | operationButtons[5] = "/"
|
||||
| 4 | 5 | 6 | - | The empty spots by "0" should be empty JPanels
|
||||
| | | | |
|
||||
|-----------------------| contentPane should be a JPanel with a BorderLayout
|
||||
| | | | | operationPanel should be a JPanel with a
|
||||
| 7 | 8 | 9 | * | GridLayout thats 1 row by 2 columns.
|
||||
| | | | | buttonPanel should be a JPanel with a GridLayout
|
||||
|-----------------------| thats 4 by 4.
|
||||
| | | | |
|
||||
| | 0 | | / | contentPane should become guiFrame's content pane.
|
||||
| | | | | numberLabel should be in the North of contentPane.
|
||||
|-----------------------| buttonPanel should be in the Center of contentPane.
|
||||
| | | operationPanel should be in the South.
|
||||
| Clr | = | "Clr" and "=" should be in operationPanel.
|
||||
| | | "1" - "9", "+" - "/" should be in buttonPanel.
|
||||
-----------------------
|
||||
|
||||
Customizations:
|
||||
|
||||
The numberLabel should have its font increased by a factor of 2.0 (size * 2.0)
|
||||
(you may want to do numberLabel.setHorizontalAlignment(numberLabel.RIGHT); too)
|
||||
"0" - "9" should have their backgrounds changed to java.awt.Color.cyan
|
||||
"=" - "/" should have their backgrounds changed to java.awt.Color.pink
|
||||
"Clr" should have their backgrounds changed to java.awt.Color.yellow
|
||||
Prevent all the buttons from having their focus be painted.
|
||||
|
||||
In CalculatorGUI:
|
||||
|
||||
Complete the functions:
|
||||
- doMakeObjects();
|
||||
- doAddObjects();
|
||||
- doCustomizations();
|
||||
|
||||
The function doEventHandler is provided for you (this time). This
|
||||
information will be the focus of lab16.
|
||||
|
||||
Two more functions. These functions will be used by the event
|
||||
handler to update the calculator's display. The event handler needs
|
||||
to be able to set the display and then read the display to do
|
||||
calculations.
|
||||
|
||||
- getDisplay
|
||||
It should return the text contained in numberLabel.
|
||||
|
||||
- setDisplay
|
||||
It should set the text in numberLabel.
|
||||
|
||||
To test your code, compile the supplied files and run CalculatorGUI.
|
||||
|
||||
TURNIN
|
||||
========================================================================
|
||||
|
||||
o Files to be turned in:
|
||||
|
||||
CalculatorGUI.java
|
||||
Reference in New Issue
Block a user