// Tutorial 5: Inventory.java
// Calculates the number of items in a shipment based on the number
// of cartons received and the number of items per carton.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Inventory extends JFrame
{
// JLabel and JTextField for cartons per shipment
private JLabel cartonsJLabel;
public JTextField cartonsJTextField;
// JLabel and JTextField for items per carton
public JLabel itemsJLabel;
public JTextField itemsJTextField;
// JLabel and JTextField for total items per shipment
private JLabel totalJLabel;
public JTextField totalResultJTextField;
// JButton to initiate calculation of total items per shipment
private JButton calculateJButton;
// no-argument constructor
public Inventory()
{
createUserInterface();
}
// create and position GUI components; register event handlers
public void createUserInterface()
{
// get content pane and set layout to null
Container contentPane = getContentPane();
contentPane.setLayout( null );
// set up cartonsJLabel
cartonsJLabel = new JLabel();
cartonsJLabel.setText( "Cartons per shipment:" );
cartonsJLabel.setBounds( 16, 16, 130, 21 );
contentPane.add( cartonsJLabel );
// set up itemsJLabel
itemsJLabel = new JLabel();
itemsJLabel.setText("Items per Carton:");
itemsJLabel.setBounds(16, 48, 104, 21);
contentPane.add( itemsJLabel );
// set up totalJLabel
totalJLabel = new JLabel();
totalJLabel.setText("Total:");
totalJLabel.setBounds(204, 16, 40, 21);
contentPane.add( totalJLabel );
// set up cartonsJTextField
cartonsJTextField = new JTextField();
cartonsJTextField.setText("0");
cartonsJTextField.setBounds( 148, 16, 40, 21 );
cartonsJTextField.setHorizontalAlignment(JTextField.RIGHT);
contentPane.add( cartonsJTextField );
// set up itemsJTextField
itemsJTextField = new JTextField();
itemsJTextField.setText("0");
itemsJTextField.setBounds( 148, 48, 40, 21 );
itemsJTextField.setHorizontalAlignment(JTextField.RIGHT);
contentPane.add( itemsJTextField );
// set up totalResultJTextField
totalResultJTextField = new JTextField();
totalResultJTextField.setBounds( 244, 16, 86, 21 );
totalResultJTextField.setHorizontalAlignment(
JTextField.RIGHT);
totalResultJTextField.setEditable( false );
contentPane.add( totalResultJTextField );
// set up calculateJButton
calculateJButton = new JButton();
calculateJButton.setText("Calculate Total");
calculateJButton.setBounds(204, 48, 126, 24);
contentPane.add( calculateJButton );
calculateJButton.addActionListener(
new ActionListener() // anonymous inner class
{
// event handler called when calculateJButton is pressed
public void actionPerformed( ActionEvent event )
{
calculateJButtonActionPerformed( event );
}
} // end anonymous inner class
); // end call to addActionListener
// set properties of application’s window
setTitle( "Inventory" ); // set title bar text
setSize( 354, 112 ); // set window size
setVisible( true ); // display window
} // end method createUserInterface
//calculate the total items in the shipment
public void calculateJButtonActionPerformed( ActionEvent event )
{
int cartons; // stores number of cartons
int items;
int result;
//retrieve number from JTextFields
cartons = Integer.parseInt( cartonsJTextField.getText() );
items = Integer.parseInt( itemsJTextField.getText() );
result = cartons * items;
totalResultJTextField.setText( String.valueOf( result ) );
} // end method calculateJButtonActionPerformed
public void cartonsJTextFieldKeyPressed( KeyEvent event )
{
totalResultJTextField.setText( "" );
}
public void itemsJTextFieldKeyPressed( KeyEvent event )
{
totalResultJTextField.setText( "" );
}
public static void main( String[] args )
{
Inventory application = new Inventory();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
}