home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / WDESAMPL.BIN / CalculateFrame.java < prev    next >
Text File  |  1998-02-26  |  9KB  |  295 lines

  1. /*
  2.     A basic extension of the java.awt.Frame class
  3.  */
  4.  
  5. import java.awt.*;
  6.  
  7. import symantec.itools.awt.KeyPressManagerPanel;
  8. public class CalculateFrame extends Frame {
  9.  
  10.     void Frame1_WindowClosing(java.awt.event.WindowEvent event)
  11.     {
  12.         hide();         // hide the Frame
  13.     }
  14.  
  15.     public CalculateFrame()
  16.     {
  17.         // This code is automatically generated by Visual Cafe when you add
  18.         // components to the visual environment. It instantiates and initializes
  19.         // the components. To modify the code, only use code syntax that matches
  20.         // what Visual Cafe can generate, or Visual Cafe may be unable to back
  21.         // parse your Java file into its visual environment.
  22.         //{{INIT_CONTROLS
  23.         GridBagLayout gridBagLayout;
  24.         gridBagLayout = new GridBagLayout();
  25.         setLayout(gridBagLayout);
  26.         setVisible(false);
  27.         setSize(282,112);
  28.         Number1Label = new java.awt.Label("Number 1 =");
  29.         Number1Label.setBounds(27,5,79,23);
  30.         GridBagConstraints gbc;
  31.         gbc = new GridBagConstraints();
  32.         gbc.gridx = 0;
  33.         gbc.gridy = 0;
  34.         gbc.weightx = 1.0;
  35.         gbc.weighty = 0.5;
  36.         gbc.fill = GridBagConstraints.NONE;
  37.         gbc.insets = new Insets(0,0,0,0);
  38.         ((GridBagLayout)getLayout()).setConstraints(Number1Label, gbc);
  39.         add(Number1Label);
  40.         Number1TextField = new java.awt.TextField(15);
  41.         Number1TextField.setBounds(158,5,24,23);
  42.         gbc = new GridBagConstraints();
  43.         gbc.gridx = 1;
  44.         gbc.gridy = 0;
  45.         gbc.weightx = 1.0;
  46.         gbc.weighty = 0.5;
  47.         gbc.fill = GridBagConstraints.NONE;
  48.         gbc.insets = new Insets(0,0,0,0);
  49.         ((GridBagLayout)getLayout()).setConstraints(Number1TextField, gbc);
  50.         add(Number1TextField);
  51.         Number2Label = new java.awt.Label("Number 2 =");
  52.         Number2Label.setBounds(27,38,79,23);
  53.         gbc = new GridBagConstraints();
  54.         gbc.gridx = 0;
  55.         gbc.gridy = 1;
  56.         gbc.weightx = 1.0;
  57.         gbc.weighty = 0.5;
  58.         gbc.fill = GridBagConstraints.NONE;
  59.         gbc.insets = new Insets(0,0,0,0);
  60.         ((GridBagLayout)getLayout()).setConstraints(Number2Label, gbc);
  61.         add(Number2Label);
  62.         Number2TextField = new java.awt.TextField(15);
  63.         Number2TextField.setBounds(158,38,24,23);
  64.         gbc = new GridBagConstraints();
  65.         gbc.gridx = 1;
  66.         gbc.gridy = 1;
  67.         gbc.weightx = 1.0;
  68.         gbc.weighty = 0.5;
  69.         gbc.fill = GridBagConstraints.NONE;
  70.         gbc.insets = new Insets(0,0,0,0);
  71.         ((GridBagLayout)getLayout()).setConstraints(Number2TextField, gbc);
  72.         add(Number2TextField);
  73.         AnswerLabel = new java.awt.Label("Answer =");
  74.         AnswerLabel.setBounds(34,77,65,23);
  75.         gbc = new GridBagConstraints();
  76.         gbc.gridx = 0;
  77.         gbc.gridy = 2;
  78.         gbc.weightx = 1.0;
  79.         gbc.weighty = 1.0;
  80.         gbc.fill = GridBagConstraints.NONE;
  81.         gbc.insets = new Insets(0,0,0,0);
  82.         ((GridBagLayout)getLayout()).setConstraints(AnswerLabel, gbc);
  83.         add(AnswerLabel);
  84.         AnswerTextField = new java.awt.TextField(15);
  85.         AnswerTextField.setEditable(false);
  86.         AnswerTextField.setBounds(158,77,24,23);
  87.         AnswerTextField.setBackground(new Color(12632256));
  88.         gbc = new GridBagConstraints();
  89.         gbc.gridx = 1;
  90.         gbc.gridy = 2;
  91.         gbc.weightx = 1.0;
  92.         gbc.weighty = 1.0;
  93.         gbc.fill = GridBagConstraints.NONE;
  94.         gbc.insets = new Insets(0,0,0,0);
  95.         ((GridBagLayout)getLayout()).setConstraints(AnswerTextField, gbc);
  96.         add(AnswerTextField);
  97.         panel1 = new java.awt.Panel();
  98.         panel1.setLayout(new GridLayout(4,1,0,2));
  99.         panel1.setBounds(208,6,21,98);
  100.         gbc = new GridBagConstraints();
  101.         gbc.gridx = 3;
  102.         gbc.gridy = 0;
  103.         gbc.gridheight = 3;
  104.         gbc.weightx = 1.0;
  105.         gbc.weighty = 1.0;
  106.         gbc.anchor = GridBagConstraints.WEST;
  107.         gbc.fill = GridBagConstraints.NONE;
  108.         gbc.insets = new Insets(0,0,0,0);
  109.         ((GridBagLayout)getLayout()).setConstraints(panel1, gbc);
  110.         add(panel1);
  111.         PlusButton = new java.awt.Button();
  112.         PlusButton.setActionCommand("button");
  113.         PlusButton.setLabel("+");
  114.         PlusButton.setBounds(0,0,21,23);
  115.         panel1.add(PlusButton);
  116.         SubtractButton = new java.awt.Button();
  117.         SubtractButton.setActionCommand("button");
  118.         SubtractButton.setLabel("-");
  119.         SubtractButton.setBounds(0,25,21,23);
  120.         panel1.add(SubtractButton);
  121.         MultiplyButton = new java.awt.Button();
  122.         MultiplyButton.setActionCommand("button");
  123.         MultiplyButton.setLabel("X");
  124.         MultiplyButton.setBounds(0,50,21,23);
  125.         panel1.add(MultiplyButton);
  126.         DivideButton = new java.awt.Button();
  127.         DivideButton.setActionCommand("button");
  128.         DivideButton.setLabel("/");
  129.         DivideButton.setBounds(0,75,21,23);
  130.         panel1.add(DivideButton);
  131.         setTitle("Simple Calculation");
  132.         //}}
  133.  
  134.         //{{INIT_MENUS
  135.         //}}
  136.  
  137.         //{{REGISTER_LISTENERS
  138.         Window lWindow = new Window();
  139.         addWindowListener(lWindow);
  140.         Action lAction = new Action();
  141.         PlusButton.addActionListener(lAction);
  142.         SubtractButton.addActionListener(lAction);
  143.         MultiplyButton.addActionListener(lAction);
  144.         DivideButton.addActionListener(lAction);
  145.         //}}
  146.     }
  147.  
  148.     public CalculateFrame(String title)
  149.     {
  150.         this();
  151.         setTitle(title);
  152.     }
  153.  
  154.     public synchronized void show()
  155.     {
  156.         move(50, 50);
  157.         super.show();
  158.     }
  159.  
  160.     public void addNotify()
  161.     {
  162.         super.addNotify();
  163.  
  164.         if (fComponentsAdjusted)
  165.             return;
  166.  
  167.         // Adjust components according to the insets
  168.         setSize(insets().left + insets().right + getSize().width, insets().top + insets().bottom + getSize().height);
  169.         Component components[] = getComponents();
  170.         for (int i = 0; i < components.length; i++)
  171.         {
  172.             Point p = components[i].getLocation();
  173.             p.translate(insets().left, insets().top);
  174.             components[i].setLocation(p);
  175.         }
  176.         fComponentsAdjusted = true;
  177.     }
  178.  
  179.     boolean fComponentsAdjusted = false;
  180.     
  181.     //{{DECLARE_CONTROLS
  182.     java.awt.Label Number1Label;
  183.     java.awt.TextField Number1TextField;
  184.     java.awt.Label Number2Label;
  185.     java.awt.TextField Number2TextField;
  186.     java.awt.Label AnswerLabel;
  187.     java.awt.TextField AnswerTextField;
  188.     java.awt.Panel panel1;
  189.     java.awt.Button PlusButton;
  190.     java.awt.Button SubtractButton;
  191.     java.awt.Button MultiplyButton;
  192.     java.awt.Button DivideButton;
  193.     //}}
  194.  
  195.     //{{DECLARE_MENUS
  196.     //}}
  197.  
  198.     class Window extends java.awt.event.WindowAdapter
  199.     {
  200.         public void windowClosing(java.awt.event.WindowEvent event)
  201.         {
  202.             Object object = event.getSource();
  203.             if (object == CalculateFrame.this)
  204.                 Frame1_WindowClosing(event);
  205.         }
  206.     }
  207.  
  208.     class Action implements java.awt.event.ActionListener
  209.     {
  210.         public void actionPerformed(java.awt.event.ActionEvent event)
  211.         {
  212.             int i = 0;
  213.             Object object = event.getSource();
  214.             if (object == PlusButton)
  215.                 PlusButton_Action(event);
  216.             if (object == SubtractButton)
  217.                 SubtractButton_Action(event);
  218.             if (object == MultiplyButton)
  219.                 MultiplyButton_Action(event);
  220.             if (object == DivideButton)
  221.                 DivideButton_Action(event);
  222.         }
  223.     }
  224.  
  225.     void PlusButton_Action(java.awt.event.ActionEvent event)
  226.     {
  227.         double sum, num1, num2;
  228.  
  229.         try {
  230.             num1 = (Double.valueOf(Number1TextField.getText())).doubleValue();
  231.             num2 = (Double.valueOf(Number2TextField.getText())).doubleValue();
  232.         } catch (NumberFormatException e) { return; }
  233.  
  234.         sum = num1 + num2;
  235.  
  236.         if (sum < 0) { AnswerTextField.setBackground(Color.red);  }
  237.         else         { AnswerTextField.setBackground(Color.lightGray); }
  238.  
  239.         AnswerTextField.setText(String.valueOf(sum));
  240.     }
  241.     
  242.     void SubtractButton_Action(java.awt.event.ActionEvent event)
  243.     {
  244.         double diff, num1, num2;
  245.  
  246.         try {
  247.             num1 = (Double.valueOf(Number1TextField.getText())).doubleValue();
  248.             num2 = (Double.valueOf(Number2TextField.getText())).doubleValue();
  249.         } catch (NumberFormatException e) { return; }
  250.  
  251.         diff = num1 - num2;
  252.  
  253.         if (diff < 0) { AnswerTextField.setBackground(Color.red);  }
  254.         else          { AnswerTextField.setBackground(Color.gray); }
  255.  
  256.         AnswerTextField.setText(String.valueOf(diff));
  257.     }
  258.     
  259.     void MultiplyButton_Action(java.awt.event.ActionEvent event)
  260.     {
  261.         double product, num1, num2;
  262.  
  263.         try {
  264.             num1 = (Double.valueOf(Number1TextField.getText())).doubleValue();
  265.             num2 = (Double.valueOf(Number2TextField.getText())).doubleValue();
  266.         } catch (NumberFormatException e) { return; }
  267.  
  268.         product = num1 * num2;
  269.  
  270.         if (product < 0) { AnswerTextField.setBackground(Color.red);  }
  271.         else             { AnswerTextField.setBackground(Color.gray); }
  272.  
  273.         AnswerTextField.setText(String.valueOf(product));
  274.     }
  275.     
  276.     void DivideButton_Action(java.awt.event.ActionEvent event)
  277.     {
  278.         double quotient, num1, num2;
  279.  
  280.         try {
  281.             num1 = (Double.valueOf(Number1TextField.getText())).doubleValue();
  282.             num2 = (Double.valueOf(Number2TextField.getText())).doubleValue();
  283.         } catch (NumberFormatException e) { return; }
  284.  
  285.         if (num2 == 0) return;
  286.  
  287.         quotient = num1 / num2;
  288.  
  289.         if (quotient < 0) { AnswerTextField.setBackground(Color.red);  }
  290.         else              { AnswerTextField.setBackground(Color.gray); }
  291.  
  292.         AnswerTextField.setText(String.valueOf(quotient));
  293.     }
  294. }
  295.