0% found this document useful (0 votes)
22 views

Java Unit 4

Uploaded by

Bindu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Java Unit 4

Uploaded by

Bindu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 34

UNIT-IV

Java AWT (Abstract Window Toolkit) is an API to develop Graphical User Interface (GUI)
or windows-based applications in Java.

Java AWT components are platform-dependent i.e. components are displayed according to the
view of operating system. AWT is heavy weight i.e. its components are using the resources of
underlying operating system (OS).

The java.awt package provides classes for AWT API such as TextField, Label, TextArea,
RadioButton, CheckBox, Choice, List etc.

Why AWT is platform independent?

Java AWT calls the native platform calls the native platform (operating systems) subroutine for
creating API components like TextField, ChechBox, button, etc.

For example, an AWT GUI with components like TextField, label and button will have different
look and feel for the different platforms like Windows, MAC OS, and Unix. The reason for this
is the platforms have different view for their native components and AWT directly calls the
native subroutine that creates those components.

In simple words, an AWT application will look like a windows application in Windows OS
whereas it will look like a Mac application in the MAC OS.

Java AWT Hierarchy

The hierarchy of Java AWT classes are given below.

Prepard by K.Swathi Lakshmi Durga


Assistant Professor
Department of IT
Components

All the elements like the button, text fields, scroll bars, etc. are called components. In Java AWT,
there are classes for each component as shown in above diagram. In order to place every
component in a particular position on a screen, we need to add them to a container.

Container

The Container is a component in AWT that can contain another components like buttons,
textfields, labels etc. The classes that extends Container class are known as container such
as Frame, Dialog and Panel.

It is basically a screen where the where the components are placed at their specific locations.
Thus it contains and controls the layout of components.

Note: A container itself is a component (see the above diagram), therefore we can add a
container inside container.

Prepard by K.Swathi Lakshmi Durga


Assistant Professor
Department of IT
Types of containers:

There are four types of containers in Java AWT:

1. Window
2. Panel
3. Frame
4. Dialog

Window

The window is the container that have no borders and menu bars. You must use frame, dialog or
another window for creating a window. We need to create an instance of Window class to create
this container.

Panel

The Panel is the container that doesn't contain title bar, border or menu bar. It is generic
container for holding the components. It can have other components like button, text field etc.
An instance of Panel class creates a container, in which we can add components.

Frame

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.

Useful Methods of Component Class


Method Description

public void add(Component c) Inserts a component on this component.

public void setSize(int width,int height) Sets the size (width and height) of the component.

public void setLayout(LayoutManager Defines the layout manager for the component.
m)

Prepard by K.Swathi Lakshmi Durga


Assistant Professor
Department of IT
public void setVisible(boolean status) Changes the visibility of the component, by default
false.

Java AWT Example

To create simple AWT example, you need a frame. There are two ways to create a GUI using
Frame in AWT.

1. By extending Frame class (inheritance)


2. By creating the object of Frame class (association)

AWT Example by Inheritance

package application;

import java.awt.event.*;

import java.awt.*;

public class first extends Frame implements WindowListener{

first(){

setTitle("BANK");

setSize(500,500);

setLayout(null);

Label l1 = new Label("Username");

Label l2 = new Label("password");

TextField t1= new TextField();

TextField t2 = new TextField();

Button b1 = new Button("Login");

l1.setBounds(30, 100,80,30);

l2.setBounds(30, 200, 80, 30);


Prepard by K.Swathi Lakshmi Durga
Assistant Professor
Department of IT
t1.setBounds(130, 100, 80, 30);

t2.setBounds(130, 200, 120, 50);

b1.setBounds(65, 300, 120, 50);

add(l1);

add(l2);

add(t1);

add(t2);

add(b1);

setVisible(true);

addWindowListener(this);

public static void main(String[] args)throws Exception {

// TODO Auto-generated method stub

new first();

@Override

public void windowOpened(WindowEvent e) {

// TODO Auto-generated method stub

@Override

public void windowClosing(WindowEvent e) {

dispose();

@Override

Prepard by K.Swathi Lakshmi Durga


Assistant Professor
Department of IT
public void windowClosed(WindowEvent e) {

// TODO Auto-generated method stub

@Override

public void windowIconified(WindowEvent e) {

// TODO Auto-generated method stub

@Override

public void windowDeiconified(WindowEvent e) {

// TODO Auto-generated method stub

@Override

public void windowActivated(WindowEvent e) {

// TODO Auto-generated method stub

@Override

public void windowDeactivated(WindowEvent e) {

// TODO Auto-generated method stub

O/P:

Prepard by K.Swathi Lakshmi Durga


Assistant Professor
Department of IT
Swings:
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 Windowing Toolkit) API and entirely
written in java.

Unlike AWT, Java Swing provides platform-independent and lightweight components.

The javax.swing package provides classes for java swing API such as JButton, JTextField,
JTextArea, JRadioButton, JCheckbox, JMenu, JColorChooser etc.

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-dependent. Java swing components are platform-


independent.

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

3) AWT doesn't support pluggable look and feel. Swing supports pluggable look and

Prepard by K.Swathi Lakshmi Durga


Assistant Professor
Department of IT
feel.

4) AWT provides less components than Swing. Swing provides more powerful
components such as tables, lists,
scrollpanes, colorchooser, tabbedpane
etc.

5) AWT doesn't follows MVC(Model View Swing follows MVC.


Controller) where model represents data, view
represents presentation and controller acts as an
interface between model and view.

What is JFC

The Java Foundation Classes (JFC) are a set of GUI components which simplify the
development of desktop applications.

Hierarchy of Java Swing classes

The hierarchy of java swing API is given below.

Prepard by K.Swathi Lakshmi Durga


Assistant Professor
Department of IT
Commonly used Methods of Component class

The methods of Component class are widely used in java swing that are given below.

Method Description

public void add(Component c) add a component on another component.

public void setSize(int width,int height) sets size of the component.

public void setLayout(LayoutManager sets the layout manager for the component.
m)

public void setVisible(boolean b) sets the visibility of the component. It is by default


false.

Prepard by K.Swathi Lakshmi Durga


Assistant Professor
Department of IT
Java Swing Examples

There are two ways to create a frame:

o By creating the object of Frame class (association)


o By extending Frame class (inheritance)

We can write the code of swing inside the main(), constructor or any other method.

Java JButton

The JButton class is used to create a labeled button that has platform independent
implementation. The application result in some action when the button is pushed. It inherits
AbstractButton class.

JButton class declaration

Let's see the declaration for javax.swing.JButton class.

1. public class JButton extends AbstractButton implements Accessible

Commonly used Constructors:

Constructor Description

JButton() It creates a button with no text and icon.

JButton(String s) It creates a button with the specified text.

JButton(Icon i) It creates a button with the specified icon object.

Prepard by K.Swathi Lakshmi Durga


Assistant Professor
Department of IT
Commonly used Methods of AbstractButton class:

Description

Methods

void setText(String s) It is used to set specified text on button

String getText() It is used to return the text of the button.

void setEnabled(boolean b) It is used to enable or disable the button.

void setIcon(Icon b) It is used to set the specified Icon on the button.

Icon getIcon() It is used to get the Icon of the button.

void setMnemonic(int a) It is used to set the mnemonic on the button.

void addActionListener(ActionListener a) It is used to add the action listener to this object.

Java JButton Example

1. import javax.swing.*;
2. public class ButtonExample {
3. public static void main(String[] args) {
4. JFrame f=new JFrame("Button Example");
5. JButton b=new JButton("Click Here");
6. b.setBounds(50,100,95,30);
7. f.add(b);
8. f.setSize(400,400);
9. f.setLayout(null);
10. f.setVisible(true);

Prepard by K.Swathi Lakshmi Durga


Assistant Professor
Department of IT
11. }
12. }

Output:

Java JLabel

The object of JLabel class is a component for placing text in a container. It is used to display a
single line of read only text. The text can be changed by an application but a user cannot edit it
directly. It inherits JComponent class.

JLabel class declaration

Let's see the declaration for javax.swing.JLabel class.

1. public class JLabel extends JComponent implements SwingConstants, Accessible

Commonly used Constructors:

Constructor Description

JLabel() Creates a JLabel instance with no image and with an empty string
for the title.

Prepard by K.Swathi Lakshmi Durga


Assistant Professor
Department of IT
JLabel(String s) Creates a JLabel instance with the specified text.

JLabel(Icon i) Creates a JLabel instance with the specified image.

JLabel(String s, Icon i, int Creates a JLabel instance with the specified text, image, and
horizontalAlignment) horizontal alignment.

Commonly used Methods:

Methods Description

String getText() t returns the text string that a label displays.

void setText(String text) It defines the single line of text this component will
display.

void setHorizontalAlignment(int It sets the alignment of the label's contents along the X
alignment) axis.

Icon getIcon() It returns the graphic image that the label displays.

int getHorizontalAlignment() It returns the alignment of the label's contents along the
X axis.

Java JLabel Example


1. import javax.swing.*;
2. class LabelExample
3. {
4. public static void main(String args[])
5. {
Prepard by K.Swathi Lakshmi Durga
Assistant Professor
Department of IT
6. JFrame f= new JFrame("Label Example");
7. JLabel l1,l2;
8. l1=new JLabel("First Label.");
9. l1.setBounds(50,50, 100,30);
10. l2=new JLabel("Second Label.");
11. l2.setBounds(50,100, 100,30);
12. f.add(l1); f.add(l2);
13. f.setSize(300,300);
14. f.setLayout(null);
15. f.setVisible(true);
16. }
17. }

Output:

Java JTextField

The object of a JTextField class is a text component that allows the editing of a single line text. It
inherits JTextComponent class.

JTextField class declaration

Let's see the declaration for javax.swing.JTextField class.

Prepard by K.Swathi Lakshmi Durga


Assistant Professor
Department of IT
Commonly used Constructors

Constructor Description

JTextField() Creates a new TextField

JTextField(String text) Creates a new TextField initialized with the specified text.

JTextField(String text, int columns) Creates a new TextField initialized with the specified text and columns.

JTextField(int columns) Creates a new empty TextField with the specified number of columns.

1. public class JTextField extends JTextComponent implements SwingConstants

Commonly used Methods:

Methods Description

void addActionListener(ActionListener l) It is used to add the specified action listener to receive


action events from this textfield.

Action getAction() It returns the currently set Action for this ActionEvent
source, or null if no Action is set.

void setFont(Font f) It is used to set the current font.

void It is used to remove the specified action listener so that it


removeActionListener(ActionListener l) no longer receives action events from this textfield.

Java JTextField Example


1. import javax.swing.*;

Prepard by K.Swathi Lakshmi Durga


Assistant Professor
Department of IT
2. class TextFieldExample
3. {
4. public static void main(String args[])
5. {
6. JFrame f= new JFrame("TextField Example");
7. JTextField t1,t2;
8. t1=new JTextField("Welcome to Javatpoint.");
9. t1.setBounds(50,100, 200,30);
10. t2=new JTextField("AWT Tutorial");
11. t2.setBounds(50,150, 200,30);
12. f.add(t1); f.add(t2);
13. f.setSize(400,400);
14. f.setLayout(null);
15. f.setVisible(true);
16. }
17. }

Output:

Prepard by K.Swathi Lakshmi Durga


Assistant Professor
Department of IT
Java JTextArea

The object of a JTextArea class is a multi line region that displays text. It allows the editing of
multiple line text. It inherits JTextComponent class

JTextArea class declaration

Let's see the declaration for javax.swing.JTextArea class.

1. public class JTextArea extends JTextComponent

Commonly used Constructors:

Constructor Description

JTextArea() Creates a text area that displays no text initially.

JTextArea(String s) Creates a text area that displays specified text initially.

JTextArea(int row, int column) Creates a text area with the specified number of rows and columns
that displays no text initially.

JTextArea(String s, int row, int Creates a text area with the specified number of rows and columns
column) that displays specified text.

Commonly used Methods:

Methods Description

void setRows(int rows) It is used to set specified number of rows.

void setColumns(int cols) It is used to set specified number of columns.

Prepard by K.Swathi Lakshmi Durga


Assistant Professor
Department of IT
void setFont(Font f) It is used to set the specified font.

void insert(String s, int position) It is used to insert the specified text on the specified position.

void append(String s) It is used to append the given text to the end of the document.

Java JTextArea Example

1. import javax.swing.*;
2. public class TextAreaExample
3. {
4. TextAreaExample(){
5. JFrame f= new JFrame();
6. JTextArea area=new JTextArea("Welcome to javatpoint");
7. area.setBounds(10,30, 200,200);
8. f.add(area);
9. f.setSize(300,300);
10. f.setLayout(null);
11. f.setVisible(true);
12. }
13. public static void main(String args[])
14. {
15. new TextAreaExample();
16. }}

Output:

Prepard by K.Swathi Lakshmi Durga


Assistant Professor
Department of IT
Sample Swing Application:
package application;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

class TabbedPane extends JFrame {

JTabbedPane tabs;

Home h1;

Log1 log;

Reg re;

TabbedPane() {

super("Tabbed Pane Example");

h1=new Home();

h1.setBackground(Color.cyan);
Prepard by K.Swathi Lakshmi Durga
Assistant Professor
Department of IT
log = new Log1();

log.setBackground(Color.darkGray);

re = new Reg();

re.setBackground(Color.yellow);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Setting the JTabbedPane Position and Layout as Wrap

tabs = new JTabbedPane(JTabbedPane.TOP, JTabbedPane.WRAP_TAB_LAYOUT);

tabs.addTab("HOME", h1);

tabs.addTab("LOGIN", log);

tabs.addTab("Registration", re);

getContentPane().add(tabs);

tabs.setBackground(Color.green);

/*Creating CoursePanel by extending JPanel*/

class Home extends JPanel {

JLabel l1;

Home() {

l1 = new JLabel("WELCOME TO ANITS");

setLayout(new FlowLayout());

Prepard by K.Swathi Lakshmi Durga


Assistant Professor
Department of IT
add(l1);

/*Creating SelectCoursePanel by extending JPanel*/

public class sample {

public static void main(String[] args) {

// TODO Auto-generated method stub

TabbedPane frame = new TabbedPane();

frame.setSize(400, 400);

frame.setVisible(true);

//frame.setBackground(Color.CYAN);

Log1.java:
package application;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JPasswordField;

Prepard by K.Swathi Lakshmi Durga


Assistant Professor
Department of IT
import javax.swing.JTextField;

class Log1 extends JPanel implements ActionListener{

// JCheckBox java, swing, hibernate;

JLabel l1;

JLabel l2;

JTextField t1;

JLabel t2;

JPasswordField psw;

JButton b1;

Log1() {

l1 = new JLabel("USername");

l2 = new JLabel("password");

t1= new JTextField();

t2 = new JLabel();

psw = new JPasswordField();

ImageIcon i1 = new ImageIcon("D:\\icon.png");

b1 = new JButton(i1);

l1.setBounds(30, 100, 100, 50);

l2.setBounds(30,180,100,50);

t1.setBounds(140,100,100,50);

psw.setBounds(140,180,100,50);

b1.setBounds(65, 300, 120, 50);

b1.addActionListener(this);

Prepard by K.Swathi Lakshmi Durga


Assistant Professor
Department of IT
add(l1);

add(l2);

add(t1);

add(psw);

add(b1);

setVisible(true);

setSize(400,400);

setLayout(null);

@Override

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

new Home();

Reg.java:
package application;
import javax.swing.*;

public class Reg extends JPanel{


Reg(){
//setTitle("Registration");
JLabel l1 = new JLabel("UserName");
JLabel l2 = new JLabel("Password");
JLabel l3 = new JLabel("Gender");
JLabel l4 = new JLabel("Location");
JLabel l5 = new JLabel("Mobile number");
JTextField t1 = new JTextField();
JPasswordField psw = new JPasswordField();
ButtonGroup bg = new ButtonGroup();
JRadioButton r1 = new JRadioButton("M");
Prepard by K.Swathi Lakshmi Durga
Assistant Professor
Department of IT
JRadioButton r2 = new JRadioButton("F");
bg.add(r1);
bg.add(r2);
Object[] loc = {"India","USA","UK","China","France"};
JComboBox c1 = new JComboBox(loc);
JLabel l6 = new JLabel("accept Terms&condition");
JCheckBox ch1 = new JCheckBox();
l1.setBounds(30,100,100,30);
l2.setBounds(30,140,100,30);
t1.setBounds(140,100,100,30);
psw.setBounds(140,140,100,30);
l3.setBounds(30,180,100,30);
r1.setBounds(70,180,50,30);
r2.setBounds(110,180,50,30);
l4.setBounds(30,220,100,30);
c1.setBounds(110,220,100,30);
ch1.setBounds(30,260,50,30);
l6.setBounds(110,260,200,30);
add(l1);
add(l2);
add(l3);
add(l4);
add(t1);
add(psw);
add(r1);
add(r2);
add(c1);
add(l6);
add(ch1);
setSize(400,400);
setVisible(true);
setLayout(null);
}
public static void main(String args[]) {
new Reg();
}
}

O/P:

Prepard by K.Swathi Lakshmi Durga


Assistant Professor
Department of IT
Prepard by K.Swathi Lakshmi Durga
Assistant Professor
Department of IT
Layout Management:

The LayoutManagers 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.
There are the following classes that represent the layout managers:

1. java.awt.BorderLayout
2. java.awt.FlowLayout
3. java.awt.GridLayout
4. java.awt.CardLayout
5. java.awt.GridBagLayout
6. javax.swing.BoxLayout
7. javax.swing.GroupLayout
8. javax.swing.ScrollPaneLayout
Prepard by K.Swathi Lakshmi Durga
Assistant Professor
Department of IT
9. javax.swing.SpringLayout etc.

Java BorderLayout

The BorderLayout is used to arrange the components in five regions: north, south, east, west, and
center. Each region (area) may contain one component only. It is the default layout of a frame or
window. The BorderLayout provides five constants for each region:

1. public static final int NORTH


2. public static final int SOUTH
3. public static final int EAST
4. public static final int WEST
5. public static final int CENTER

Constructors of BorderLayout class:

o BorderLayout(): creates a border layout but with no gaps between the components.
o BorderLayout(int hgap, int vgap): creates a border layout with the given horizontal and
vertical gaps between the components.

Example of BorderLayout class: Using BorderLayout() constructor

FileName: Border.java

1. import java.awt.*;
2. import javax.swing.*;
3.
4. public class Border
5. {
6. JFrame f;
7. Border()
8. {

Prepard by K.Swathi Lakshmi Durga


Assistant Professor
Department of IT
9. f = new JFrame();
10.
11. // creating buttons
12. JButton b1 = new JButton("NORTH");; // the button will be labeled as NORTH
13. JButton b2 = new JButton("SOUTH");; // the button will be labeled as SOUTH
14. JButton b3 = new JButton("EAST");; // the button will be labeled as EAST
15. JButton b4 = new JButton("WEST");; // the button will be labeled as WEST
16. JButton b5 = new JButton("CENTER");; // the button will be labeled as CENTER
17.
18. f.add(b1, BorderLayout.NORTH); // b1 will be placed in the North Direction
19. f.add(b2, BorderLayout.SOUTH); // b2 will be placed in the South Direction
20. f.add(b3, BorderLayout.EAST); // b2 will be placed in the East Direction
21. f.add(b4, BorderLayout.WEST); // b2 will be placed in the West Direction
22. f.add(b5, BorderLayout.CENTER); // b2 will be placed in the Center
23.
24. f.setSize(300, 300);
25. f.setVisible(true);
26. }
27. public static void main(String[] args) {
28. new Border();
29. }
30. }

Output:

Prepard by K.Swathi Lakshmi Durga


Assistant Professor
Department of IT
Java GridLayout

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

1. GridLayout(): creates a grid layout with one column per component in a row.
2. GridLayout(int rows, int columns): creates a grid layout with the given rows and
columns but no gaps between the components.
3. 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.

Example of GridLayout class: Using GridLayout() Constructor

The GridLayout() constructor creates only one row. The following example shows the usage of
the parameterless constructor.

FileName: GridLayoutExample.java

FileName: GridLayoutExample1.java
Prepard by K.Swathi Lakshmi Durga
Assistant Professor
Department of IT
1. / import statements
2. import java.awt.*;
3. import javax.swing.*;
4.
5. public class GridLayoutExample1
6. {
7.
8. JFrame frameObj;
9.
10. // constructor
11. GridLayoutExample1()
12. {
13. frameObj = new JFrame();
14.
15. // creating 9 buttons
16. JButton btn1 = new JButton("1");
17. JButton btn2 = new JButton("2");
18. JButton btn3 = new JButton("3");
19. JButton btn4 = new JButton("4");
20. JButton btn5 = new JButton("5");
21. JButton btn6 = new JButton("6");
22. JButton btn7 = new JButton("7");
23. JButton btn8 = new JButton("8");
24. JButton btn9 = new JButton("9");
25.
26. // adding buttons to the frame
27. // since, we are using the parameterless constructor, therefore;
28. // the number of columns is equal to the number of buttons we
29. // are adding to the frame. The row count remains one.
30. frameObj.add(btn1); frameObj.add(btn2); frameObj.add(btn3);
31. frameObj.add(btn4); frameObj.add(btn5); frameObj.add(btn6);
32. frameObj.add(btn7); frameObj.add(btn8); frameObj.add(btn9);
33. // setting the grid layout

Prepard by K.Swathi Lakshmi Durga


Assistant Professor
Department of IT
34. // a 3 * 3 grid is created with the horizontal gap 20
35. // and vertical gap 25
36. frameObj.setLayout(new GridLayout(3, 3, 20, 25));
37. frameObj.setSize(300, 300);
38. frameObj.setVisible(true);
39. }
40. // main method
41. public static void main(String argvs[])
42. {
43. new GridLayoutExample();
44. }
45. }

Output:

Java FlowLayout

The Java FlowLayout class is used to arrange the components in a line, one after another (in a
flow). It is the default layout of the applet or panel.

Fields of FlowLayout class

1. public static final int LEFT


2. public static final int RIGHT
Prepard by K.Swathi Lakshmi Durga
Assistant Professor
Department of IT
3. public static final int CENTER
4. public static final int LEADING
5. public static final int TRAILING

Constructors of FlowLayout class

1. FlowLayout(): creates a flow layout with centered alignment and a default 5 unit
horizontal and vertical gap.
2. FlowLayout(int align): creates a flow layout with the given alignment and a default 5
unit horizontal and vertical gap.
3. FlowLayout(int align, int hgap, int vgap): creates a flow layout with the given
alignment and the given horizontal and vertical gap.

Example of FlowLayout class: Using FlowLayout() constructor

FileName: FlowLayoutExample.java

1. // import statements
2. import java.awt.*;
3. import javax.swing.*;
4.
5. public class FlowLayoutExample
6. {
7.
8. JFrame frameObj;
9.
10. // constructor
11. FlowLayoutExample()
12. {
13. // creating a frame object
14. frameObj = new JFrame();
15.
16. // creating the buttons
17. JButton b1 = new JButton("1");

Prepard by K.Swathi Lakshmi Durga


Assistant Professor
Department of IT
18. JButton b2 = new JButton("2");
19. JButton b3 = new JButton("3");
20. JButton b4 = new JButton("4");
21. JButton b5 = new JButton("5");
22. JButton b6 = new JButton("6");
23. JButton b7 = new JButton("7");
24. JButton b8 = new JButton("8");
25. JButton b9 = new JButton("9");
26. JButton b10 = new JButton("10");
27.
28.
29. // adding the buttons to frame
30. frameObj.add(b1); frameObj.add(b2); frameObj.add(b3); frameObj.add(b4);
31. frameObj.add(b5); frameObj.add(b6); frameObj.add(b7); frameObj.add(b8);
32. frameObj.add(b9); frameObj.add(b10);
33.
34. // parameter less constructor is used
35. // therefore, alignment is center
36. // horizontal as well as the vertical gap is 5 units.
37. frameObj.setLayout(new FlowLayout());
38.
39. frameObj.setSize(300, 300);
40. frameObj.setVisible(true);
41. }
42.
43. // main method
44. public static void main(String argvs[])
45. {
46. new FlowLayoutExample();
47. }
48. }

Output:

Prepard by K.Swathi Lakshmi Durga


Assistant Professor
Department of IT
Prepard by K.Swathi Lakshmi Durga
Assistant Professor
Department of IT

You might also like