0% found this document useful (0 votes)
17 views73 pages

Layout Managers

The document outlines the curriculum for a course on Object Oriented Programming using Java, focusing on imparting Java programming skills and knowledge of the object-oriented paradigm. It covers various AWT controls and layout managers, including FlowLayout, GridLayout, BorderLayout, and CardLayout, providing examples and code snippets for each. The course aims to equip students with the ability to design desktop applications using Java's latest APIs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views73 pages

Layout Managers

The document outlines the curriculum for a course on Object Oriented Programming using Java, focusing on imparting Java programming skills and knowledge of the object-oriented paradigm. It covers various AWT controls and layout managers, including FlowLayout, GridLayout, BorderLayout, and CardLayout, providing examples and code snippets for each. The course aims to equip students with the ability to design desktop applications using Java's latest APIs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 73

AMITY INSTITUTE OF INFORMATION

TECHNOLOGY

BCA B,C & D IT 247


Semester-III
Object Oriented Programming Using
Java
Prof(Dr)Laxmi Ahuja
Learning Objectives
•Imparting java programming skill to students

•Knowledge of object oriented paradigm in


context of Java programming language

•Designing desktop applications using latest


Java based API
Contents to be Covered
• AWT CONTROLS
• Layout Introduction
• LAYOUT MANAGERS
JAVA AWT Controls
• The AWT supports following types of controls
• Labels
• Check boxes
• Choice
• Lists
• Scrollbars
• Menu Bars
LAYOUT
• The arrangement of the components inside a container is called
Layout and used in the graphics programming of Java
• It is an interface that is implemented by all classes of layout
managers

• FlowLayout
• BorderLayout
• GridLayout
• GridBagLayout
• CardLayout
• GroupLayout
• BoxLayout
• NullLayout
Java--Layout
Managers
Layout Managers
• A layout Manager arranges the child components of
a Container

• It positions and sets the size of components within


the containers display area according to a particular
layout scheme

• The layout managers' job is to fit the components into


the available area, while maintaining the proper
spatial relationship between the component
Adding Components via Layouts
Arranging components on User Interface
By using Layout Manager ,programmer can gain more control over the layout of an
Interface

The AWT contains 5 basic layout Managers:

• FlowLayout
• GridLayout
• BorderLayout
• CardLayout
• GridBagLayout

• To set a particular layout manager, an object of that type is created first and then
the setLayout method is used
FlowLayout
• FlowLayout is a simple layout manager that tries to arrange components with
their preferred sizes,from left to right and top to bottom in the display.

• It’s a default Layout of Applets and Panels

• Components are added to the container from left to right. When there is no
space in a row, components get added in the next row from left to right and so
on.

• A FlowLayout can have a specified justification of LEFT,CENTER or RIGHT.

• By default ,a FlowaLayout uses CENTER justification,meaning that all


component are centered within the area allotted to them
Cont…
FlowLayout is the default for Panel Components like Applet

• FlowLayout f=new FlowLayout();


• FlowLayout f=new FlowLayout(align);
• Align could be Flowlayout.LEFT or CENTER or RIGHT
• FlowLayout f=new FlowLayout(align,hgap,vgap);

• hgap horizontal gap between components in pixel


• Vgap Verticalgap between components in pixel
FlowLayout—display button
import from
java.applet.*; left to right
/*<applet code=flow1 width=300 height=400>
</applet>*/
public class flow1 extends Applet
{
FlowLayout fl=new FlowLayout();
// FlowLayout fl=new FlowLayout(FlowLayout.LEFT);Place button on left side
//FlowLayout fl=new FlowLayout(FlowLayout.LEFT,30,20);//Provide spaces between button
Button b1=new Button("Submit");
Button b2=new Button("Play");
Button b3=new Button("Stop");
Button b4=new Button("Reset");
public void init()
{
setLayout(fl);
add(b1);
add(b2);
add(b3);
add(b4);}}
Flow Layout
import java.awt.*;
import java.applet.*;
/*<applet code=flow1 width=300 height=400>
</applet>*/
public class flow1 extends Applet
{
public void init()
{
Button b1;
FlowLayout f1;
f1=new FlowLayout(FlowLayout.LEFT,10,10);
setLayout(f1);
b1=new Button("hi");
add(b1);}}
FlowLayout Program

import java.awt.*;
import java.awt.event.*;
class frame1 extends Frame
{
frame1(String s)
{
super(s);
setLayout(new FlowLayout());
for(int i=1;i<=9;i++)
add(new Button("Button no:" +i));
setVisible(true);
setSize(300,300);
}
public static void main(String args[])
{
frame1 f=new frame1("FlowLayout");}}
GridLayout
• Components are arranged in the form of Grid consisting of rows and
columns.
• Components are added from left to right and top to bottom in the grid
• Components are arbitrarily resized to fit in the resulting areas; their
minimum and preferred sizes are consequently ignored.
• GridLayout is most useful for arranging very regularly ,identically sized
objects
• GridLayout takes the number of rows and columns in a constructor
GridLayout (3,2)
Grid Layout can be created :

• GridLayout g=new GidLayout(rows,columns);


• GridLayout g=new GidLayout(rows,columns,hgap,vgap);
Grid Layout
import java.awt.*;
import java.applet.*;
/*<applet code=grid1 width=300 height=400>
</applet>*/
public class grid1 extends Applet
{
Button b1;
Button b2;
Button b3;
Button b4;
public void init()
{
GridLayout gl=new GridLayout(2,2);
setLayout(gl);
b1=new Button("hello");
b2=new Button("hello");
b3=new Button("hello");
b4=new Button("hello");
add(b1); add(b2); add(b3); add(b4);}}
GridLayout Program
import java.awt.*;
import java.awt.event.*;
class Grid1 extends Frame
{
Grid1(String s)
{
super(s);
setLayout(new GridLayout(3,4));
for(int i=1;i<=12;i++)
add(new Button("Button no:" +i));
setVisible(true);
setSize(300,300);
}
public static void main(String args[])
{
Grid1 f=new Grid1("FlowLayout");}}
BorderLayout
In this Layout container is divided into five geographical locations:
• North
• South
• East
• West
• Center
with some padding between
• Border Layout is the default layout for Window and Frame Objects
• Because each component is associated with a direction.BorderLayout can manage
at most five components:
• Center component is the largest and other components surrounded it will be smaller
• A border layout is created by either using a zero argument constructor or the one
which takes hgap and vgap as arguments.
• Once the layout manager is set the components are added by using :
• add(place,component)
Border Layout

import java.awt.*;
import java.applet.*;
/*<applet code=border1 width=300 height=400>
</applet>*/
public class border1 extends Applet
{
public void init()
{
Button b1;
Button b2;
Button b3;
Button b4;
BorderLayout b=new BorderLayout();
setLayout(b);
b1=new Button("hello");
b2=new Button("hi");
b3=new Button("wow");
b4=new Button("anu");
add("North" ,b1);
add("South",b2);
add("East",b3);
add("West",b4);}}
BorderLayout Program
import java.awt.*;
import java.awt.event.*;
class border1 extends Frame
{
border1(String s)
{
super(s);
setLayout(new BorderLayout());

add(new Button("North"),BorderLayout.NORTH);
add(new Button("South"),BorderLayout.SOUTH);
add(new Button("East"),BorderLayout.EAST);
add(new Button("West"),BorderLayout.WEST);
add(new Button("Center"),BorderLayout.CENTER);
setVisible(true);
setSize(300,300);
}
public static void main(String args[])
{
border1 f=new border1("FlowLayout");}}
USE of Three Layout Managers
import java.awt.*;
import java.applet.*;
/*<applet code=flow1 width=300 height=400>
</applet>*/
public class flow1 extends Applet
{
Panel p1=new Panel();
Panel p2=new Panel();
GridLayout gl=new GridLayout(2,2,2,2);
BorderLayout b=new BorderLayout (2,2);
Button b1=new Button("l1");
Button b2=new Button("l2");
Button b3=new Button("l3");
Button b4=new Button("l4");
Button b5=new Button("l5");
Button b6=new Button("l6");
Button b7=new Button("l7");
Button b8=new Button("l8");
public void init()
{
p1.setLayout(gl);
p2.setLayout(b);
//Add buttons to panels
p1.add(b1);
p1.add(b2);
p1.add(b3);
p1.add(b4);
p1.add(b5);
p1.setBackground(Color.red);
//Add panel p1 to p2
p2.add("North",p1);
p2.add("West",b5);
p2.add("East",b6);
p2.add("South",b7);
p2.add("Center",b8);
p1.setBackground(Color.green);
add(p2);}}
GridBag Layout
• GridBagLayout is a very flexible layout manager that allows
you to position the component relative to one another using
constraints
• With GridBagLayout you can create almost any imaginable
layout
• A row or column in a GrodBagLayout expands to
accommodate the dimensions and constraints of the largest
component in its ranks
• Individual components may span more than one row and
column
GridBag Layout

• This layout is an extension of the grid layout manager.

A grid bag layout differs from grid layout manager as mentioned below:

• A component can occupy more than one cell in the grid


• The proportions between different rows and columns do not have to be equal
• Components inside grid cells can be arranged in different ways.
• The class GridBagLayout is used to create an object for the layout manager.
The class GridBagConstarints defines the properties of each component to be
placed into the cell.

• Overall layout is defined by the relationship between the GridBag,the


constraints and each component
GridBag Layout
import java.awt.*;
import java.applet.*;
/*<applet code=gridbag1 width=300 height=400>
</applet>*/
public class gridbag1 extends Applet
{
GridBagConstraints gbc=new GridBagConstraints();

void add1(Component c,int x,int y)


{
gbc.gridx=x;
gbc.gridy=y;
add(c,gbc);
}
public void init()
{
setLayout(new GridBagLayout());
int x,y;
add1(new Button("North"),x=1,y=0);
add1(new Button("West"),x=0,y=1);
add1(new Button("Center"),x=1,y=1);
add1(new Button("East"),x=2,y=1);
add1(new Button("South"),x=1,y=2);
}
}
Grid Bag Layout To make
buttons Larger

import java.awt.*;
import java.applet.*;
/*<applet code=gridbag1 width=300 height=400>
</applet>*/
public class gridbag1 extends Applet
{
GridBagConstraints gbc=new GridBagConstraints();

void add1(Component c,int x,int y)


{
gbc.gridx=x;
gbc.gridy=y;
add(c,gbc);
}
public void init()
{
setLayout(new GridBagLayout());
gbc.weightx=1.0;//this will expand my buttons
gbc.weighty=1.0;
gbc.fill=GridBagConstraints.BOTH;//expand button from both x
any //position
int x,y;
add1(new Button("North"),x=1,y=0);
add1(new Button("West"),x=0,y=1);
add1(new Button("Center"),x=1,y=1);
add1(new Button("East"),x=2,y=1);
add1(new Button("South"),x=1,y=2);
}
}
Merging of two rows and Columns
Program to merge Rows and Columns
import java.awt.*;
import java.applet.*;
/*<applet code=gridbag1 width=300 height=400>
</applet>*/
public class gridbag1 extends Applet
{
GridBagConstraints gbc=new GridBagConstraints();

void add1(Component c,int x,int y)


{
gbc.gridx=x;
gbc.gridy=y;
add(c,gbc);
}
public void init()
{
setLayout(new GridBagLayout());
gbc.weightx=1.0;//this will expand my buttons
gbc.weighty=1.0;
gbc.fill=GridBagConstraints.BOTH;//expand button from both x any //position
int x,y;
gbc.gridheight=2;//span two rows in terms of height
add1(new Button("North"),x=0,y=0);
gbc.gridheight=1;//set it back
add1(new Button("West"),x=1,y=0);
add1(new Button("Center"),x=2,y=0);
gbc.gridwidth=2;//span two columns
add1(new Button("East"),x=1,y=1);
add1(new Button("South"),x=1,y=2);
gbc.gridheight=2;//set it back
GridBag Layout
import java.awt.*;
import java.applet.*;
public class gridbag extends Applet
{
GridBagLayout gl=new GridBagLayout();
GridBagConstraints gbc=new GridBagConstraints();
public void init()
{
Button b1=new Button("hello");
Button b2=new Button("hello");
Button b3=new Button("hello");
Button b4=new Button("hello");
gbc.fill=GridBagConstraints.BOTH;
gbc.anchor=GridBagConstraints.CENTER;
gbc.gridwidth=1;
gbc.weightx=1.0;
gl.setConstraints(b1,gbc);
add(b1);
gbc.gridwidth=GridBagConstraints.REMAINDER;
gl.setConstraints(b2,gbc);
add(b2);
gbc.gridwidth=GridBagConstraints.REMAINDER;
gl.setConstraints(b3,gbc);
add(b3);
gbc.weightx=0.0;
gbc.gridheight=1;
gl.setConstraints(b4,gbc);
add(b4);
gbc.gridwidth=GridBagConstraints.REMAINDER;
gl.setConstraints(b4,gbc);
add(b4);
/*gbc.add("Button1",b1);
gbc.add("Button2",b2);
gbc.add("Button3",b3);
gbc.add("Button4",b4);*/
}}
CardLayout
• CardLayout is a special layout manager for creating the effect
of a stack of cards
• Instead of arranging all of the container’s components, it
displays only one at a time
• Arranges each component in the container as acrd
• Only one card is visible and the container acts as a satck of
cards
• Card layout is a group of containers or components that are
displayed one at a time.
• Each container in the group is called as card.
• Use of Panel to each card and each panel can have any
number of components.
CardLayout cl=new CardLayout ();

setLayout () method is used to assign this manager for the container.

• Method add(string,container)is used to add specific container or


component to the card.
• CardLayout class methods:
• next(),previous(),first(),last()

• Card can be displayed by using show () method

• Only one card can be viewed at a time


CardLayout Program
import java.awt.*;
import java.applet.*;
/*<applet code=card1 width=300 height=400>
</applet>*/
public class card1 extends Applet
{
CardLayout c=new CardLayout();
public void init()
{
setLayout(c);
add(new Button("one"),"one");
add(new Button("two"),"two");
add(new Button("three"),"three");
setVisible(true);
setSize(300,300);
}
public boolean action(Event e,Object o)
{
c.next(this);
return true;}}
Card Layout
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*<applet code=card1 width=300 height=400>
</applet>*/
public class card1 extends Applet implements ActionListener
{
Button b1;
Button b2;
Button b3;
Button b4;
CardLayout c;
Panel p;
public void init()
{
p=new Panel();
add(p);
c=new CardLayout();
p.setLayout(c);
b1=new Button("one");
b2=new Button("two");
b3=new Button("three");
b4=new Button("Four");
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
p.add("Button1",b1);
p.add("Button2",b2);
p.add("Button3",b3);
p.add("Button4",b4);
}
public void actionPerformed(ActionEvent e)
{
c.next(p);
}}
Java Swing
Java swing also design windows based API
and also part of Java foundation classes

Java Swing is part of Java Foundation


classes(JFC)
Built on top of AWT API
• Applets run by HTML
• Applet Components are OS dependent

• Swing components develop using java(Pure java Standalone


application)
• Package java extended pkg javax.swing
• Run by JVM only (no need to depend on any other technology)
• Independent of OS
• Components creation logic developed using java only(no need to
depend on any other programming language to create the
components of swing
DIFFERENCE
AWT SWING
• Platform dependent. Look and
• Platform Independent
Feel of AWT depends upon the
platform • Light weight component(They
• Heavy weight component need less resources to perform)
• Swing supports pluggable look
(As they have to access resources
from the platform or OS) and feel
• Does not support pluggable look
and feel(if you have develop
AWT application in one of the
OS then same look and feel will
not appear in another OS)
DIFFERNCE
AWT SWING
• Less number of components for • Large number of components
example AWT does not support for example
Radio class we design it with the help
of Checkbox and checkboxgroup class

• It does not support MVC(Model view


Controller) architecture(Here Model • It follows MVC architecture
represents the data ,View represents
the presentation Controller acts an
an interface between model and
view
Important Methods of
Component Class
• void add(component c)//Add component to a container

• void setSize(int width,int height)//set size of window

• void setLayout(LayoutManger)//set layout

• void setVisible(boolean b)//true or false


Important Java Swing classes
Jbutton class—used to create a button
• Jbutton()//Not text/icon
• Jbutton(String s)//click
• Jbutton(Icon i)//display icon

• setText(String)
• getText()

JRadioButton class
• JRadioButton()
• JRadioButton(String s)
• JRadioButton( String s ,Boolean select)
• setText(String)//methods
• getText()
JTextArea class
• JTextArea(String s)
• JTextArea(String s,Boolean select)
JLabel
import java.awt.*;
import javax.swing.*;

public class JLabelDemo extends JApplet


{
public void init()
{
Container contentpane=getContentPane();
ImageIcon i=new ImageIcon("Diamonds.gif");
JLabel j=new JLabel("diamonds",i,JLabel.CENTER);
contentpane.add(j);
}
}
JTextField
import java.awt.*;
import javax.swing.*;

public class JTextFieldDemo extends JApplet


{
public void init()
{
Container contentpane=getContentPane();
contentpane.setLayout(new FlowLayout());
JTextField t=new JTextField(15);
contentpane.add(t);
}
}
JButton
public class JButtonDemo extends JApplet implements ActionListener
{
JTextField t;
ImageIcon i;JButton b;
public void init()
{
Container contentpane=getContentPane();
contentpane.setLayout(new FlowLayout());
i=new ImageIcon("Clouds.gif");
b=new JButton(i);
b.setActionCommand("clouds");
b.addActionListener(this);
contentpane.add(b);

t=new JTextField(15);
contentpane.add(t);
}
public void actionPerformed(ActionEvent e)
{
t.setText(e.getActionCommand());
}
}
The JFrame Class
Frames are a powerful feature of Swing. You can create a window for your
application by using the JFrame class.

JFrame frame = new JFrame(“Frame Window”);


After a frame window is created, you can use the add( )
method to add components to it.

You can display a frame window by invoking the


setVisible( ) method. You can set the size of a
frame window by invoking the setSize( ) method
50
JFrame Class Cont..
import java.awt.*;
import javax.swing.*;
public class fm extends JFrame
{
JFrame f1;
public fm()
{
f1=new JFrame("frame window");
f1.setVisible(true);
f1.setSize(300,300);
}
public static void main(String args[])
{
new fm(); } 51
The JPanel Class

The JPanel class is a simple container class.

It provides space in which you can add other components.

JPanel p1=new Jpanel( )


getContentPane() should add components to
contentpane.

Contentpane object for adding components

52
JPanel class Cont….
// Customer.java

Customer.java

53
The JRadioButton Class
JRadioButton r1,r2,r3,r4;

r1=new JRadioButton(“String”);

Customer1.java

54
The JRadioButton Class
Cont..
JRadioButton r1,r2;
ButtonGroup g1;

r1=new JRadioButton(“String”);
g1= new ButtonGroup();
g1.add(r1);
g1.add(r2);

Customer2.java
55
The JPasswordField Class
JPasswordField tp1;
JTextArea ta1;

tp1=new JPasswordField(10);
ta1= new JTextArea(row, column);
ta1= new JTextArea(3,8);

Customer3.java
56
JFrame Window
Example of Jframe window:

Fr2.java

57
The JMenu, JMenuBar, JMenuItem
Class
JMenuBar first;
Jmenu one , two, three;
JMenuItem m1,m2,m3,m4,m5,m6,m7;

Fr3.java

58
The Scrollbar Class
It is specified by integer values that are set during Scrollbar
construction.

Scrollbar sb;
sb = new Scrollbar(Scrollbar.X, initial_value, slider_width, range_from,
range_to);

sb = new Scrollbar(Scrollbar.HORIZONTAL, 0, 60,


0, 300);
add(sb);

Scrl.java

59
Example of Swing using FlowLayout

import javax.swing.*;
import java.awt.*;
/*<applet code=grid1 width=300 height=400>
</applet>*/
public class grid1 extends JApplet {
public void init() {
//create a variable that references
//the content pane of the applet
Container appletContainer = getContentPane();
// set the layout to FlowLayout
appletContainer.setLayout( new FlowLayout() );
// creates five buttons
JButton button1 = new JButton("Button1");
JButton button2 = new JButton("Button2");
JButton button3 = new JButton("Button3");
// add the buttons to the applet's content pane
appletContainer.add(button1);
appletContainer.add(button2);
appletContainer.add(button3);
}
}
import javax.swing.*;
import java.awt.*;
public class grid1 {
public static void main(String arg[]) {
JFrame frame = new JFrame("SetBounds Method Test");
frame.setSize(375, 250);
// Setting layout as null
frame.setLayout(null);
// Creating Button
JButton button = new JButton("Hello Java");
// Setting position and size of a button
button.setBounds(80,30,120,40);
frame.add(button);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Java AWT TextField Example with ActionListener
import java.awt.event.*;
public class grid1 extends Frame implements ActionListener{
TextField tf1,tf2,tf3;
Button b1,b2;
grid1(){
tf1=new TextField();
tf1.setBounds(50,50,150,20);
tf2=new TextField();
tf2.setBounds(50,100,150,20);
tf3=new TextField();
tf3.setBounds(50,150,150,20);
tf3.setEditable(false);
b1=new Button("+");
b1.setBounds(50,200,50,50);
b2=new Button("-");
b2.setBounds(120,200,50,50);
b1.addActionListener(this);
b2.addActionListener(this);
add(tf1);add(tf2);add(tf3);add(b1);add(b2);
setSize(300,300);
setLayout(null);
setVisible(true);
public void actionPerformed(ActionEvent e) {
String s1=tf1.getText();
String s2=tf2.getText();
int a=Integer.parseInt(s1);
int b=Integer.parseInt(s2);
int c=0;
if(e.getSource()==b1){
c=a+b;
}else if(e.getSource()==b2){
c=a-b;
}
String result=String.valueOf(c);
tf3.setText(result);
}
public static void main(String[] args) {
new grid1();
} }
Box Layout
Box Layout
BoxLayout is used to arrange the component either vertically or horizontally

BoxLayout class is found in javax.swing package


BoxLayout is implemented with the help of Box class defined in javax.swing package

Constructor of BoxLayout class:

BoxLayout(Container c,int axis);// creates a box layout that arranges the components with the given axis.

BoxLayout provides four constants. They are as follows:

1. public static final int X_AXIS


2. public static final int Y_AXIS
3. public static final int LINE_AXIS
4. public static final int PAGE_AXIS
//creates a box layout that arranges the components with the given axis

Axis-orientation are(components are arranged accordingly with these axis)


• X_AXIS
• Y_AXIS
• LINE_AXIS
• PAGE_AXIS
The BoxLayout class puts components in a single row or column. This class respects the components'
requested maximum sizes and also lets you align components.
import java.awt.*;
import javax.swing.*;
public class box1 extends Frame {
Button buttons[];
public box1 () {
buttons = new Button [5];

for (int i = 0;i<5;i++) {


buttons[i] = new Button ("Button " + (i + 1));
add (buttons[i]);
}
setLayout (new BoxLayout (this, BoxLayout.Y_AXIS));
setSize(400,400);
setVisible(true);
}
public static void main(String args[]){
box1 b=new box1();
} }
CardLayout in Swing
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class box1 extends JFrame implements


ActionListener{
CardLayout card;
JButton b1,b2,b3;
Container c;
box1(){

c=getContentPane();
card=new CardLayout(40,30);
//create CardLayout object with 40 hor space and 30
ver space
c.setLayout(card);
b1=new JButton("Apple");
b2=new JButton("Boy");
b3=new JButton("Cat");
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);

c.add("a",b1);c.add("b",b2);c.add("c",b3);

}
public void actionPerformed(ActionEvent e) {
card.next(c);
}

public static void main(String[] args) {


box1 cl=new box1();
cl.setSize(400,400);
cl.setVisible(true);
cl.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
GroupLayout

The GroupLayout class provides methods such as


createParallelGroup() and createSequentialGroup() to create
groups.

GroupLayout treats each axis independently. That is, there is a


group representing the horizontal axis, and a group representing
the vertical axis. Each component must exists in both a
horizontal and vertical group, otherwise an IllegalStateException
is thrown during layout, or when the minimum, preferred or
maximum size is requested.
Group Layout
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
public class GroupExample {
public static void main(String[] args) {
JFrame frame = new JFrame("GroupLayoutExample");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPanel = frame.getContentPane();
GroupLayout groupLayout = new
GroupLayout(contentPanel);

contentPanel.setLayout(groupLayout);

JLabel clickMe = new JLabel("Click Here");


JButton button = new JButton("This Button");
groupLayout.setHorizontalGroup(
groupLayout.createSequentialGroup()
.addComponent(clickMe)
.addGap(10, 20, 100)
.addComponent(button));
groupLayout.setVerticalGroup(

groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(clickMe)
.addComponent(button));
frame.pack();
frame.setVisible(true);
}
}
Null Layout
To use fix position of components we use Null Layout
THANK YOU

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy