Docstoc

NetBeans Tutorial

Document Sample
NetBeans Tutorial Powered By Docstoc
					a. Colors Bean Example

1.Creating a Bean

Let's begin with a Color Bean that has a method change() which will change the color whenever a button
actionPerformed event is generated.

Create a java file with the given source code below and name it as Colors. java.

import java.awt.*;

import java.awt.event.*;

import java.io.*;

public class Colors extends Canvas implements Serializable{

    private Color color;

    private boolean rect;

public Colors(){

    rect=false; setSize(200,100);

    change();

    }

public boolean getRect(){

            return rect;

}

public void setRect(boolean flag){this.rect=flag;
repaint();}

public void change(){ color = randomColor(); repaint();}

private Color randomColor(){

 int r=(int)(255*Math.random());

 int g =(int)(255*Math.random()); int
b=(int)(255*Math.random());

 return new Color(r,g,b);}
public void paint(Graphics g){ Dimension d = getSize();

                                                       int h=d.height; int
w=d.width; g.setColor(color);

                                                       if(rect){g.fillRect(0,0,w-
1,h-1);}

                                                       else{g.fillOval(0,0,w-1,h-
1);

                          }

                  }

       }



  2.       Compile the source code for the Bean and generate the Colors.class in the command prompt :

           javac Colors.java




  3.       Create a manifest file in a text editor.

        The manifest file specifies the name of the class file and indicates that it is a
        JavaBean. The manifest file becomes part of the JAR file. You can name the
        manifest file manifest.tmp. It contains the following two lines:

        Name: Colors.class
        Java-Bean: True



        (On Windows, be sure to include a carriage return at the end of the text in the
        manifest file.)

  4.       Create the executable JAR file.


        Use the form of the jar command to include the manifest file along with the
        Colors.class file (Type the command on one line):

        jar cvfm colors.jar manifest.tmp Colors.class



           NetBean 6.1 IDE For Colors JavaBean
        Click File | New Project to create a new project, a grouping of related files.
       Follow the wizard steps below.
           1.      Choose Project - General | Java Application
           2.      Name and Location - Uncheck Create Main Class, check Set as Main
              Project
        Click File | New File to create a new file. Follow the wizard steps below.
           1.      Choose File Type - Java GUI Forms | JApplet Form (from swing GUI
                   Forms)
           2.      Class Name – Use the Default class name

        Installing Counter JavaBeans - JavaBeans that have been jarr'ed can be
       downloaded and added to the Palette .
           o      Tools | Pallette ---Swing/AWT components
                  1.        Click Add from JAR
                  2.        Select JAR file - Locate and select colors.jar
                  3.        Select Components - Highlight Colors
                  4.        Select Palette Category - Highlight Beans
           o      Beans should now contain colors class
        Visual programming - Since applets are visual it is possible to program by
       dragging and dropping components from the toolbar.
           o      Click Design tab.
           o      To see the layout manager:
                           Right click in gray design area | Set Layout | Flow Layout
           o      To add a visual object, right click the gray area:
                           Add From Palette | Swing | JButton
                           Add From Palette | Beans | colors

Then the net beans IDE look like this:
Connecting the Button to the Colors Bean

NetBeans and other Java development environments can automatically generate the code
needed to connect events and listeners. First connect the JButton event actionPerformed
with the Colors change( ) method.

        Right click the jButton1. Select Events | Action | actionPerformed.
       The source code will be generated to call jButton1ActionPerformed method on a
       JButton event.

        Edit jButton1ActionPerformed, adding code to increment the counter:



private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
   colors1.change();
}



Running the Applet

        Shift+F6 to run the applet.
when clicked on the button the oval changes its color .




b. Counter Bean Example

1. Creating a Bean




The easiest way to learn to build a Bean is to start with a basic Bean, then add a property to it. Let's begin
with a Counter Bean that has a method increment() which increment a Counter bean whenever a button
actionPerformed event is generated.

Create a java file with the given source code below and name it as Counter.java.
import java.awt.*;



public class Counter extends Panel {

 private long count=0;

 private Label label;

 private long maxValue=20;

 public void setMaxValue(long max) {

             maxValue = max;

 }

 public long getMaxValue() { return maxValue; }

  public Counter() {

              setBackground(Color.blue);

              setForeground(Color.white);

              label = new Label(""+count);

              add(label);

  }

     public void increment () {

         if (count < maxValue) {

               count++;

               label.setText(count+" ");

         }

         else label.setText("!!");

     }
}



    2.    Compile the source code for the Bean and generate the Counter.class in the command prompt :

         javac Counter.java

    3.   Create a manifest file in a text editor.

         The manifest file specifies the name of the class file and indicates that it is a
         JavaBean. The manifest file becomes part of the JAR file. You can name the
         manifest file manifest.man. It contains the following two lines:

         Name: Counter.class
         Java-Bean: True



         (On Windows, be sure to include a carriage return at the end of the text in the
         manifest file.)

    4.   Create the executable JAR file.


         Use the form of the jar command to include the manifest file along with the
         Counter.class file (Type the command on one line):

         jar cfm counterEvent.jar manifest.man Counter.class




         NetBean 6.1 IDE For Counter JavaBean

          Click File | New Project to create a new project, a grouping of related files.
         Follow the wizard steps below.
             1.      Choose Project - General | Java Application
             2.      Name and Location - Uncheck Create Main Class, check Set as Main
                Project
          Click File | New File to create a new file. Follow the wizard steps below.
             1.      Choose File Type - Java GUI Forms | JApplet Form
             2.      Class Name - Use Default

          Installing Counter JavaBeans - JavaBeans that have been jarr'ed can be
         downloaded and added to the Palette .
             o      Tools | Pallette Manager
                    1.        Click Add from JAR
                    2.        Select JAR file - Locate and select counterEvent.jar
                    3.        Select Components - Highlight Counter
                    4.        Select Palette Category - Highlight Beans
           o      Beans should now contain Counter class
        Visual programming - Since applets are visual it is possible to program by
       dragging and dropping components from the toolbar.
          o       Click Design tab.
          o       To see the layout manager:
                          Right click in gray design area | Set Layout | Flow Layout
          o       To add a visual object, right click the gray area:
                          Add From Palette | Swing | JButton
                          Add From Palette | Beans | Counter

Then the net beans IDE look like this:




Connecting the Button to the Counter Bean

Previous examples illustrated how events generated by clicking the mouse could be
connected to a listener method by manually writing code such as:

       button.addActionListener(new ActionListener( ) {
            public void actionPerformed(ActionEvent e) {
              counter.increment();
            }
          }
       );
NetBeans and other Java development environments can automatically generate the code
needed to connect events and listeners. First connect the JButton event actionPerformed
with the Counter increment( ) method.

        Right click the jButton1. Select Events | Action | actionPerformed.
       The source code will be generated to call jButton1ActionPerformed method on a
       JButton event.

        Edit jButton1ActionPerformed, adding code to increment the counter:



private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
   counter1.increment();
}



Running the Applet

        Shift+F6 to run the applet.




    Label shows the number of times the button is clicked .

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:7
posted:10/18/2012
language:English
pages:9