0% found this document useful (0 votes)
69 views21 pages

SWING Lab1

The document provides an example of creating a basic GUI in Java using Swing components. It discusses containers like Frame, Panel and Dialog that can hold other components. It then shows how to create a simple GUI with a JFrame containing a JButton. It demonstrates adding multiple buttons and explains why they overlap without a layout manager. Finally, it discusses common layout managers like BorderLayout, FlowLayout and GridBagLayout and provides code samples for basic event handling using ActionListener.

Uploaded by

isayashpbende26
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)
69 views21 pages

SWING Lab1

The document provides an example of creating a basic GUI in Java using Swing components. It discusses containers like Frame, Panel and Dialog that can hold other components. It then shows how to create a simple GUI with a JFrame containing a JButton. It demonstrates adding multiple buttons and explains why they overlap without a layout manager. Finally, it discusses common layout managers like BorderLayout, FlowLayout and GridBagLayout and provides code samples for basic event handling using ActionListener.

Uploaded by

isayashpbende26
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/ 21

• All components in swing are JComponent which can be

added to container classes.

• What is a container class?


• Container classes are classes that can have other components
on it. So for creating a GUI, we need at least one container
object. There are 3 types of containers.
• Panel: It is a pure container and is not a window in itself. The
sole purpose of a Panel is to organize the components on to a
window.
• Frame: It is a fully functioning window with its title and icons.
• Dialog: It can be thought of like a pop-up window that pops
out when a message has to be displayed. It is not a fully
functioning window like the Frame.
Java GUI Example
Example: To learn to design GUI in Java
Step 1) Write the following code into an editor
import javax.swing.*;
class gui
{
public static void main(String args[])
{
JFrame frame = new JFrame("My First GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
JButton button = new JButton("Press");
frame.getContentPane().add(button); // Adds Button to content pane of frame
frame.setVisible(true);
}
}

Step 2) Save, Compile, and Run the code.


Step 3) Now let's Add a Button to our frame.
import javax.swing.*;
class gui
{
public static void main(String args[])
{
JFrame frame = new JFrame("My First GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
JButton button1 = new JButton("Press");
frame.getContentPane().add(button1);
frame.setVisible(true);
}
}
• Step 4) Execute the code. You will get a big
button
Step 5) How about adding two buttons?write the following code into an editor.
import javax.swing.*;
class gui
{
public static void main(String args[])
{
JFrame frame = new JFrame("My First GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
JButton button1 = new JButton("Button 1");
JButton button2 = new JButton("Button 2");
frame.getContentPane().add(button1);
frame.getContentPane().add(button2);
frame.setVisible(true);
}
}
Step 6) Save , Compile , and Run the program.
Step 7) Unexpected output =? Buttons are getting overlapped.
• Java Layout Manger
• The Layout manager is used to layout (or
arrange) the GUI java components inside a
container.There are many layout managers,
but the most frequently used are-
Java BorderLayout

A BorderLayout places components in up to five areas: top, bottom, left, right,


and center. It is the default layout of frame or window. The BorderLayout
provides five constants for each region:
public static final int NORTH
public static final int SOUTH
public static final int EAST
public static final int WEST
public static final int CENTER
Java FlowLayout

FlowLayout is the default layout manager for every JPanel. It simply lays out
components in a single row one after the other.
Java GridBagLayout

It is the more sophisticated of all layouts. It aligns components by placing


them within a grid of cells, allowing components to span more than one cell.
//Usually you will require both swing and awt //Creating the panel at bottom and adding
packages components
// even if you are working with just swings. JPanel panel = new JPanel();
// the panel is not visible in output
import javax.swing.*; JLabel label = new JLabel("Enter Text");
import java.awt.*; JTextField tf = new JTextField(10); // accepts upto
class gui 10 characters
{
JButton send = new JButton("Send");
public static void main(String args[]) JButton reset = new JButton("Reset");
{ panel.add(label); // Components Added using Flow
//Creating the Frame Layout
panel.add(tf);
JFrame frame = new JFrame("Chat Frame"); panel.add(send);
frame.setDefaultCloseOperation(JFrame.EXIT_ panel.add(reset);
ON_CLOSE); // Text Area at the Center
JTextArea ta = new JTextArea();
frame.setSize(400, 400);
//Creating the MenuBar and adding components //Adding Components to the frame
JMenuBar mb = new JMenuBar(); frame.getContentPane().add(BorderLayout.SOUTH,
JMenu m1 = new JMenu("FILE"); panel);
JMenu m2 = new JMenu("Help");
mb.add(m1); mb.add(m2); frame.getContentPane().add(BorderLayout.NORTH,
JMenuItem m11 = new JMenuItem("Open"); mb);
JMenuItem m22 = new JMenuItem("Save as"); frame.getContentPane().add(BorderLayout.CENTE
R, ta);
m1.add(m11);
frame.setVisible(true); } }
Event and Listener (Java Event Handling)

Changing the state of an object is known as an event. For example, click on button, dragging
mouse etc. The java.awt.event package provides many event classes and Listener interfaces for
event handling.

Java Event classes and Listener interfaces

Event Classes Listener Interfaces

ActionEvent ActionListener
MouseEvent MouseListener and
MouseMotionListener
MouseWheelEvent MouseWheelListener
KeyEvent KeyListener
ItemEvent ItemListener
TextEvent TextListener
AdjustmentEvent AdjustmentListener
WindowEvent WindowListener
ComponentEvent ComponentListener
ContainerEvent ContainerListener
FocusEvent FocusListener
Steps to perform Event Handling

Following steps are required to perform event handling:


--Register the component with the Listener
Registration Methods
--For registering the component with the Listener, many classes provide the registration
methods. For example:
Button
public void addActionListener(ActionListener a){}
MenuItem
public void addActionListener(ActionListener a){}
TextField
public void addActionListener(ActionListener a){}
public void addTextListener(TextListener a){}
TextArea
public void addTextListener(TextListener a){}
Checkbox
public void addItemListener(ItemListener a){}
Choice
public void addItemListener(ItemListener a){}
List
public void addActionListener(ActionListener a){}
public void addItemListener(ItemListener a){}
Java Event Handling Code

• We can put the event handling code into one


of the following places:
• Within class
• Other class
• Anonymous class (A class that have no name is
known as anonymous inner class in java)
//Code to display using JFrame with actionlistener

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class AEvent extends JFrame implements ActionListener
{
AEvent() public void actionPerformed(ActionEvent e)
{ {
//create components System.exit(0);
Button b=new Button("quit"); }
b.setBounds(100,120,80,30); public static void main(String args[])
{
//register listener new AEvent();
//passing current instance }
b.addActionListener(this); }

//add components and set size, layout and visibility


add(b);
setSize(300,300);
setLayout(null);
setVisible(true);
}
Java event handling by implementing ActionListener

import java.awt.*;
import java.awt.event.*;
class AEvent extends Frame implements ActionListener{
TextField tf;
AEvent()
{ public void actionPerformed(ActionEvent e)
//create components {
tf=new TextField(); tf.setText("Welcome");
tf.setBounds(60,50,170,20); }
Button b=new Button("click me"); public static void main(String args[])
b.setBounds(100,120,80,30); {
new AEvent();
//register listener }
b.addActionListener(this);//passing current instance

//add components and set size, layout and visibility


add(b);add(tf);
setSize(300,300);
setLayout(null);
setVisible(true);
}
{
import java.awt.*; headerLabel.setText("Control in action: Button");
import java.awt.event.*;
import javax.swing.*; JButton okButton = new JButton("OK");
JButton submitButton = new JButton("Submit");
public class SwingControlDemo { JButton cancelButton = new JButton("Cancel");
private JFrame mainFrame; okButton.setActionCommand("OK");
private JLabel headerLabel; submitButton.setActionCommand("Submit");
cancelButton.setActionCommand("Cancel");
private JLabel statusLabel;
private JPanel controlPanel; okButton.addActionListener(new ButtonClickListener());
public SwingControlDemo()
{ submitButton.addActionListener(new
prepareGUI(); ButtonClickListener());
}
private void prepareGUI(){ cancelButton.addActionListener(new
mainFrame = new JFrame("Java SWING Examples"); ButtonClickListener());
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1)); controlPanel.add(okButton);
controlPanel.add(submitButton);
headerLabel = new JLabel("",JLabel.CENTER );
controlPanel.add(cancelButton);
statusLabel = new JLabel("",JLabel.CENTER);
statusLabel.setSize(350,100); mainFrame.setVisible(true);
}
mainFrame.addWindowListener(new WindowAdapter() private class ButtonClickListener implements
{ ActionListener
public void windowClosing(WindowEvent windowEvent) {
{ public void actionPerformed(ActionEvent e)
System.exit(0); {
} String command = e.getActionCommand();
});
if( command.equals( "OK" )) {
controlPanel = new JPanel(); statusLabel.setText("Ok Button clicked.");
controlPanel.setLayout(new FlowLayout()); } else if( command.equals( "Submit" ) ) {
mainFrame.add(headerLabel); statusLabel.setText("Submit Button clicked.");
mainFrame.add(controlPanel); } else {
mainFrame.add(statusLabel); statusLabel.setText("Cancel Button clicked.");
mainFrame.setVisible(true); }
} }
}
public static void main(String[] args)
{
SwingControlDemo swingControlDemo = new
SwingControlDemo();
swingControlDemo.showEventDemo();
Swing and database connectivity

You might also like