How to perform Calculation when a button a pressed in a Frame


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: How to perform Calculation when a button a pressed in a Frame

  1. #1
    Join Date
    Jan 2005
    Posts
    1

    How to perform Calculation when a button a pressed in a Frame

    Hi friends,

    I have created a Frame which has some checkboxes , a button and text field. when i check some checkboxes and press the Button (ie in my program Metrics level Button ) it should display the result as number of checkboxes that are checked divided by total number of chechboxes. ie if i check some 6 check boxes and press the Metrics level button it should display 6 divided by 12 ie 0.5 in the Result Textfield.

    I am sending the code i have written.

    Thanks in advance.



    public class Frame extends java.awt.Frame {

    /** Creates new form Frame */
    public Frame() {
    initComponents();
    setSize(800, 800);
    }

    private void initComponents() {
    label1 = new java.awt.Label();
    checkbox1 = new java.awt.Checkbox();
    checkbox2 = new java.awt.Checkbox();
    checkbox3 = new java.awt.Checkbox();
    checkbox4 = new java.awt.Checkbox();
    checkbox5 = new java.awt.Checkbox();
    checkbox6 = new java.awt.Checkbox();
    checkbox7 = new java.awt.Checkbox();
    checkbox8 = new java.awt.Checkbox();
    checkbox9 = new java.awt.Checkbox();
    checkbox10 = new java.awt.Checkbox();
    checkbox11 = new java.awt.Checkbox();
    checkbox12 = new java.awt.Checkbox();
    button1 = new java.awt.Button();
    textField1 = new java.awt.TextField();

    setLayout(null);

    addWindowListener(new java.awt.event.WindowAdapter() {
    public void windowClosing(java.awt.event.WindowEvent evt) {
    exitForm(evt);
    }
    });

    label1.setText("Select the Metrics below");
    add(label1);
    label1.setBounds(320, 20, 136, 20);

    checkbox1.setLabel("Architecture Metrics");
    add(checkbox1);
    checkbox1.setBounds(240, 80, 84, 20);

    checkbox2.setLabel("Runtime Metrics");
    add(checkbox2);
    checkbox2.setBounds(240, 200, 115, 20);

    checkbox3.setLabel("Documentation Metrics");
    add(checkbox3);
    checkbox3.setBounds(240, 320, 152, 20);

    checkbox4.setLabel("Size");
    add(checkbox4);
    checkbox4.setBounds(280, 110, 49, 20);

    checkbox5.setLabel("Structure");
    add(checkbox5);
    checkbox5.setBounds(280, 130, 75, 20);

    checkbox6.setLabel("Complexity");
    add(checkbox6);
    checkbox6.setBounds(280, 150, 86, 20);

    checkbox7.setLabel("Size");
    add(checkbox7);
    checkbox7.setBounds(290, 230, 49, 20);

    checkbox8.setLabel("Structure");
    add(checkbox8);
    checkbox8.setBounds(290, 250, 75, 20);

    checkbox9.setLabel("Complexity");
    add(checkbox9);
    checkbox9.setBounds(290, 270, 86, 20);

    checkbox10.setLabel("Size");
    add(checkbox10);
    checkbox10.setBounds(300, 350, 49, 20);

    checkbox11.setLabel("Structure");
    add(checkbox11);
    checkbox11.setBounds(300, 370, 75, 20);

    checkbox12.setLabel("Complexity");
    add(checkbox12);
    checkbox12.setBounds(300, 390, 86, 20);

    button1.setLabel("Metrics level");
    add(button1);
    button1.setBounds(290, 470, 83, 24);

    textField1.setText("Result");
    textField1.setName("Result");
    add(textField1);
    textField1.setBounds(400, 470, 60, 20);

    pack();
    }
    public void actionPerformed(ActionEvent e) {

    ****** I think code should be added here for the button pressed event*******
    }


    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {
    System.exit(0);
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
    new Frame().show();
    }


    // Variables declaration - do not modify
    private java.awt.Button button1;
    private java.awt.Checkbox checkbox1;
    private java.awt.Checkbox checkbox10;
    private java.awt.Checkbox checkbox11;
    private java.awt.Checkbox checkbox12;
    private java.awt.Checkbox checkbox2;
    private java.awt.Checkbox checkbox3;
    private java.awt.Checkbox checkbox4;
    private java.awt.Checkbox checkbox5;
    private java.awt.Checkbox checkbox6;
    private java.awt.Checkbox checkbox7;
    private java.awt.Checkbox checkbox8;
    private java.awt.Checkbox checkbox9;
    private java.awt.Label label1;
    private java.awt.TextField textField1;
    // End of variables declaration

    }

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Hey !, Don't extend a class with new a class w. the
    same name


    If you have an actionPerformed() method you should
    also implement the ActionListener interface for your frame:
    Code:
    public class MyFrame extends java.awt.Frame implements ActionListener {
    Then you must tell the buttons that somebody has any
    interesest in their clicks, eg your Frame:

    aButton.addActionListener(this);

    Then you implement the actionPerformed() method as you described:

    Code:
    public void actionPerformed (ActionEvent ae) {
      if (ae.getSource()==aButton) {
      } else  (etc.)
      .
      .
    }
    eschew obfuscation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center