home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 1998 November
/
Chip_1998-11_cd.bin
/
tema
/
Cafe
/
WDESAMPL.BIN
/
CalculateFrame.java
< prev
next >
Wrap
Text File
|
1998-02-26
|
9KB
|
295 lines
/*
A basic extension of the java.awt.Frame class
*/
import java.awt.*;
import symantec.itools.awt.KeyPressManagerPanel;
public class CalculateFrame extends Frame {
void Frame1_WindowClosing(java.awt.event.WindowEvent event)
{
hide(); // hide the Frame
}
public CalculateFrame()
{
// This code is automatically generated by Visual Cafe when you add
// components to the visual environment. It instantiates and initializes
// the components. To modify the code, only use code syntax that matches
// what Visual Cafe can generate, or Visual Cafe may be unable to back
// parse your Java file into its visual environment.
//{{INIT_CONTROLS
GridBagLayout gridBagLayout;
gridBagLayout = new GridBagLayout();
setLayout(gridBagLayout);
setVisible(false);
setSize(282,112);
Number1Label = new java.awt.Label("Number 1 =");
Number1Label.setBounds(27,5,79,23);
GridBagConstraints gbc;
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1.0;
gbc.weighty = 0.5;
gbc.fill = GridBagConstraints.NONE;
gbc.insets = new Insets(0,0,0,0);
((GridBagLayout)getLayout()).setConstraints(Number1Label, gbc);
add(Number1Label);
Number1TextField = new java.awt.TextField(15);
Number1TextField.setBounds(158,5,24,23);
gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 0;
gbc.weightx = 1.0;
gbc.weighty = 0.5;
gbc.fill = GridBagConstraints.NONE;
gbc.insets = new Insets(0,0,0,0);
((GridBagLayout)getLayout()).setConstraints(Number1TextField, gbc);
add(Number1TextField);
Number2Label = new java.awt.Label("Number 2 =");
Number2Label.setBounds(27,38,79,23);
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 1;
gbc.weightx = 1.0;
gbc.weighty = 0.5;
gbc.fill = GridBagConstraints.NONE;
gbc.insets = new Insets(0,0,0,0);
((GridBagLayout)getLayout()).setConstraints(Number2Label, gbc);
add(Number2Label);
Number2TextField = new java.awt.TextField(15);
Number2TextField.setBounds(158,38,24,23);
gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 1;
gbc.weightx = 1.0;
gbc.weighty = 0.5;
gbc.fill = GridBagConstraints.NONE;
gbc.insets = new Insets(0,0,0,0);
((GridBagLayout)getLayout()).setConstraints(Number2TextField, gbc);
add(Number2TextField);
AnswerLabel = new java.awt.Label("Answer =");
AnswerLabel.setBounds(34,77,65,23);
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 2;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.fill = GridBagConstraints.NONE;
gbc.insets = new Insets(0,0,0,0);
((GridBagLayout)getLayout()).setConstraints(AnswerLabel, gbc);
add(AnswerLabel);
AnswerTextField = new java.awt.TextField(15);
AnswerTextField.setEditable(false);
AnswerTextField.setBounds(158,77,24,23);
AnswerTextField.setBackground(new Color(12632256));
gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 2;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.fill = GridBagConstraints.NONE;
gbc.insets = new Insets(0,0,0,0);
((GridBagLayout)getLayout()).setConstraints(AnswerTextField, gbc);
add(AnswerTextField);
panel1 = new java.awt.Panel();
panel1.setLayout(new GridLayout(4,1,0,2));
panel1.setBounds(208,6,21,98);
gbc = new GridBagConstraints();
gbc.gridx = 3;
gbc.gridy = 0;
gbc.gridheight = 3;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.NONE;
gbc.insets = new Insets(0,0,0,0);
((GridBagLayout)getLayout()).setConstraints(panel1, gbc);
add(panel1);
PlusButton = new java.awt.Button();
PlusButton.setActionCommand("button");
PlusButton.setLabel("+");
PlusButton.setBounds(0,0,21,23);
panel1.add(PlusButton);
SubtractButton = new java.awt.Button();
SubtractButton.setActionCommand("button");
SubtractButton.setLabel("-");
SubtractButton.setBounds(0,25,21,23);
panel1.add(SubtractButton);
MultiplyButton = new java.awt.Button();
MultiplyButton.setActionCommand("button");
MultiplyButton.setLabel("X");
MultiplyButton.setBounds(0,50,21,23);
panel1.add(MultiplyButton);
DivideButton = new java.awt.Button();
DivideButton.setActionCommand("button");
DivideButton.setLabel("/");
DivideButton.setBounds(0,75,21,23);
panel1.add(DivideButton);
setTitle("Simple Calculation");
//}}
//{{INIT_MENUS
//}}
//{{REGISTER_LISTENERS
Window lWindow = new Window();
addWindowListener(lWindow);
Action lAction = new Action();
PlusButton.addActionListener(lAction);
SubtractButton.addActionListener(lAction);
MultiplyButton.addActionListener(lAction);
DivideButton.addActionListener(lAction);
//}}
}
public CalculateFrame(String title)
{
this();
setTitle(title);
}
public synchronized void show()
{
move(50, 50);
super.show();
}
public void addNotify()
{
super.addNotify();
if (fComponentsAdjusted)
return;
// Adjust components according to the insets
setSize(insets().left + insets().right + getSize().width, insets().top + insets().bottom + getSize().height);
Component components[] = getComponents();
for (int i = 0; i < components.length; i++)
{
Point p = components[i].getLocation();
p.translate(insets().left, insets().top);
components[i].setLocation(p);
}
fComponentsAdjusted = true;
}
boolean fComponentsAdjusted = false;
//{{DECLARE_CONTROLS
java.awt.Label Number1Label;
java.awt.TextField Number1TextField;
java.awt.Label Number2Label;
java.awt.TextField Number2TextField;
java.awt.Label AnswerLabel;
java.awt.TextField AnswerTextField;
java.awt.Panel panel1;
java.awt.Button PlusButton;
java.awt.Button SubtractButton;
java.awt.Button MultiplyButton;
java.awt.Button DivideButton;
//}}
//{{DECLARE_MENUS
//}}
class Window extends java.awt.event.WindowAdapter
{
public void windowClosing(java.awt.event.WindowEvent event)
{
Object object = event.getSource();
if (object == CalculateFrame.this)
Frame1_WindowClosing(event);
}
}
class Action implements java.awt.event.ActionListener
{
public void actionPerformed(java.awt.event.ActionEvent event)
{
int i = 0;
Object object = event.getSource();
if (object == PlusButton)
PlusButton_Action(event);
if (object == SubtractButton)
SubtractButton_Action(event);
if (object == MultiplyButton)
MultiplyButton_Action(event);
if (object == DivideButton)
DivideButton_Action(event);
}
}
void PlusButton_Action(java.awt.event.ActionEvent event)
{
double sum, num1, num2;
try {
num1 = (Double.valueOf(Number1TextField.getText())).doubleValue();
num2 = (Double.valueOf(Number2TextField.getText())).doubleValue();
} catch (NumberFormatException e) { return; }
sum = num1 + num2;
if (sum < 0) { AnswerTextField.setBackground(Color.red); }
else { AnswerTextField.setBackground(Color.lightGray); }
AnswerTextField.setText(String.valueOf(sum));
}
void SubtractButton_Action(java.awt.event.ActionEvent event)
{
double diff, num1, num2;
try {
num1 = (Double.valueOf(Number1TextField.getText())).doubleValue();
num2 = (Double.valueOf(Number2TextField.getText())).doubleValue();
} catch (NumberFormatException e) { return; }
diff = num1 - num2;
if (diff < 0) { AnswerTextField.setBackground(Color.red); }
else { AnswerTextField.setBackground(Color.gray); }
AnswerTextField.setText(String.valueOf(diff));
}
void MultiplyButton_Action(java.awt.event.ActionEvent event)
{
double product, num1, num2;
try {
num1 = (Double.valueOf(Number1TextField.getText())).doubleValue();
num2 = (Double.valueOf(Number2TextField.getText())).doubleValue();
} catch (NumberFormatException e) { return; }
product = num1 * num2;
if (product < 0) { AnswerTextField.setBackground(Color.red); }
else { AnswerTextField.setBackground(Color.gray); }
AnswerTextField.setText(String.valueOf(product));
}
void DivideButton_Action(java.awt.event.ActionEvent event)
{
double quotient, num1, num2;
try {
num1 = (Double.valueOf(Number1TextField.getText())).doubleValue();
num2 = (Double.valueOf(Number2TextField.getText())).doubleValue();
} catch (NumberFormatException e) { return; }
if (num2 == 0) return;
quotient = num1 / num2;
if (quotient < 0) { AnswerTextField.setBackground(Color.red); }
else { AnswerTextField.setBackground(Color.gray); }
AnswerTextField.setText(String.valueOf(quotient));
}
}