Java Code
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DebugValue extends JApplet
implements ItemListener
{
double cat = 4;
double dog = 10;
double horse = 350;
double costOfItem = 0;
double quantity;
JComboBox list;
JTextField qty;
JLabel price;
public void init( )
{
setLayout( new FlowLayout( ) );
qty = new JTextField( 4 );
list = new JComboBox( );
price = new JLabel( "0.0" );
list.addItem( "cat" );
list.addItem( "dog" );
list.addItem( "horse" );
list.addItemListener( this );
add( qty );
add( list );
add( price );
}
public void itemStateChanged( ItemEvent ie )
{
Object src = ie.getSource( );
if( ie.getStateChange( )==ItemEvent.SELECTED)
{
if( src == list )
{
quantity = Integer.parseInt( qty.getText( ) );
System.out.println( "qty=" + quantity );
System.out.println( "cost="+costOfItem);
double totalCost = costOfItem * quantity;
System.out.println( "total=" + totalCost );
price.setText( "" + totalCost );
}
}
}
}
©2006 by E.S.Boese. All Rights Reserved