0% found this document useful (0 votes)
77 views23 pages

AWT and Swing Important QUESTIONS

Java AWT (Abstract Windowing Toolkit) is an API for developing GUI applications in Java, providing classes and methods for window management. The line 'import java.awt.*' imports all classes from the AWT package, allowing access to various GUI components. AWT components are platform-dependent and include elements like labels, buttons, and checkboxes.

Uploaded by

arati.sandbhor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views23 pages

AWT and Swing Important QUESTIONS

Java AWT (Abstract Windowing Toolkit) is an API for developing GUI applications in Java, providing classes and methods for window management. The line 'import java.awt.*' imports all classes from the AWT package, allowing access to various GUI components. AWT components are platform-dependent and include elements like labels, buttons, and checkboxes.

Uploaded by

arati.sandbhor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Define AWT and state the meaning of import

java.awt.* line in java


Java AWT(Abstract Windowing Toolkit)is an API to develop GUI or window-based
Java AWT(Abstract Windowing Toolkit)is an API to develop GUI or window-based
application in java. The abstract window toolkit(AWT) was Java’s first GUI
framework, and its been part of java since version 1.0.. The AWT contains numerous
classes and methods that allow us to create and manage windows. Although the main
purpose of the AWT is to support applet windows, it can also be used to create
stand-alone windows that run in a GUI environment, such as Windows. The java.awt
package provides many classes like Textfield, checkbox, list, choice, label,
radiobutton, etc. Java AWT components are platform dependent.
application in java. The line import java.awt. * means import all classes under in the awt
package. Java.awt.*imports the whole package but not sub-packages.

Define AWT? List any 2 swing features


Ans:
AWT:
Java AWT(Abstract Windowing Toolkit)is an API to develop GUI or window-based application
in java. The abstract window toolkit(AWT) was Java’s first GUI framework, and its been part of
java since version 1.0.. The AWT contains numerous classes and methods that allow us to create
and manage windows. Although the main purpose of the AWT is to support applet windows, it
can also be used to create stand-alone windows that run in a GUI environment, such as Windows.
The java.awt package provides many classes like Textfield, checkbox, list, choice, label,
radiobutton, etc. Java AWT components are platform dependent
Features of Swing: (Any 2)
Borders : We can draw borders in many different styles around components using the setborder( )
method.

Graphics Debugging : We can use setDebuggingGraphicsOptions method to set up graphics


debugging which means among the other things, that you can watch each line as its drawn and
make it flash.
Easy mouseless operation : It is easy to connect keystrokes to components.

Tooltips : We can use the setToolTipText method of JComponent to give components a tooltip,
one of those small windows that appear when the mouse hovers over a component and gives
explanatory text.

Easy Scrolling : We can connect scrolling to various components-something that was impossible
in AWT.

Pluggable look and feel : We can set the appearance of applets and applications to one of three
standard looks. Windows, Motif (Unix) or Metal (Standard swing look).
List AWT controls? Explain any one in detail
with suitable example
Ans:

AWT Controls:
List AWT controls? Explain any one in detail with suitable example

Ans:

AWT Controls:

Java AWT controls are the controls that are used to design graphical user interfaces or web
applications. To make an effective GUI, Java provides java.awt package that supports various
AWT controls like
Label,
Button,
CheckBox,
List,
Choice, etc.
A label is used for placing text inside the container. A label is used only for inputting text. The
label does not imply that the text can be altered or it can be used as a button which can be further
worked upon.
For example-
import java.awt.*;
public class LabelExample {
public static void main(String args[]){

// creating the object of Frame class and Label class


Frame f = new Frame ("Label example");
Label l1;
// initializing the labels
l1 = new Label ("First Label.");
// set the location of label
l1.setBounds(50, 100, 100, 30);
// adding labels to the frame
f.add(l1);
// setting size, layout and visibility of frame
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
} }
List any 4 swing features
Swing is a set of classes this provides more powerful and flexible components than are possible with
the AWT. In addition to the familiar components, such as buttons, check boxes, and labels, Swing
supplies several exciting additions, including tabbed panes, scroll panes, trees, and tables. Even
familiar components such as buttons have more capabilities in Swing. For example, a button may have
both an image and a text string associated with it. Also, the image can be changed as the state of the
button changes.
Features of Swing:
Borders : We can draw borders in many different styles around components using the setborder( )
method.

Graphics Debugging : We can use setDebuggingGraphicsOptions method to set up graphics


debugging which means among the other things, that you can watch each line as its drawn and
make it flash.
Easy mouseless operation : It is easy to connect keystrokes to components.

Tooltips : We can use the setToolTipText method of JComponent to give components a tooltip,
one of those small windows that appear when the mouse hovers over a component and gives
explanatory text.

Easy Scrolling : We can connect scrolling to various components-something that was impossible
in AWT.

Pluggable look and feel : We can set the appearance of applets and applications to one of three
standard looks. Windows, Motif (Unix) or Metal (Standard swing look).

New Layout Managers : Swing introduces the BoxLayout and OverlayLayout layout managers.

Construct a List control in awt to display the Subject


Name list shown below- a)Advance Java b)Vb
c)Data Structure d)Linux e)Android f)Operating
System
Ans:
// importing awt class
import java.awt.*;
public class ListExample1
{
// class constructor
ListExample1() {
// creating the frame
Frame f = new Frame();
// creating the list of 6 rows
List l1 = new List(6);

// setting the position of list component


l1.setBounds(100, 100, 75, 75);

// adding list items into the list


l1.add("Advance Java ");
l1.add("Vb");
l1.add("Data Structure");
l1.add("Linux");
l1.add("Android");
l1.add("Operating System")

// adding the list to frame


f.add(l1);

// setting size, layout and visibility of frame


f.setSize(400, 400);
f.setLayout(null);
f.setVisible(true);
}

// main method
public static void main(String args[])
{
new ListExample1();
}
}

Define GridLayout .Explain Constructors of


GridLayout class.
GridLayout : 1M
The Java GridLayout class is used to arrange the components in a rectangular
grid. One component is displayed in each rectangle.

Constructors of GridLayout class: 3M


GridLayout(): creates a grid layout with one column per component in a row.
GridLayout(int rows, int columns): creates a grid layout with the given rows and
columns but no gaps between the components.
GridLayout(int rows, int columns, int hgap, int vgap): creates a grid layout with
the given rows and columns along with given horizontal and vertical gaps.

Describe the Applet Life-Cycle with neat


diagram.

Describe the Applet Life-Cycle with neat diagram.


Ans:
An applet is a Java program that can be embedded into a web page. It runs inside the web
browser and works on the client-side. An applet is embedded in an HTML page using the
APPLET or OBJECT tag and hosted on a web server. The entire life cycle of an applet is
managed by the Applet Container.
Step 1: Initialization 3M
public void init() 1M
There is no main method unlike our normal java programs. Every Applet will start it’s
execution from init() method. It is executed only once
Step 2: Start 1M
public void start()
After init() method start() method is invoked. Executed when the browser is maximized
Step 3: Paint 1M
public void paint (Graphics g)
Paint method is used to display the content on the applet. We can create the objects or
components to the applet or we can directly write a message on the applet. It will take
Graphics class as a parameter.
Step 4: Stop
public void stop()
stop() method is used to stop the applet. It is executed when the browser is minimized.

Explain AWT controls? Explain any one in


detail with suitable example
Java AWT controls are the controls that are used to design graphical user interfaces or web
applications. To make an effective GUI, Java provides java.awt package that supports various
AWT controls like Label, Button, CheckBox, List, Choice, etc.
A label is used for placing text inside the container. A label is used only for inputting text.
The label does not imply that the text can be altered or it can be used as a button which can
be further worked upon.
For example- import java.awt.*;
public class LabelExample {
public static void main(String args[]){

// creating the object of Frame class and Label class


Frame f = new Frame ("Label example");
Label l1;
// initializing the labels
l1 = new Label ("First Label.");

// set the location of label


l1.setBounds(50, 100, 100, 30);
// adding labels to the frame
f.add(l1);
// setting size, layout and visibility of frame
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
} }

Define Frame? Explain in detail with suitable


example.
The Frame is the container that contain title bar and border and can have menu bars. It can
have other components like button, text field, scrollbar etc. Frame is most widely used
container while developing an AWT application.
For example-
// importing Java AWT class
import java.awt.*;

// class AWTExample2 directly creates instance of Frame class


class AWTExample2 {

// initializing using constructor


AWTExample2() {

// creating a Frame
Frame f = new Frame();

// creating a Label
Label l = new Label("Employee id:");

// setting position of above components in the frame


l.setBounds(20, 80, 80, 30);

// adding components into frame


f.add(l);
// frame size 300 width and 300 height
f.setSize(400,300);

// setting the title of frame


f.setTitle("Employee info");

// no layout
f.setLayout(null);

// setting visibility of frame


f.setVisible(true);
}
// main method
public static void main(String args[]) {

AWTExample2 awt_obj = new AWTExample2();


} }

Write a program to design JComboBox

Write a program to design JComboBox.


Ans.

import javax.swing.*;
public class ComboBoxExample {
JFrame f;
ComboBoxExample() {
f = new JFrame("ComboBox Example");
String country[] = {
"India",
"Aus",
"U.S.A",
"England",
"Newzealand"
};
JComboBox cb = new JComboBox(country);
cb.setBounds(50, 50, 90, 20);
f.add(cb);
f.setLayout(null);
f.setSize(400, 500);
f.setVisible(true);
}
public static void main(String[] args) {
new ComboBoxExample();
}
}

Explain AWT and state the meaning of import


java.awt.* package in Advanced java.
Ans:

AWT:

Java AWT(Abstract Windowing Toolkit)is an API to develop GUI or window-based application in


java. The abstract window toolkit(AWT) was Java’s first GUI framework, and its been part of java
since version 1.0.. The AWT contains numerous classes and methods that allow us to create and
manage windows. Although the main purpose of the AWT is to support applet windows, it can also
be used to create stand-alone windows that run in a GUI environment, such as Windows. The
java.awt package provides many classes like Textfield, checkbox, list, choice, label, radiobutton, etc.
Java AWT components are platform dependent.

java.awt.*

Java AWT(Abstract Windowing Toolkit)is an API to develop GUI or window-based application in


java. The line import java.awt. * means import all classes under in the awt package.
Java.awt.*imports the whole package but not sub-packages.

Define Panel? Explain in Detail with suitable


example.
The Panel class is a concrete subclass of Container. It doesn’t add any new methods; it
simply implements Container. A Panel may be thought of as a recursively nestable, concrete
screen component. Panel is the super-class for Applet. When screen output is directed to an
applet, it is drawn on the surface of a Panel object. In essence, a Panel is a window that does
not contain a title bar, menu bar, or border. This is why we don’t see these items when an
applet is run inside a browser. When we run an applet using an applet viewer, the applet
viewer provides the title and border. Other components can be added to a Panel object by its
add( ) method (inherited from Container). Once these components have been added, we can
position and resize them manually using the
setLocation( ), setSize( ), or setBounds( ) methods defined by Component.
import java.awt.*;
class frame_with_panel extends Frame{
frame_with_panel(){
setTitle("PanelExample");
Panel p = new Panel();
Label l1 = new Label("MyLabel1");
Label l2 = new Label("MyLabel2");
Label l3 = new Label("MyLabel3");
TextField t1
Checkbox cb = new Checkbox("MyCheckbox1");
cb.setBounds(50, 50, 70, 30);
p.add(cb);
Button sb = new Button("Button1");
p.add(sb);
l1.setBounds(50, 60, 70, 30);
l2.setBounds(60, 60, 80, 80);
l3.setBounds(60, 60, 70, 30);
p.add(l1);p.add(l2);p.add(l3);
p.setVisible(true);
add(p);
setVisible(true);
setSize(500,500);
}
public static void main(String args[]){
frame_with_panel s = new frame_with_panel();
} }

Explain event delegation model with diagram


Ans:
The modern approach to handling events is based on the delegation event model, which
defines standard and consistent mechanisms to generate and process events. Its concept
is quite simple: a source generates an event and sends it to one or more listeners. In this
scheme, the listener simply waits until it receives an event. Once received, the listener
processes the event and then returns. The advantage of this design is that the application
logic that processes events is cleanly separated from the user interface logic that
generates those events. A user interface element is able to “delegate” the processing of
an event to a separate piece of code.
In the delegation event model, listeners must register with a source in order to
receive an event notification. This provides an important benefit: notifications are sent
only to listeners that want to receive them. This is a more efficient way to handle events
than the design used by the old Java 1.0 approach.
Java also allows us to process events without using the delegation event model.
This can be done by extending an AWT component. However, the delegation event
model is the preferred design for the reasons just cited.
}

List AWT controls? Explain any one in detail with


suitable example.
Ans:

AWT controls 1M

Label,
Button,
CheckBox,
List,
Choice, etc.
For example- import java.awt.*; 3M
public class LabelExample {
public static void main(String args[])
{

// creating the object of Frame class and Label class


Frame f = new Frame ("Label example");
Label l1;
// initializing the labels
l1 = new Label ("First Label.");
// set the location of label
l1.setBounds(50, 100, 100, 30);
// adding labels to the frame
f.add(l1);
// setting size, layout and visibility of frame
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}

What is Layout ? List out different Layout Managers in


Java.(4)
Explain FlowLayout with suitable example.(4)
Ans:
Layout: 1M
The Layout are used to arrange components in a particular manner.
The Java LayoutManagers facilitates us to control the positioning and size
of the components in GUI forms. LayoutManager is an interface that is implemented
by all the classes of layout managers.

The following are some Layouts available in Java:


- BorderLayout 1M
- FlowLayout
- GridLayout
- CardLayout
- GridBagLayout
- BoxLayout
- GroupLayout
- ScrollPaneLayout
- SpringLayout
FlowLayout program:(allowed any method) 2M
import java.awt.*;
import java.applet.*;
/*
<applet code="FlowLayoutDemo" width=250 height=200>
</applet>
*/

What is Layout ? List out different Layout Managers in Java.


Explain FlowLayout with suitable example.
Ans:
Layout: 1M
The Layout are used to arrange components in a particular manner.
The Java LayoutManagers facilitates us to control the positioning and size
of the components in GUI forms. LayoutManager is an interface that is implemented
by all the classes of layout managers.

The following are some Layouts available in Java:


- BorderLayout 1M
- FlowLayout
- GridLayout
- CardLayout
- GridBagLayout
- BoxLayout
- GroupLayout
- ScrollPaneLayout
- SpringLayout
FlowLayout program:(allowed any method) 2M
import java.awt.*;
import java.applet.*;
/*
<applet code="FlowLayoutDemo" width=250 height=200>
</applet>
*/
public class FlowLayoutDemo extends Applet
{
String msg = "";
Checkbox Win98, winNT, solaris, mac;
public void init()
{
Win98 = new Checkbox("Windows 98/XP", null, true);
winNT = new Checkbox("Windows NT/2000");
solaris = new Checkbox("Solaris");
mac = new Checkbox("MacOS");
setLayout(new FlowLayout(FlowLayout.CENTER));
add(Win98);
add(winNT);
add(solaris);
add(mac);
}
public void paint(Graphics g)
{
}
}

State the Difference between AWT and SWING.


Any 4 point each point 1 mark
S.N
O
AWT Swing

Swing is a part of Java Foundation


Java AWT is an API to develop Classes and is used to create various
1. GUI applications in Java applications.
S.N
O
AWT Swing

The components of Java AWT The components of Java Swing are


2. are heavy weighted. light weighted.

Java AWT has comparatively less


functionality as compared to Java Swing has more functionality as
3. Swing. compared to AWT.

The execution time of AWT is The execution time of Swing is less


4. more than Swing. than AWT.

The components of Java AWT The components of Java Swing are


5. are platform dependent. platform independent.

MVC pattern is not supported by


6. AWT. MVC pattern is supported by Swing.

AWT provides comparatively Swing provides more powerful


7. less powerful components. components.

Write a program to demonstrate JButton


Ans:

JButton: 4M
import javax.swing.*;
public class ButtonExample{
ButtonExample(){
JFrame f=new JFrame("Button Example");
JButton b=new JButton(new ImageIcon("D:\\icon.png"));
b.setBounds(100,100,100, 40);
f.add(b);
f.setSize(300,400);
f.setLayout(null);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args)
{

new ButtonExample();
}
}
With the help of diagram describe following Swing
Components
JCheckBox
II. JCheckBox
-> import javax.swing.*;
public class CheckBoxExample {
CheckBoxExample() {
JFrame f = new JFrame("CheckBox Example");

JCheckBox checkBox1 = new JCheckBox("C++");


checkBox1.setBounds(100, 100, 50, 50);
JCheckBox checkBox2 = new JCheckBox("Java", true);
checkBox2.setBounds(100, 150, 50, 50);
f.add(checkBox1);
f.add(checkBox2);
f.setSize(400, 400);
f.setLayout(null);
f.setVisible(true);
}public static void main(String args[]) {
new CheckBoxExample();
}
}

State the Difference between AWT and SWING.


S.N
O
AWT Swing

Swing is a part of Java Foundation


Java AWT is an API to develop Classes and is used to create various
1. GUI applications in Java applications.

The components of Java AWT The components of Java Swing are


2. are heavy weighted. light weighted.

3. Java AWT has comparatively less Java Swing has more functionality as
S.N
O
AWT Swing

functionality as compared to
Swing. compared to AWT.

The execution time of AWT is The execution time of Swing is less


4. more than Swing. than AWT.

The components of Java AWT The components of Java Swing are


5. are platform dependent. platform independent.

MVC pattern is not supported by


6. AWT. MVC pattern is supported by Swing.

AWT provides comparatively Swing provides more powerful


7. less powerful components. components.

Construct a list to display the OS list shown below-


a)windows b)macOS c)Linux d)Webgl e)Android
f)Directx
The object of List class represents a list of text items. With the help of the List class, user can
choose either one item or multiple items.

// importing awt class


import java.awt.*;
public class ListExample1
{
// class constructor
ListExample1() {
// creating the frame
Frame f = new Frame();
// creating the list of 5 rows
List l1 = new List(5);

// setting the position of list component


l1.setBounds(100, 100, 75, 75);

// adding list items into the list


l1.add("Windows");
l1.add("Mac OS");
l1.add("Linux");
l1.add("WebGL");
l1.add("Android");
l1.add("DirectX")

// adding the list to frame


f.add(l1);

// setting size, layout and visibility of frame


f.setSize(400, 400);
f.setLayout(null);
f.setVisible(true);
}

// main method
public static void main(String args[])
{
new ListExample1();
}
}

Write a program to design GridBagLayout.


The Java GridBagLayout class is used to align components vertically, horizontally or along their
baseline.The components may not be of the same size. Each GridBagLayout object maintains a
dynamic, rectangular grid of cells. Each component occupies one or more cells known as its
display area. Each component associates an instance of GridBagConstraints. With the help of the
constraints object, we arrange the component's display area on the grid. GridBagLayout
components are also arranged in the rectangular grid but can have many different sizes and can
occupy multiple rows or columns.

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

public class GridBagLayoutExample extends JFrame {


public GridBagLayoutExample() {
setTitle("GridBag Layout Example");
GridBagLayout layout = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
this.setLayout(layout);

gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 0;
this.add(new Button("Button One"), gbc);

gbc.gridx = 1;
gbc.gridy = 0;
this.add(new Button("Button two"), gbc);
gbc.ipady = 20;
gbc.gridx = 0;
gbc.gridy = 1;
this.add(new Button("Button Three"), gbc);

gbc.gridx = 1;
gbc.gridy = 1;
this.add(new Button("Button Four"), gbc);

gbc.gridx = 0;
gbc.gridy = 2;
gbc.gridwidth = 2;
this.add(new Button("Button Five"), gbc);

setSize(300, 300);
setPreferredSize(getSize());
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public static void main(String[] args) {


GridBagLayoutExample a = new GridBagLayoutExample();
}
}

Describe the Applet Life-Cycle with neat diagram.


An applet is a Java program that can be embedded into a web page. It runs inside the web
browser and works on the client-side. An applet is embedded in an HTML page using the
APPLET or OBJECT tag and hosted on a web server. The entire life cycle of an applet is
managed by the Applet Container.
Step 1: Initialization
public void init()
There is no main method unlike our normal java programs. Every Applet will start it’s
execution from init() method. It is executed only once
Step 2: Start
public void start()
After init() method start() method is invoked. Executed when the browser is maximized
Step 3: Paint
public void paint (Graphics g)
Paint method is used to display the content on the applet. We can create the objects or
components to the applet or we can directly write a message on the applet. It will take
Graphics class as a parameter.
Step 4: Stop
public void stop()
stop() method is used to stop the applet. It is executed when the browser is minimized.
destroy() method is used to completely close the applet. It is executed when the applet is
Step 5: Destroy
public void destroy()
closed

Write a program to design GridLayout


import java.awt.*;

class frame_with_gridlayout extends Frame{


frame_with_gridlayout(String titlename){
setTitle("GridLayout Example");
Panel p = new Panel();
p.setLayout(new GridLayout(4, 2));
Button b1 = new Button("Button 1");
Button b2 = new Button("Button 2");
Button b3 = new Button("Button 3");
Button b4 = new Button("Button 4");
Button b5 = new Button("Button 5");
Button sb = new Button("Submit");

p.add(sb);
p.add(b1);
p.add(b2);
p.add(b3);
p.add(b4);
p.add(b5);

p.setVisible(true);
add(p);

setVisible(true);
setSize(1000,1000);
}
public static void main(String args[]){
frame_with_gridlayout s = new frame_with_gridlayout("IDk");
}
}
}

Write a program to demonstrate JLabel


JLabel
->
import javax.swing.*;
class LabelExample {
public static void main(String args[]) {
JFrame f = new JFrame("Label Example")
JLabel l1, l2;
l1 = new JLabel("First Label.");
l1.setBounds(50, 50, 100, 30);
l2 = new JLabel("Second Label.");
l2.setBounds(50, 100, 100, 30);
f.add(l1);
f.add(l2);
f.setSize(300, 300);
f.setLayout(null);
f.setVisible(true);
}
}

You might also like