Posted By:
20 Feb, 2014 11:58 am PST

How to create checkboxes and scrollbar in Java

Write a program to create a vertical scrollbar with scroll button length 30 px and with starting and ending positions ranging from 0 to 400px.

//creating a vertical scrollbar

import java.awt.*;

import java.awt.event.*;

public class MyScroll extends Frame implements AdjustmentListener{

                String msg=" ";

                Scrollbar s1;

                MyScroll()

                {

                                //do not set any layout

                                setLayout(null);

                               //create a vertical scrollbar

                                s1=new Scrollbar(Scrollbar.VERTICAL,0,30,0,400);

                                //set the location of scrollbar in a frame

                                s1.setBounds(250,50,30,200);

                                //add it to frame

                                add(s1);

                                //add adjustment listener to scrollbar

                                s1.addAdjustmentListener(this);

                                //frame closing

                                addWindowListener (new WindowAdapter()

                                {

                                                public void windowClosing(WindowEvent we)

                                                {System.exit(0);

                                                }

                                                });

                                }

public void adjustmentValueChanged(AdjustmentEvent ae)

{

                repaint();  //call paint()

}

public void paint(Graphics g)

{

                //display the position of the scrollbar

                g.drawString("SCROLLBAR POSITION: ", 20, 150);

                msg +=s1.getValue();

                g.drawString(msg, 20, 180);

                msg="";

                }

public static void main(String args[])

{

                //create the frame

                MyScroll v=new MyScroll();

                v.setTitle("My Scroll Bar");

                v.setSize(400,400);

                v.setVisible(true);

                }

}

OUTPUT:

           

Write a program to create 3 check boxes to display Bold, Italic, and Underline to the user.

//checkbox demo

import java.awt.*;

import java.awt.event.*;

public class MyCheckbox extends Frame implements ItemListener {

                String msg="";

                Checkbox c1,c2,c3;

                MyCheckbox()

                {

                                //set flow layout manager

                                setLayout(new FlowLayout());

                //display 3 checkboxes

                c1=new Checkbox("Bold", true);

                c2=new Checkbox("Italic");

                c3=new Checkbox("UnderLine");

                //add the checkboxes to frame

                add(c1);

                add(c2);

                add(c3);

                //add item listener to the check boxes

                c1.addItemListener(this);

                c2.addItemListener(this);

                c3.addItemListener(this);

                //close the frame

                addWindowListener(new WindowAdapter()

                {

                                public void windowClosing(WindowEvent we)

                                {

                                                System.exit(0);

                                }

                                });

                }// end of constructor

                //this method is called when the user clicks on a check box

                public void itemStateChanged(ItemEvent ie)

                {

                                repaint(); //calls paint() method

                }

//display current state of checkboxes

                public void paint(Graphics g)

                {             

                                g.drawString("Current State", 10, 100);

                                msg+="Bold: "+c1.getState();

                                g.drawString(msg, 10, 120);

                                msg="";

                                msg+="Italic: " +  c2.getState();

                                g.drawString(msg, 10, 140);

                                msg="";

                                msg+="UnderLine: " + c3.getState();

                                g.drawString(msg, 10, 160);

                                msg="";

                }

                public static void main(String [] args)

                {

                                //create the frame

                                MyCheckbox n=new MyCheckbox();

                                n.setVisible(true);;

                                n.setTitle("My Check boxes");

                                n.setSize(500,550);

                                }

}

OUTPUT:


Attachment: workspace.zip


setLayoutA layout manager is an object that implements the LayoutManager interface  and determines the size and position of the components within a container

Label classA Label object is a component for placing text in a container. A label displays a single line of read-only text. The text can be changed by the application, but a user cannot edit it directly

public class Label

extends Component

implements Accessible

Label(String text, int alignment):Constructs a new label that presents the specified string of text with the specified alignment.

Scrollbar class: The Scrollbar class embodies a scroll bar, a familiar user-interface object. A scroll bar provides a convenient means for allowing a user to select from a range of values.

add(Popupmenu Popup): Adds the specified popup menu to the component.

addAdjustmentListener(thisThe listener interface for receiving adjustment events. this in the method takes the current class object as a parameter. It will invoke voidadjustmentValueChanged(AdjustmentEvent ae)

WindowListener interface: The listener interface for receiving window events. The class that is interested in processing a window event either implements this interface (and all the methods it contains) or extends the abstract WindowAdapter class (overriding only the methods of interest). The listener object created from that class is then registered with a Window using the window's addWindowListener method. When the window's status changes by virtue of being opened, closed, activated or deactivated, iconified or deiconified, the relevant method in the listener object is invoked, and the WindowEvent is passed to it.

Attachment: MyScroll.java
Was it useful?  




Please login to reply to this problem.



Related Problems on Fixoncloud