//Childcare.java
//Michael E. Shultz
//Java Programming
//Tutorial 7
//J341 #2
import java.applet.*;
import java.awt.*;
import Dialog3;
public class Childcare extends Applet
{
Dialog3 Three = new Dialog3(this);
Image myImage;
public void init()
{
myImage = getImage (getDocumentBase(),"Blocks.gif");
Three.CreateControls();
Three.TOTAL.setEditable(false);
Three.RATE.setText("$45.00");
Three.RATE.setEditable(false);
Three.CHILDREN.requestFocus();
}
public void paint (Graphics g)
{
g.drawImage(myImage, 200,200, this);
}
public boolean action (Event thisEvent, Object thisObject)
{
if (thisEvent.target == Three.CALCULATE)
CalcTotal();
else if (thisEvent.target == Three.CLEAR)
{
Three.CHILDREN.setText("");
Three.DAYS.setText("");
Three.TOTAL.setText("");
Three.CHILDREN.requestFocus();
}
return true;
}
public void CalcTotal()
{
double numChildren = toFloat(Three.CHILDREN.getText() );
int totalDays = Integer.parseInt(Three.DAYS.getText() );
double ChildRate = 45.00;
double totalRate = 0;
totalRate = numChildren * totalDays * ChildRate;
Three.TOTAL.setText("$" + totalRate);
}
public float toFloat (String str)
{
Float value = new Float(str);
return value.floatValue();
}
}
//------------------------------------------------------------------------------
// Dialog3.java:
// Implementation for container control initialization class Dialog3
//
// WARNING: Do not modify this file. This file is recreated each time its
// associated .rct/.res file is sent through the Java Resource Wizard!
//
// This class can be use to create controls within any container, however, the
// following describes how to use this class with an Applet. For addtional
// information on using Java Resource Wizard generated classes, refer to the
// Visual J++ 1.1 documention.
//
// 1) Import this class in the .java file for the Applet that will use it:
//
// import Dialog3;
//
// 2) Create an instance of this class in your Applet's 'init' member, and call
// CreateControls() through this object:
//
// Dialog3 ctrls = new Dialog3 (this);
// ctrls.CreateControls();
//
// 3) To process events generated from user action on these controls, implement
// the 'handleEvent' member for your applet:
//
// public boolean handleEvent (Event evt)
// {
//
// }
//
//------------------------------------------------------------------------------
import java.awt.*;
import DialogLayout;
public class Dialog3
{
Container m_Parent = null;
boolean m_fInitialized = false;
DialogLayout m_Layout;
// Control definitions
//--------------------------------------------------------------------------
Button CALCULATE;
Button CLEAR;
Label lblChildren;
Label Dialog3;
TextField CHILDREN;
TextField DAYS;
Label lblTotal;
TextField TOTAL;
Label lblRate2;
TextField RATE;
// Constructor
//--------------------------------------------------------------------------
public Dialog3 (Container parent)
{
m_Parent = parent;
}
// Initialization.
//--------------------------------------------------------------------------
public boolean CreateControls()
{
// Can only init controls once
//----------------------------------------------------------------------
if (m_fInitialized || m_Parent == null)
return false;
// Parent must be a derivation of the Container class
//----------------------------------------------------------------------
if (!(m_Parent instanceof Container))
return false;
// Since there is no way to know if a given font is supported from
// platform to platform, we only change the size of the font, and not
// type face name. And, we only change the font if the dialog resource
// specified a font.
//----------------------------------------------------------------------
Font OldFnt = m_Parent.getFont();
if (OldFnt != null)
{
Font NewFnt = new Font(OldFnt.getName(), OldFnt.getStyle(), 8);
m_Parent.setFont(NewFnt);
}
// All position and sizes are in Dialog Units, so, we use the
// DialogLayout manager.
//----------------------------------------------------------------------
m_Layout = new DialogLayout(m_Parent, 209, 133);
m_Parent.setLayout(m_Layout);
m_Parent.addNotify();
Dimension size = m_Layout.getDialogSize();
Insets insets = m_Parent.insets();
m_Parent.resize(insets.left + size.width + insets.right,
insets.top + size.height + insets.bottom);
// Control creation
//----------------------------------------------------------------------
CALCULATE = new Button ("Calculate");
m_Parent.add(CALCULATE);
m_Layout.setShape(CALCULATE, 10, 105, 50, 14);
CLEAR = new Button ("Clear");
m_Parent.add(CLEAR);
m_Layout.setShape(CLEAR, 150, 105, 50, 14);
lblChildren = new Label ("Children", Label.CENTER);
m_Parent.add(lblChildren);
m_Layout.setShape(lblChildren, 10, 10, 45, 15);
Dialog3 = new Label ("Days", Label.CENTER);
m_Parent.add(Dialog3);
m_Layout.setShape(Dialog3, 10, 40, 45, 15);
CHILDREN = new TextField ("");
m_Parent.add(CHILDREN);
m_Layout.setShape(CHILDREN, 70, 10, 45, 15);
DAYS = new TextField ("");
m_Parent.add(DAYS);
m_Layout.setShape(DAYS, 70, 40, 45, 15);
lblTotal = new Label ("Total Amount", Label.CENTER);
m_Parent.add(lblTotal);
m_Layout.setShape(lblTotal, 155, 15, 45, 15);
TOTAL = new TextField ("");
m_Parent.add(TOTAL);
m_Layout.setShape(TOTAL, 155, 40, 45, 15);
lblRate2 = new Label ("Rate", Label.CENTER);
m_Parent.add(lblRate2);
m_Layout.setShape(lblRate2, 10, 70, 45, 15);
RATE = new TextField ("");
m_Parent.add(RATE);
m_Layout.setShape(RATE, 70, 70, 45, 15);
m_fInitialized = true;
return true;
}
}
© Copyright 2001 by Michael E. Shultz, All rights reserved