Hello,
I am creating simple an applet to draw a registration form based on the selected item within the awt choice object.
I have include a radio button, choice list and button.
My problem is when running the Applet...the choice list is blinking and when I maximized or minimise frame the component will be repeatedly to whole frame...

Can someone please help explain how exactly to create a button, choice and radio button?

Thanks :)

import java.awt.*;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Choice;
import java.awt.event.*;
import java.applet.Applet;


public class Student extends Applet implements ItemListener {


    public void paint(Graphics g)
    {
		LayoutManager Layout;
    	Layout = new FlowLayout();
    	setLayout(Layout);
    	super.paint( g );

		//*************************************************************
    	//Straight Line
    	g.drawLine(30,30,750,30); //upper (x1,y1,x2,y2)
      	g.drawLine(30,110,750,110); //header

      	g.drawLine(30,30,30,670); //left
      	g.drawLine(750,30,750,670); //right
      	g.drawLine(30,480,750,480); //center
      	g.drawLine(30,510,750,510); //center2
      	g.drawLine(30,670,750,670); //bottom

		//*************************************************************
		//Header
      	g.setFont(new Font("Tahoma",Font.BOLD,32));
    	g.drawString("Student Registration Form",190,80);

    	g.setFont(new Font("Tahoma",Font.BOLD,22));
    	g.drawString("Family Details",35,505);

		//*************************************************************
		//On Left Side(change y(+40))
    	g.setFont(new Font("Arial",Font.PLAIN,16)); //Name
    	g.drawString("Name :",50,150);

    	g.setFont(new Font("Arial",Font.PLAIN,16));	//IC
    	g.drawString("No. IC :",50,190);
    	g.setFont(new Font("Arial",Font.BOLD,10));
    	g.drawString("(900209-01-6537)",285,195);

    	g.setFont(new Font("Arial",Font.PLAIN,16));	//Age
    	g.drawString("Age :",50,230);
    	//g.setFont(new Font("Arial",Font.BOLD,16));
    	//g.drawString("years",175,233);

    	g.setFont(new Font("Arial",Font.PLAIN,16));	//DOB
    	g.drawString("Date of Birth :",50,270);

    	g.setFont(new Font("Arial",Font.PLAIN,16));	//POB
    	g.drawString("Place of Birth :",50,310);

    	g.setFont(new Font("Arial",Font.PLAIN,16));	//Race
    	g.drawString("Race :",50,350);

    	g.setFont(new Font("Arial",Font.PLAIN,16));	//Gender
    	g.drawString("Gender :",50,390);


		//*************************************************************
    	//On Right Side
    	g.setFont(new Font("Arial",Font.PLAIN,16));	//Address
    	g.drawString("Address :",390,150);

    	g.setFont(new Font("Arial",Font.PLAIN,16));	//Postcode
    	g.drawString("Postcode :",390,190);

    	g.setFont(new Font("Arial",Font.PLAIN,16));	//State
    	g.drawString("State :",390,230);

    	g.setFont(new Font("Arial",Font.PLAIN,16));	//Phone(H)
    	g.drawString("Telephone(h) :",390,270);

    	g.setFont(new Font("Arial",Font.PLAIN,16));	//Phone(M)
    	g.drawString("Telephone(m) :",390,310);

    	g.setFont(new Font("Arial",Font.PLAIN,16));	//Course
    	g.drawString("Course :",390,355);

 		//*************************************************************
 		//Family Details
 		g.setFont(new Font("Arial",Font.PLAIN,16));	//Father
    	g.drawString("Father Name :",50,550);
    	g.setFont(new Font("Arial",Font.PLAIN,16));	//Occupation
    	g.drawString("Occupation :",50,590);

    	g.setFont(new Font("Arial",Font.PLAIN,16));	//Mother
    	g.drawString("Mother Name :",390,550);
    	g.setFont(new Font("Arial",Font.PLAIN,16));	//Occupation
    	g.drawString("Occupation :",390,590);


    	//*************************************************************
    	//Rectangle TextBox (change y1(+40))
    	g.drawRect(120,133,245,25); //Name (x1,y1,width,height)
    	g.drawRect(120,173,160,25); //IC
    	g.drawRect(155,213,50,25); // Age
    	g.drawRect(155,253,190,25); // DOB
    	g.drawRect(155,293,190,25); // POB
    	//g.drawRect(155,333,190,25); // Race

    	g.setFont(new Font("Arial",Font.PLAIN,16)); //Name
    	g.drawString("Nuramira binti Ibrahim",123,153);

    	g.drawRect(500,133,225,25); // Address
    	g.drawRect(500,173,80,25); // Postcode
    	g.drawRect(500,210,190,25);	// State
    	g.drawRect(500,253,205,25); // Tel(h)
    	g.drawRect(500,293,205,25); // Tel(p)

    	g.drawRect(155,535,210,25); // Father
    	g.drawRect(155,575,210,25); // Position

    	g.drawRect(495,535,215,25); // Mother
    	g.drawRect(495,575,215,25); // Position

		//*************************************************************
		//Button
		Button regButton, resetButton;  //declaration

		regButton = new Button("Register");
    	regButton.setBounds(310,640,70,25); // x,y,width,height
    	add(regButton);

    	resetButton = new Button("Reset");
    	resetButton.setBounds(390,640,70,25); // x,y,width,height
    	add(resetButton);


    	//*************************************************************
    	//Choice Race
    	Choice race = new Choice();  //declaration + constructor
    	race.add("Malay");
    	race.add("Chinesse");
    	race.add("Indian");

    	race.setBounds(155,335,190,25); // x,y,width,height
    	add(race);
    	race.addItemListener(this);

    	//Choice Course
		Choice course;
    	course = new Choice();  //declaration + constructor
    	course.addItem("JTMK");
    	course.addItem("JKE");
    	course.addItem("JKA");
    	course.addItem("JKM");
    	course.addItem("Perd");

    	course.setBounds(500,340,60,25);
    	add(course);
    	Choice.addItemListener(this);

    	//*************************************************************
		//radio button
     	CheckboxGroup radioGroup; 	// necessary to only allow one radio button to be selected at the same time.
		Checkbox radioM, radioF;	// declaration
		radioGroup = new CheckboxGroup();
		radioM = new Checkbox("Male", radioGroup,false);
        radioF = new Checkbox("Female", radioGroup,true);

        radioM.setBounds(155,375,50,30);
        radioF.setBounds(215,375,70,30);
        add(radioM);
        add(radioF);
        radioM.addItemListener(this);
        radioF.addItemListener(this);
    }
}

The first thing I'm noticing is that all your code is en the paint method. This method should only be overriden if you want to do custom painting like the painting you are doing in the beginning of the method. You should not add swing components to layouts etc. in this method. You should do it once when the program is started.

So move out the layout code for the radiobuttons and other swing components to another method that is called once by the constructor or something. The layout of the components will conform to their parent containers depending on what layout manager you use.

The paint(Graphics g) method will be called constantly while you are resizing a window or anytime anything needs to be painted onto the screen. So if you add new components for each time the method is called, you will get a hell of a lot buttons. And will probably be out of memory pretty fast.

On another note, you should preferrably override paintComponent(Graphics g) instead of paint(Graphics g). paint paints borders and children and stuff, and is calling paintComponent, while paintComponent is only responsible for painting itself.

May i know which one is sing component and how to crete to another method...im still new in java

SasseMan is right.

The only code in your paint method should be actual painting code - roughly speaking that's just the lines that use the Graphics object g, as in super(g) or g.draw...
All the stuff about layout manager or adding components (choice, button, radio group, etc) belong in your initialisation code so they are executed just once. The easy way to do that is to implement your own public void init() method and put the code in there. It will be called automatically when your applet is loaded.

SasseMan refers to "Swing". When Java was first released (last century) it had a GUI system called AWT. When people used that they found all kinds of problems with how it was designed, so Sun had a second try and released Swing, an updated version of AWT that fixed many problems. You can tell which is which because the old AWT classes have names like Applet, Button etc, and the replacement Swing classes all have names that begin with a J, eg JApplet, JButton etc. Your code uses the old AWT classes, but in general you should always use the newer Swing classes.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.