0% found this document useful (0 votes)
3 views17 pages

Unit Vi Java Notes

The document provides an overview of the Abstract Window Toolkit (AWT) in Java, detailing its components, containers, and layout management for creating graphical user interfaces. It explains the platform-dependent nature of AWT and contrasts it with the more modern, platform-independent Swing API. Additionally, it describes various AWT components such as buttons, labels, checkboxes, and containers like windows and frames, along with examples of their usage.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views17 pages

Unit Vi Java Notes

The document provides an overview of the Abstract Window Toolkit (AWT) in Java, detailing its components, containers, and layout management for creating graphical user interfaces. It explains the platform-dependent nature of AWT and contrasts it with the more modern, platform-independent Swing API. Additionally, it describes various AWT components such as buttons, labels, checkboxes, and containers like windows and frames, along with examples of their usage.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

UNIT VI

Abstract Window Toolkit


AWT: introduction, components and containers, Button, Label, Checkbox, Radio Buttons, List
Boxes, Choice Boxes, Container class, Layouts, Menu and Scrollbar.

AWT
AWT stands for Abstract Window Toolkit. It is a platform dependent API for creating Graphical
User Interface (GUI) objects such as buttons, scroll bars, and windows. AWT is part of the Java

TS
Foundation Classes ( JFC ) from Sun Microsystems.
Why AWT is platform dependent?
Java AWT calls native platform (Operating systems) subroutine for creating components
such as textbox, checkbox, button etc. For example an AWT GUI having a button would have a
different look and feel across platforms like windows, Mac OS & Unix, this is because these
platforms have different look and feel for their native buttons and AWT directly calls their
native subroutine that creates the button. In simple, an application build on AWT would look
like a windows application when it runs on Windows, but the same application would look like a
Mac application when runs on Mac OS.
AWT is rarely used now days because of its platform dependent and heavy-weight
nature. AWT components are considered heavy weight because they are being generated by
underlying operating system (OS). For example if you are instantiating a text box in AWT that

CI
means you are actually asking OS to create a text box for you.
Swing is a preferred API for window based applications because of its platform
independent and light-weight nature. Swing is built upon AWT API however it provides a look
and feel unrelated to the underlying platform. It has more powerful and flexible components
than AWT.
Examples of GUI based Applications
Following are some of the examples for GUI based applications.
• Automated Teller Machine (ATM)
• Airline Ticketing System
BV
• Information Kiosks at railway stations
• Mobile Applications
• Navigation Systems
[Link] package
The [Link] package provides classes for AWT api such as TextField, Label, TextArea,
RadioButton, CheckBox, Choice, List etc. The following diagram shows various classes defined in
awt package.
Java AWT Hierarchy
The hierarchies of Java AWT classes are given below.

Page No 1
Prepared By,
[Link],
Training and Placement Officer, BVCITS
TS
CI Figure: AWT hierarchy
COMPONENTS AND CONTAINERS
All the elements like buttons, text fields, scrollbars etc are known as components. In
AWT we have classes for each component as shown in the above diagram. To have everything
placed on a screen to a particular position, we have to add them to a container. A container is
like a screen wherein we are placing components like buttons, text fields, checkbox etc. In short
a container contains and controls the layout of components. A container itself is a component
(shown in the above hierarchy diagram) thus we can add a container inside container.
BV
Types of containers:
As explained above, a container is a place wherein we add components like text field,
button, checkbox etc. There are four types of containers available in AWT: Window, Frame,
Dialog and Panel. As shown in the hierarchy diagram above, Frame and Dialog are subclasses of
Window class.
1. Window: The window is the container that has no borders and menu bars. You
must use frame, dialog or another window for creating a window.
2. Frame: The Frame is the container that contain title bar and can have menu bars.
It can have other components like button, textfield etc.
3. Dialog: A Dialog control represents a top-level window with a title and a border
used to take some form of input from the user.
4. Panel: The Panel is the container that doesn't contain title bar and menu bars. It
can have other components like button, textfield etc.
Components:
Components are generally the stuff that the user interacts with.
Following are the list of common Components.
1. Label: A Label object is a component for placing text in a container.
Page No 2
Prepared By,
[Link],
Training and Placement Officer, BVCITS
import [Link];
import [Link];
/*
<applet code="CreateLabel" width=200 height=200>
</applet>
*/
public class CreateLabel extends Applet
{
public void init()

TS
{
Label ob1 = new Label();
[Link]("Username");
Label ob2 = new Label("My Label 2");
add(ob1);
add(ob2);
}
}
2. Button: This class creates a labeled button.
import [Link];
import [Link].*;
import [Link].*;

CI/*
<applet code="HandleActionEventExample" width=200 height=200>
</applet>
*/
public class HandleActionEventExample extends Applet implements ActionListener{
String message="";
public void init()
{
//create Buttons
Button b1 = new Button("Ok");
BV
Button b2 = new Button("Cancel");
//add Buttons
add(b1);
add(b2);
//set action listeners for buttons
[Link](this);
[Link](this);
}
public void paint(Graphics g){
[Link](message,10,50);
}
public void actionPerformed(ActionEvent ae){
String action = [Link]();
if([Link]("Ok"))
message = "Ok Button Pressed";
else if([Link]("Cancel"))

Page No 3
Prepared By,
[Link],
Training and Placement Officer, BVCITS
message = "Cancel Button Pressed";
repaint();
}
}
3. Checkbox: A check box is a graphical component that can be in either an on (true) or off
(false) state.
import [Link];
import [Link];
/*

TS
<applet code="CreateCheckBox" width=200 height=200>
</applet>
*/
public class CreateCheckBox extends Applet{
public void init(){
Checkbox cb1 = new Checkbox("My Checkbox 1");
Checkbox cb2 = new Checkbox("My Checkbox 2", true);
add(cb1);
add(cb2);
}
}
4. CheckboxGroup: The CheckboxGroup class is used to group the set of checkbox, that means

CI
it is used to create RadioButtions.
import [Link];
import [Link];
import [Link];
/*
<applet code="CreateRadioButtonsExample" width=200 height=200>
</applet>
*/
public class CreateRadioButtonsExample extends Applet{
public void init(){
BV
CheckboxGroup c = new CheckboxGroup();
//if you create checkboxes and add to group,they become radio buttons
Checkbox ja = new Checkbox("Java", c, true);
Checkbox cb = new Checkbox("C++", c, true);
Checkbox vb = new Checkbox("VB", c, true);
add(ja);
add(cb);
add(vb);
}
}
i. What is Radio button?
Infact, there is no radio button class in Java. Radio buttons are nothing but checkboxes only but
grouped as one unit.
ii. What the advantage of grouping checkboxes?
Once grouped, in the group if one checkbox is selected, the other gets automatically
deselected. This is the only advantage. Sometimes, it is required to make user-friendly code.

Page No 4
Prepared By,
[Link],
Training and Placement Officer, BVCITS
iii. How to group checkboxes?
To group checkboxes as one unit, there comes a Java class CheckboxGroup
5. List: The List component presents the user with a scrolling list of text items.
import [Link];
import [Link];
/*
<applet code="CreateListExample" width=200 height=200>
</applet>
*/

TS
public class CreateListExample extends Applet{
public void init(){
//this list will have 5 visible rows
List l = new List(5);
[Link]("One");
[Link]("Two");
[Link]("Three");
[Link]("Four");
[Link]("Five");
[Link]("Six");
[Link]("Seven");
add(l);

} CI
text.
}

6. TextField: A TextField object is a text component that allows for the editing of a single line of

import [Link].*;
import [Link].*;
/*
<applet code="TextFieldExample" width=400 height=400>
</applet>
*/
BV
public class TextFieldExample extends Applet
{
public void init()
{
TextField t1 = new TextField(20);
TextField t2 = new TextField("kumar", 20);
add(t1);
add(t2);
}
}
7. TextArea: A TextArea is a text component that allows for the editing of a multiple lines of
text. It is used for storing address like fields.
import [Link].*;
import [Link].*;
/*
<applet code="TextAreaExample" width=400 height=400>

Page No 5
Prepared By,
[Link],
Training and Placement Officer, BVCITS
</applet>
*/
public class TextAreaExample extends Applet
{
public void init()
{
TextArea t1 = new TextArea(12,30);
add(t1);
}

TS
}
8. Choice Boxes : A Choice control is used to show popup menu of choices. Selected choice is
shown on the top of the menu.
import [Link];
import [Link];
/*
<applet code="CreateChoiceExample" width=200 height=200>
</applet>
*/
public class CreateChoiceExample extends Applet{
public void init(){
Choice lang = new Choice();

}
CI }
[Link]("Java");
[Link]("C++");
[Link]("VB");
[Link]("Perl");
//add choice or combobox
add(lang);

9. Canvas : A Canvas control represents a rectangular area where application can draw
something or can receive inputs created by user.
BV
10. Image: An Image control is super class for all image classes representing graphical images.
11. Scrollbar: A Scrollbar control represents a scroll bar component in order to enable user to
select from range of values.
import [Link].*;
import [Link].*;
import [Link].*;
/*<applet code="Scroll" width=300 height=300>
</applet>*/
public class Scroll extends Applet {
private Scrollbar sb;
public void init() {
sb = new Scrollbar([Link], 10,100, 1, 100);
[Link](sb);
}
}
Note: Every AWT controls inherit properties from Component class.

Page No 6
Prepared By,
[Link],
Training and Placement Officer, BVCITS
CONTAINER CLASSES
Window, Frame, Dialog, Applet and Panel are the classes of Container.
1. Window:
II. The window class of a container does not contain any borders & menubar and is a top-
level window.
III. As Window class extends from the Container class, other components like textfields, and
buttons can be added to a window. These components can be arranged by using a
LayoutManager. The default LayoutManager of Window class is BorderLayout.

TS
Declaration : [Link] class can be declared as follows:
public class Window extends Container implements Accessible
2. Frame:
Frame is considered as a window with a menu bar, borders and titles in real-time environment.
A frame can be modified as per the requirement. The default LayoutManager is BorderLayout.
Frames can divide the data into different windows.
Declaration :
[Link] class can be declared as follows:
public class Frame extends Window implements MenuContainer
To create a frame without a title bar, use Frame() constructor that does not have any
[Link] f = new Frame();
Window title can be declared using strings as below:

CI
Frame f = new Frame("Window Title");
A component can be added to a frame by using add() method. For example,
Frame f = new Frame("Example Window");
[Link](new Button("Correct");
Otherwise this can be used, if present inside the class.
this.f(new Button("Correct");
A Frame can be given a size using the setsize() method. For example,
[Link](150,100);
After adding all the required components , it must be made visible as it is initially set to
invisible. This can be done by using setVisible() method. For example,
BV
[Link](true);
Otherwise if one wants the exact size, use pack() method.
3. Dialog : Dialog is considered as a window without having a menu bar. This sub class of
window will help in taking inputs from the user and also gives alerts to the user. Dialog has a
parent frame. If the parent frame is closed, the dialog frame also gets closed.
A Modal Dialog does not allow the user to interact with the application until there is
some input action. It blocks all other windows of that application till it gets desired input. It is
similar to the cookies. Non-Modal Dialog outputs a pop-up, however, it will allow the user to
open other windows and communicate with the application.
Declaration : [Link] class can be declared as follows:
public class Dialog extends Window
Example for Dialog,Frame,Window and Menu :
import [Link].*;
public class Container extends Frame
{
Dialog dialog;
Page No 7
Prepared By,
[Link],
Training and Placement Officer, BVCITS
public static void main (String args [ ])
{
Container win = new Container();
}
public Container ( )
{
super ("Container");
resize (400,400);
addMenus();

TS
createDialog();
show();
}
void addMenus()
{
MenuBar menubar = new MenuBar();
Menu file = new Menu ("File");
Menu dialog = new Menu ("Dialog");
[Link] ("Quit");
[Link]("Show");
[Link]("Hide");
[Link] (file);

}
CI [Link](dialog);
setMenuBar (menubar);

void createDialog ( )
{

}
dialog = new Dialog (this, "Dialog Box", false);
[Link] (200,200);

public boolean handleEvent(Event event)


{
BV
if ([Link] ==Event.WINDOW_DESTROY)
{
[Link] (0);
return true;
}
else if ([Link] == Event.ACTION_EVENT && [Link] instanceof MenuItem)
{
if ("Quit".equals ([Link]) )
{
[Link] (0);
return true;
}
else if ("Show".equals ([Link] ) )
{
[Link] ( );
return true;

Page No 8
Prepared By,
[Link],
Training and Placement Officer, BVCITS
}
else
{
[Link] ();
return true;
}
}
else
return false;

TS
}
}
4. Panel: Panel is an area containing components, which can be divided further into rectangular
pieces and contains other panels if required.
Panel will have LayoutManager explicitly, which helps in performing other operations
that may become burden to the single LayoutManager. The default LayoutManager is
FlowLayout.
Declaration : [Link] class can be declared as follows:
public class Panel extends Container implements Accessible
Ex:
import [Link].*;
import [Link].*;

CI
public class Example extends Applet {
public void init() {
[Link](new BorderLayout());
[Link]([Link], new TextArea());
Panel p = new Panel();
[Link](new FlowLayout([Link]));
[Link](new Button("OK"));
[Link]([Link], p);
}
}
BV
LAYOUTS
Layout means the arrangement of components within the container. In other way we can say
that placing the components at a particular position within the container. The task of layouting
the controls is done automatically by the Layout Manager.
Layout Manager
The layout manager automatically positions all the components within the container. If we do
not use layout manager then also the components are positioned by the default layout
manager. It is possible to layout the controls by hand but it becomes very difficult because of
the following two reasons.
• It is very tedious to handle a large number of controls within the container.
• Oftenly the width and height information of a component is not given when we need to
arrange them.
Java provide us with various layout manager to position the controls. The properties like
size,shape and arrangement varies from one layout manager to other layout manager. When
the size of the applet or the application window changes the size, shape and arrangement of

Page No 9
Prepared By,
[Link],
Training and Placement Officer, BVCITS
the components also changes in response i.e. the layout managers adapt to the dimensions of
appletviewer or the application window.
The layout manager is associated with every Container object. Each layout manager is an object
of the class that implements the LayoutManager interface.
AWT Layout Manager Classes:
Following is the list of commonly used controls while designed GUI using AWT.
1. BorderLayout : The borderlayout arranges the components to fit in the five regions: east,
west, north, south and center.
Example:

TS
import [Link].*;
import [Link].*;
public class BoderEx extends JFrame
{
public BoderEx()
{
super ("Border Layout ");
setFont (new Font ("Arial", Font. PLAIN, 26) );
setLayout (new BorderLayout (5, 5));
add ( new Button ("Button 2"),[Link]);
add ( new Button ("Button 1"),[Link]);
add ( new Button ("Button 3"),[Link]);

CI
add ( new Button ("Button 4"),[Link]);
add ( new Button ("Button 5" ),[Link]);
setBounds(100, 100, 300, 200);
setVisible(true);

}
[Link](JFrame.EXIT_ON_CLOSE);

public static void main (String args [ ])


{
new BoderEx( );
}
BV
}
2. FlowLayout: The FlowLayout is the default layout. The FlowLayout is used to arrange the
components in a line, one after another (in a flow).
import [Link].*;
import [Link].*;
public class MyFlowLayout{
JFrame f;
MyFlowLayout(){
f=new JFrame();
JButton b1=new JButton("1");
JButton b2=new JButton("2");
JButton b3=new JButton("3");
JButton b4=new JButton("4");
JButton b5=new JButton("5");
[Link](b1);[Link](b2);[Link](b3);[Link](b4);[Link](b5);
[Link](new FlowLayout([Link]));

Page No 10
Prepared By,
[Link],
Training and Placement Officer, BVCITS
//setting flow layout of right alignment
[Link](300,300);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new MyFlowLayout();
}
}

TS
3. GridLayout: The GridLayout manages the components in form of a rectangular grid. One
component is displayed in each rectangle.
Example: Refer lab exercise 15a
4. CardLayout: The CardLayout object treats each component in the container as a card. Only one card is
visible at a time.
import [Link].*;
/*<applet code=main width=300 height=300></applet>*/
public class main extends [Link] {
CardLayout cards = new CardLayout();
public void init() {
setLayout( cards );
add( new Button("one"), "one" );
add( new Button("two"), "two" );

}
}

}
CI
add( new Button("three"), "three" );

public boolean action( Event e, Object arg) {


[Link]( this );
return true;

5. GridBagLayout: This is the most flexible layout manager class. The object of GridBagLayout aligns the
component vertically, horizontally or along their baseline without requiring the components of same
BV
size.

Page No 11
Prepared By,
[Link],
Training and Placement Officer, BVCITS
Menu
The Menu class is a pull-down menu component which is displayed from the menu bar.

TS
TOPIC BEYOND THE SYLLABUS
Swing – Introduction
Java Swing tutorial is a part of Java Foundation Classes (JFC) that is used to create window-
based applications. It is built on the top of AWT (Abstract Window Toolkit) API and entirely
written in java.
Unlike AWT, Java Swing provides platform-independent and lightweight components. The
[Link] package provides classes for java swing API such as JButton, JTextField, JTextArea,
JRadioButton, JCheckbox, JMenu, JColorChooser etc.
CI
Swing package provides classes which are an extension of the classes defined in the AWT
package.
•Swing components are more powerful and flexible than the components available in AWT
package.
•The components in Swing package will have a modern look and feel in contrast to the AWT
components.
•Swing components are used more over the AWT components due to three reasons:
[Link] AWT components are translated into native components based on the operating system.
They do not have the same look and feel in every operating system.
BV
[Link] look and feel of the AWT components is fixed.
[Link] components are heavy weight components. They are rectangular in shape and opaque.
•Swing is built on AWT. Swing is not a replacement for AWT.
•Swing package provides two important features:
[Link] components are light weight components.
[Link] supports pluggable look and feel.
Difference between AWT and Swing
There are many differences between java awt and swing that are given below.
No. Java AWT Java Swing

1) AWT components are platform- Java swing components are platform-


dependent. independent.
2) AWT components are heavyweight. Swing components are lightweight.

3) AWT doesn't support pluggable look Swing supports pluggable look and feel.
and feel.
Page No 12
Prepared By,
[Link],
Training and Placement Officer, BVCITS
Swing provides more powerful components such
AWT provides less components than
4) as tables, lists, scrollpanes, colorchooser,
Swing.
tabbedpane etc.

AWT doesn't follows MVC(Model


View Controller) where model
5) Swing follows MVC.
represents data, view represents
presentation and controller acts as an
interface between model and view.

TS
MVC Architecture
•Generally each visual component consists of three distinct aspects:
[Link] state information associated with the component.
[Link] visual appearance of the component.
[Link] way the component reacts to the user.
•In MVC(Model-View-Controller) architecture “model” corresponds to the state information of
the component. “view” corresponds to the visual appearance of the component and the
“controller” corresponds to how the component reacts to the user.
•In MVC architecture, model, view and controller are independent of one another.
•It means that the state information is independent of the look and feel of the component.
•Swing component uses a modified version of the MVC architecture known as “Model-

CI
Delegate” or “Separate model” architecture.
•In Java, “view” and “controller” of the MVC had been combined into a single entity known as
“UI Delegate”.
The MVC design pattern consists of three modules model, view and controller.
Model The model represents the state (data) and business logic of the application. It responds
to the request from the view and it also responds to instructions from the controller to update
itself.
View The view module is responsible to display data i.e. it represents the presentation.
Controller The controller module acts as an interface between view and model. It is responsible
for responding to user input and perform interactions on the data model objects. The controller
BV
receives the input, it validates the input and then performs the business operation that
modifies the state of the data model.

Page No 13
Prepared By,
[Link],
Training and Placement Officer, BVCITS
Advantages of MVC Architecture
• Navigation control is centralized now only controller contains the logic to determine the
next page.
• Easy to maintain
• Easy to extend
• Easy to test
• Better separation of concerns
Disadvantage of MVC Architecture
• We need to write the controller code self. If we change the controller code, we need to

TS
recompile the class and redeploy the application.
Note: The struts2 framework is developed based on the MVC architecture
Hierarchy of Java Swing classes
The hierarchy of java swing API is given below

CI
BV
Components and Containers in Swing
•In general every Swing GUI consists of “components” and “containers”.
•All containers are components.
•A component is a thing/object that has a visual appearance and on which user can perform
events.
•A container is a object/thing which can hold other components.
•Since all containers are components, a container can hold other containers.
•Every component must be added to a container in order to display it.
•So, every Swing GUI will contain atleast one container.
•This allows for container hierarchy, in which at the top we will have “top-level containers”.
•All the Swing components are derived from the “JComponent” class except the four top level
containers: JFrame, JApplet, JWindow and JDialog.
•The above four top level containers are the only heavy weight components in the Swing
package.
•“JComponent“ inherits both “Container” and “Component” in AWT package.

Page No 14
Prepared By,
[Link],
Training and Placement Officer, BVCITS
•So, all the Swing components are built on top of the AWT components.
•The four top level containers:
JFrame
JApplet
JWindow
JDialog do not inherit “JComponent“ class.
•The are two types of containers in Swing. They are:
•Top-level containers (JFrame, JApplet, JWindow and JDialog)
•Light weight containers (JPanel)

TS
•Following are some of the examples of Swing components:
[Link] [Link] [Link] [Link] [Link] [Link] [Link]
[Link] [Link] [Link] [Link] [Link]
Exploring swing- JApplet: A JApplet is an Applet that supports the Swing graphics library.
The classes that make up the Swing library can be found in the package [Link]. Swing
includes the class [Link] to be used as a basis for writing applets.
JApplet is actually a subclass of Applet, so JApplets are in fact Applets in the usual sense.
However, JApplets have a lot of extra structure that plain Applets don't have. Because of this
structure, the painting of a JApplet is a more complex affair and is handled by the system. So,
when you make a subclass of JApplet you should not write a paint() method for it. As we
will see, if you want to draw on a JApplet, you should add a component to the applet to be used
for that purpose. On the other hand, you can and generally should write an init() method

CI
for a subclass of JApplet.
import [Link];
import [Link];
public class HelloWorldJApplet extends JApplet {
public void paint(Graphics g) {

}
}
[Link]("Helo Word in a JApplet",225,225);

/*<applet code="HelloWorldJApplet" width="500" height="400">


BV
</applet>*/
Output:

JFrame and JButton class: JFrame class is one of the containers provided by the [Link]
package. Containers in [Link] are components where other components can be located. The
components added to the frame are referred to as its contents; these are managed by the
contentPane. To add a component to a JFrame, we must use its contentPane [Link] is
a Window with border, title and buttons. When JFrame is set visible, an event dispatching
Page No 15
Prepared By,
[Link],
Training and Placement Officer, BVCITS
thread is started. JFrame objects store several objects including a Container object known as
the content pane. To add a component to a JFrame, add it to the content pane.
JFrame Features: It’s a window with title, border, (optional) menu bar and user-specified
components.
It can be moved, resized, iconified. It is not a subclass of JComponent. Delegates responsibility
of managing user-specified components to a content pane, an instance of JPanel.
Centering JFrame’s
By default, a Jframe is displayed in the upper-left corner of the screen. To display a frame at a
specified location, you can use the setLocation(x, y) method in the JFrame class. This method

TS
places the upper-left corner of a frame at location (x, y). The Swing API keeps improving with
abstractions such as the setDefaultCloseOperation method for the JFrame
Crating a JFrame Window:
Step 1: Construct an object of the JFrame class.
Step 2: Set the size of the JFrame.
Step 3: Set the title of the JFrame to appear in the title bar (title bar will be blank if no title is
set).
Step 4: Set the default close operation. When the user clicks the close button, the program
stops running.
Step 5: Make the JFrame visible.
JButton: It is a fundamental Swing component that renders a button on screen and responds
to user’s clicking event for performing a specific task. The abstract class AbstractButton extends

CI
class JComponent and provides a foundation for a family of button classes, including JButton. A
button is a component the user clicks to trigger a specific action.
There are several types of buttons in Java, all are subclasses of AbstractButton.
• command buttons: is created with class JButton. It generates ActionEvent.
• toggle buttons: have on/off or true/false values.
• check boxes: a group of buttons. It generates ItemEvent.
• radio buttons: a group of buttons in which only one can be selected. It generates
ItemEvent.
Create a default button with a caption:
JButton button = new JButton("Edit");
BV
Image:
Ex:
import [Link].*;
public class JFrameExample {
public static void main(String[] args) {
JFrame f=new JFrame("This is JFrame window");//creating instance of JFrame
JButton b=new JButton("click");//creating instance of JButton
[Link](130,100,100, 40);//x axis, y axis, width, height
[Link](b);//adding button in JFrame
[Link](400,500);//400 width and 500 height
[Link](null);//using no layout managers
[Link](true);//making the frame visible
[Link](JFrame.EXIT_ON_CLOSE); //to exit the frame
}
}
Page No 16
Prepared By,
[Link],
Training and Placement Officer, BVCITS
Output:

TS
JPanel
JPanel is a Swing’s lightweight container which is used to group a set of components together.
JPanel is a pretty simple component which, normally, does not have a GUI (except when it is
being set an opaque background or has a visual border). JPanel components are opaque, while
JComponents are not opaque.
import [Link];
import [Link];
import [Link];
public class PanelWithComponents {

CI
public static void main(String[] args) {
JPanel panel = new JPanel();
JButton okButton = new JButton("OK");
[Link](okButton);
JButton cancelButton = new JButton("Cancel");
[Link](cancelButton);
JFrame frame = new JFrame("Oval Sample");
[Link](JFrame.EXIT_ON_CLOSE);
[Link](panel);
BV
[Link](300, 200);
[Link](true);
}
}
Output:

************************ALL THE VERY BEST STUDENTS*****************************

Page No 17
Prepared By,
[Link],
Training and Placement Officer, BVCITS

You might also like