Friday, October 15, 2010

Counter-simple Java programm [Open Source]

Hello,
I wrote a code today, I was trying to do something I never did. Well, it's not that impressive but I'm happy I did it myself XD

take a look:

//October 15 2010 Friday
//5:26 pm
//by:Liberty
//www.hctlife.com
/* **************** */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class test extends JFrame {
    private JButton button;
    private JLabel label;
    int i=0;
   
    public test(){
        super("Counter");
       
        Container container=getContentPane();
        container.setLayout(new FlowLayout() );
       
        button=new JButton("Click");
        container.add(button);
       
        label=new JLabel("count : 0");
        container.add(label);
       
        ButtonHandler handler=new ButtonHandler();
        button.addActionListener(handler);
       
        setSize(200,70);
        setVisible(true);
        setLocation(100,100);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
   
    public static void main (String args []){
        test app=new test();
    }
   
    private class ButtonHandler implements ActionListener{
        public void actionPerformed (ActionEvent event){
            i++;
            label.setText("count : "+ i);
       
        }
    }
}

That's it.

4 comments:

  1. may i disturb u???!!!
    1. why ur codes are always written without the comments?
    2. The name of the variables should be logic, right?

    1 mark at least will be deducted from ur result :p

    ReplyDelete
  2. that's ok Amase, you're welcome.

    1. i do write comments, but that's not always. it depends on the code and where will i show it.

    2. no, that's wrong. only classes should be the same name.

    XDD

    you are very welcome Amase :)
    thanks

    ReplyDelete
  3. hehehe :D thanx for answering

    1. i advise u to write the comments in whatever programs u are writing... even if it's only to print "Hello"... and i'm so serious, specially that u r still a student..
    Do u know programmers are evaluated based on many criteria, a critical one among those criteria is: either their codes include comments or not!! it's true

    2. i will clarify my point with an example:
    "private JButton button;"
    'button' is what i meant
    usually it shouldn't be a general object name, it should specify which button it's referring to such as 'disButton' or 'addButton'
    coz in future if u return to ur code, it will be much easier to track whatever u want by the name of the objects....
    in fact our instructors deduct half mark from us if we didn't name the objects and variables uniquely

    hey by the way these r only enhancements not bugs loooool... i.e. u can ignore them off-course
    just i like to give some advises to students

    cheers and wish u the best... keep it up gal

    ReplyDelete
  4. hey Amase :)
    1. ummm may be you're right. i'll put your advice in mind.

    2. yes that's it. thank you for clarifying.

    you're welcome anytime, give us more advices in future XD, cause we need that :)

    ReplyDelete