SlideShare a Scribd company logo
CHAPTER SEVEN
JAVA
GRAPHICAL USER INTERFACE
GUI
Introduction to GUI
A graphical user interface (GUI) presents a user-friendly
mechanism for interacting with an application.
A GUI (pronounced "goo-ee") gives an application a distinctive
"look" and "feel."
It provides different applications with consistent, intuitive user
interface components allows users to be somewhat familiar with an
application, so that they can learn it more quickly and use it
more productively.
Overview Of Swing Components
Most Swing components are pure Java components they are
written, manipulated and displayed completely in Java.
They are part of the Java Foundation Classes (JFC)Java's
libraries for cross-platform GUI development. There are actually two
sets of GUI components in Java. Before Swing was introduced in J2SE
1.2, Java GUIs were built with components from the Abstract
Window Toolkit (AWT) in package java.awt.
When a Java application with an AWT GUI executes on
different Java platforms, the application's GUI components display
differently on each platform. i.e. GUI app in Windows differs from
Mac.
Together, the appearance and the way in which the user interacts with
the application are known as that application's look-and-feel.
Swing GUI components allow you to specify a uniform look-
and-feel for your application across all platforms or to use
each platform's custom look-and-feel.
Overview Of Swing Components
Overview Of Swing Components
An application can even change the look-and-feel during execution
to enable users to choose their own preferred look-and-feel.
Most Swing components are not tied to actual GUI components
supported by the underlying platform on which an application
executes.
Such GUI components are known as lightweight components. For
this reason, Swing GUI components are generally preferred.
Overview Of Swing Components
In Swing, classes that represent GUI components have names
beginning with the letter J.
Some examples are JButton, JLabel, JFrame, JTextField, JCheckBox,
JComboBox, JRadioButton, JMenu, Jslider and so on.
Altogether there are more than 250 new classes and 75 interfaces in
Swing, the figure shown below depicts the Java Swing class hierarchy.
Overview Of Swing Components
Overview Of Swing Components
Overview Of Swing Components
Overview Of Swing Components
1. JPanel is swing's version of the AWT class panel and uses the same
default layout, Flowlayout and it is descended directly from JComponent.
2. JFrame is swing's version of frame and is descended directly from that
class. 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 instead.
3. FlowLayout are used to arrange swing components from left to right
until there's no more space available. Then it begins a new row below it
and moves from left to right again. Each component in a FlowLayout gets
as much space as it needs and no more.
Overview Of Swing Components
4. GridLayout is a layout manager that lays out a container's components in
a rectangular grid in row by column manner as per provided by the user.
The container is divided into equal-sized rectangles, and one component
is placed in each rectangle.
5. JLabel descended from JComponent, is used to create text labels that
can not be edited live but are capable of modified in order.
6. AbstractButton is an abstract class which extends class JComponent and
provides a foundation for a family of button classes, including JButton.
JButton is a component the user clicks to trigger a specific action.
Overview Of Swing Components
7. JTextField allows editing of a single line of text. New features include
the ability to justify the text left, right, or center, and to set the text's
font.
8. JPasswordField (a direct subclass of JTextField ) you can suppress the
display of input. Each character entered can be replaced by echo
character. This allows confidential input for passwords, for example. By
default, the asterisk, *.
9. JTextArea allows editing of multiple lines of text. JTextArea can
be used in conjunction with class JScrollPane to achieve scrolling.
Overview Of Swing Components
The underlying JScrollPane can be forced to always or never have
either the vertical or horizontal scrollbar;
10.JRadioButton is similar to JCheckbox, except for the default
icon for each class. A set of radio buttons can be associated
as a group in which only one button at a time can be selected.
11.JCheckBox is not a member of a checkbox group. A checkbox can
be selected and deselected, AND it also displays its current state.
Overview Of Swing Components
12. JComboBox is like a drop down box. You can click a drop-down
arrow and select an option from a list. For example, when the
component has focus, pressing a key that corresponds to the first
character in some entry’s name selects that entry. A vertical scrollbar is
used for longer lists.
13. JMenubar can contain several JMenu’s. Each of the JMenu’s can
contain a series of JMenuItem ’s that it can let you select one. Swing
provides support for pull-down and popup menus.
Overview Of Swing Components
Java Swing Example:
Below is a java swing code for the traditional Hello World program.
import javax.swing.JFrame;
import javax.swing.JLabel; OUTPUT:
public class HelloWorldFrame extends JFrame {
HelloWorldFrame(){
JLabel jlbHelloWorld = new JLabel("Hello World");
add(jlbHelloWorld);
this.setSize(200, 100);
setVisible(true);
public static void main(String args[]) {
new HelloWorldFrame();
} } }
JFrame
JFrame is a Window with border, title and buttons. When JFrame is set
visible, an event dispatching 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 Constructors
JFrame() : Constructs a new frame that is initially invisible.
JFrame(String title) : Creates a new, initially invisible Frame with the
specified title.
JFrame Contnd…
Steps to create a JFrame windows are as follows:
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.
JFlowLayout
It is an AWT component used to arrange
swing components from left to right until
there’s no more space available.
import java.awt.FlowLayout.*;
import javax.swing.*;
public class FlowLayoutDemo extends JFrame{
FlowLayoutDemo() {
super("FlowLayout Source Demo");
JPanel panel = new JPanel();
add(panel);
panel.setLayout(new FlowLayout());
panel.add(new JLabel("JLabel 1"));
panel.add(new JButton("JButton 2"));
panel.add(new JCheckBox("JCheckBox 3"));
panel.add(new JTextField("Long-Named JTextField
4"));
panel.add(new JButton("JButton 5"));
setVisible(true);
setSize(300,350); }
public static void main(String[] args) {
FlowLayoutDemo flowLayout = new FlowLayoutDemo();
flowLayout.setDefaultCloseOperation(JFrame.EXIT_ON_
CLOSE);
}
}
JFlowLayout Contnd…
OUTPUT:
GridLayout
It is a layout manager that lays out a container’s components in a
rectangular grid. The container is divided into equal-sized rectangles,
and one component is placed in each rectangle.
GridLayout Constructors
GridLayout() : Creates a grid layout with a default of one column per component,
in a single row.
GridLayout(int rows, int cols) : Creates a grid layout with the specified number of
rows and columns.
JFrame Contnd…
Steps to create a JFrame windows with predefined layout are as follows:
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 up the content pane and components in a layout FlowLayout,
GridLayout etc.
Step 5: Set the default close operation.
Step 6: Make the JFrame visible.
JLabel
JLabel, descended from JComponent, is used to create text labels.
A JLabel object provides text instructions or information on a GUI;
display a single line of read-only text, an image or both text and image.
We use a Swing JLabel when we need a user interface component
that displays a message or an image.
JLabel Constructor
JLabel() : Creates a JLabel instance with no image and with an empty string for the title.
JLabel(Icon image) :Creates a JLabel instance with the specified image.
JLabel(String text) : Creates a JLabel instance with the specified text.
Introduction to Event Handling
Normally, a user interacts with an application’s GUI to indicate the tasks that the
application should perform.
For example, when you write an e-mail in an e-mail application, clicking the send button
tells the application to send the e-mail to the specified e-mail addresses.
GUIs are event driven. When the user interacts with a GUI component, the interaction
known as an event drives the program to perform a task.
Some common events (user interactions) that might cause an application to perform a task
include clicking a button, typing in a text field, selecting an item from a menu, closing a
window and moving the mouse.
 The code that performs a task in response to an event is called an event handler and the
overall process of responding to events is known as event handling.
More on Event Handling
Both AWT and Swing applications uses the AWT event-handling classes (in package java.awt.event).
Swing added a few new event handling classes (in package javax.swing.event), but they are not
frequently used.
AWT GUI Components (such as Button, TextField, ComboBox, CheckBox and Window) can trigger an
AWTEvent upon user’s activation.
More on Event Handling
ActionEvent & ActionListener
An ActionEvent is fired, when an action has been performed by the user.
For examples, when the user clicks a button, chooses a menu item, presses enter key in a text field.
The associated ActionListener interface declares only one abstract method, as follows:
ActionListner Method Definition:
public interface ActionListener extends java.util.eventlistener {
public void actionPerformed(ActionEvent evt); // called-back when an action
//has been performed
}
More on Event Handling
Example source code:
The example illustrates a source code fragment which clears a text in text field when the clear button pressed.
JPanel panel = new JPanel();
JTextfield1 jTextField1 = new JTextField();
JButton jBtnClear new JButton(“CLEAR”)
panel.setLayout(new GridLayout(1,2));
add(panel, BorderLayout.CENTER);
panel.add(jBtnClear);
panel.add(jTextField1);
jBtnClear.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
jTextfield1.setText("");
}});
JTextField
It allows editing/displaying of a single line of text.
When the user types data into them and presses the Enter key, an action
event occurs.
If the program registers an event listener, the listener processes the event
and can use the data in the text field at the time of the event in the program.
JTextField is an input area where the user can type in characters.
JTextField Contnd…
If you want to let the user enter multiple lines of text, you cannot use
JTextfield’s unless you create several of them.
The solution is to use JTextArea which enables the user to enter multiple lines
of text.
JTextField Constructor
JTextField() : Constructs a new TextField.
JTextField(String text): Constructs a new TextField initialized with the specified
text.
All search engine applications are of typical examples of JTextFields.
JButton
JButton is a component that the user clicks to trigger a specific action.
A Java application can use several types of buttons, including command
buttons, checkboxes, toggle buttons and radio buttons.
JButton Constructors
JButton() : Creates a button with no set text or icon.
JButton(Action a) : Creates a button where properties are taken from the
Action supplied.
JButton(String text) : Creates a button with text.
JComboBox
JComboBox is like a drop down box that you can click a drop-down arrow and select
an option from a list.
It generates ItemEvent. For example, when the component has focus, pressing
a key that corresponds to the first character in some entry’s name selects that
entry. A vertical scrollbar is used for longer lists.
JComboBox Constructors
JComboBox() : Creates a JComboBox with a default data model.
JComboBox(Object[] items) :Creates a JComboBox that contains the elements in the
specified array.
JComboBox(Vector items) : Creates a JComboBox that contains the elements in the
specified Vector.
JMenus
Swing provides support for pull-down and popup menus.
A JMenubar can contain several JMenus. And each of the JMenus can contain a series
of JMenuItems that you can select.
How Menus are created?
1. First, a JMenubar is created
2. Then, we attach all of the menus to this JMenubar.
3. Then we add JMenuItems to the JMenus.
4. The JMenubar is then added to the frame. By default, each JMenuItem added to a JMenu is
enabled, that is, it can be selected.
JMenus
import java.awt.*;
import java.awt.event.*;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JFrame;
public class JMenuDemo extends JFrame implements
ActionListener{
JTextArea jtAreaOutput;
JMenuBar mainMenuBar;
JMenu menu1, menu2, submenu;
JMenuItem New,subMenuItem;
JMenuDemo() {
super("Microsoft Word");
mainMenuBar = new JMenuBar();
menu1 = new JMenu("File");
menu1.setMnemonic(KeyEvent.VK_F);
mainMenuBar.add(menu1);
New = new JMenuItem("New");
New.addActionListener(this);
menu1.add(New);
menu1.addSeparator();
JMenus
// Sub Menu follows a seperator
submenu = new JMenu("Send To:");
submenu.setMnemonic(KeyEvent.VK_D);
subMenuItem = new JMenuItem("My Document");
subMenuItem.addActionListener(this);
submenu.add(subMenuItem);
menu1.add(submenu);
// Build second menu in the menu bar.
menu2 = new JMenu("Edit");
menu2.setMnemonic(KeyEvent.VK_E);
mainMenuBar.add(menu2);
// return mainMenuBar;
JPanel jplContentPane = new JPanel();
jplContentPane.setLayout(new FlowLayout());
jtAreaOutput = new JTextArea(5, 30);
jplContentPane.add(jtAreaOutput);
add(jplContentPane);
setSize(400, 300);
setVisible(true);
}
JMenus
public void actionPerformed(ActionEvent e) {
JMenuItem source = (JMenuItem) (e.getSource());
String s = "Menu Item source: " + source.getText();
jtAreaOutput.setText(s);
}
public static void main(String[] args) {
JMenuDemo frame= new JMenuDemo();
frame.setJMenuBar(frame.mainMenuBar);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_
CLOSE);
} }
The End

More Related Content

What's hot

Java Swing JFC
Java Swing JFCJava Swing JFC
Java Swing JFC
Sunil OS
 
Swing
SwingSwing
Gui
GuiGui
Graphical User Interface (Gui)
Graphical User Interface (Gui)Graphical User Interface (Gui)
Graphical User Interface (Gui)
Bilal Amjad
 
Swings
SwingsSwings
Complete java swing
Complete java swingComplete java swing
Complete java swing
jehan1987
 
Jdbc
JdbcJdbc
Swing and Graphical User Interface in Java
Swing and Graphical User Interface in JavaSwing and Graphical User Interface in Java
Swing and Graphical User Interface in Java
babak danyal
 
Java: GUI
Java: GUIJava: GUI
Java: GUI
Tareq Hasan
 
Java swing and events
Java swing and eventsJava swing and events
Java swing and events
Prathamesh sawant
 
Java swing
Java swingJava swing
Java swing
Nataraj Dg
 
GUI components in Java
GUI components in JavaGUI components in Java
GUI components in Java
kirupasuchi1996
 
GUI Programming in JAVA (Using Netbeans) - A Review
GUI Programming in JAVA (Using Netbeans) -  A ReviewGUI Programming in JAVA (Using Netbeans) -  A Review
GUI Programming in JAVA (Using Netbeans) - A Review
Fernando Torres
 
Swing and AWT in java
Swing and AWT in javaSwing and AWT in java
Swing and AWT in java
Adil Mehmoood
 
swingbasics
swingbasicsswingbasics
swingbasics
Arjun Shanka
 
Awt and swing in java
Awt and swing in javaAwt and swing in java
Awt and swing in java
Shehrevar Davierwala
 
java2 swing
java2 swingjava2 swing
java2 swing
guest0282b71
 
Java Swing
Java SwingJava Swing
Java Swing
Arkadeep Dey
 
Java Swing
Java SwingJava Swing
Java Swing
Shraddha
 
Java- GUI- Mazenet solution
Java- GUI- Mazenet solutionJava- GUI- Mazenet solution
Java- GUI- Mazenet solution
Mazenetsolution
 

What's hot (20)

Java Swing JFC
Java Swing JFCJava Swing JFC
Java Swing JFC
 
Swing
SwingSwing
Swing
 
Gui
GuiGui
Gui
 
Graphical User Interface (Gui)
Graphical User Interface (Gui)Graphical User Interface (Gui)
Graphical User Interface (Gui)
 
Swings
SwingsSwings
Swings
 
Complete java swing
Complete java swingComplete java swing
Complete java swing
 
Jdbc
JdbcJdbc
Jdbc
 
Swing and Graphical User Interface in Java
Swing and Graphical User Interface in JavaSwing and Graphical User Interface in Java
Swing and Graphical User Interface in Java
 
Java: GUI
Java: GUIJava: GUI
Java: GUI
 
Java swing and events
Java swing and eventsJava swing and events
Java swing and events
 
Java swing
Java swingJava swing
Java swing
 
GUI components in Java
GUI components in JavaGUI components in Java
GUI components in Java
 
GUI Programming in JAVA (Using Netbeans) - A Review
GUI Programming in JAVA (Using Netbeans) -  A ReviewGUI Programming in JAVA (Using Netbeans) -  A Review
GUI Programming in JAVA (Using Netbeans) - A Review
 
Swing and AWT in java
Swing and AWT in javaSwing and AWT in java
Swing and AWT in java
 
swingbasics
swingbasicsswingbasics
swingbasics
 
Awt and swing in java
Awt and swing in javaAwt and swing in java
Awt and swing in java
 
java2 swing
java2 swingjava2 swing
java2 swing
 
Java Swing
Java SwingJava Swing
Java Swing
 
Java Swing
Java SwingJava Swing
Java Swing
 
Java- GUI- Mazenet solution
Java- GUI- Mazenet solutionJava- GUI- Mazenet solution
Java- GUI- Mazenet solution
 

Similar to Z blue introduction to gui (39023299)

swings.pptx
swings.pptxswings.pptx
swings.pptx
Parameshwar Maddela
 
Swingpre 150616004959-lva1-app6892
Swingpre 150616004959-lva1-app6892Swingpre 150616004959-lva1-app6892
Swingpre 150616004959-lva1-app6892
renuka gavli
 
SWING USING JAVA WITH VARIOUS COMPONENTS
SWING USING  JAVA WITH VARIOUS COMPONENTSSWING USING  JAVA WITH VARIOUS COMPONENTS
SWING USING JAVA WITH VARIOUS COMPONENTS
bharathiv53
 
Awt, Swing, Layout managers
Awt, Swing, Layout managersAwt, Swing, Layout managers
Awt, Swing, Layout managers
swapnac12
 
Swing
SwingSwing
Swing
Fahim Khan
 
Swing
SwingSwing
Swing
Fahim Khan
 
Chap1 1 1
Chap1 1 1Chap1 1 1
Chap1 1 1
Hemo Chella
 
Chap1 1.1
Chap1 1.1Chap1 1.1
Chap1 1.1
Hemo Chella
 
Ajp notes-chapter-02
Ajp notes-chapter-02Ajp notes-chapter-02
Ajp notes-chapter-02
Ankit Dubey
 
Advance Java Programming (CM5I) 2.Swing
Advance Java Programming (CM5I) 2.SwingAdvance Java Programming (CM5I) 2.Swing
Advance Java Programming (CM5I) 2.Swing
Payal Dungarwal
 
Java AWT and Java FX
Java AWT and Java FXJava AWT and Java FX
Java AWT and Java FX
pratikkadam78
 
SWING.pptx
SWING.pptxSWING.pptx
SWING.pptx
SamyakJain710491
 
java presentation on Swings chapter java presentation on Swings
java presentation on Swings chapter java presentation on Swingsjava presentation on Swings chapter java presentation on Swings
java presentation on Swings chapter java presentation on Swings
MohanYedatkar
 
Computer Programming NC III - Java Swing.pptx
Computer Programming NC III - Java Swing.pptxComputer Programming NC III - Java Swing.pptx
Computer Programming NC III - Java Swing.pptx
jonathancapitulo2
 
14a-gui.ppt
14a-gui.ppt14a-gui.ppt
14a-gui.ppt
DrDGayathriDevi
 
SwingApplet.pptx
SwingApplet.pptxSwingApplet.pptx
SwingApplet.pptx
GEETHAS668001
 
Chapter iv(modern gui)
Chapter iv(modern gui)Chapter iv(modern gui)
Chapter iv(modern gui)
Chhom Karath
 
GUI Programming In Java
GUI Programming In JavaGUI Programming In Java
GUI Programming In Java
yht4ever
 
DSJ_Unit III.pdf
DSJ_Unit III.pdfDSJ_Unit III.pdf
DSJ_Unit III.pdf
Arumugam90
 
L11cs2110sp13
L11cs2110sp13L11cs2110sp13
L11cs2110sp13
karan saini
 

Similar to Z blue introduction to gui (39023299) (20)

swings.pptx
swings.pptxswings.pptx
swings.pptx
 
Swingpre 150616004959-lva1-app6892
Swingpre 150616004959-lva1-app6892Swingpre 150616004959-lva1-app6892
Swingpre 150616004959-lva1-app6892
 
SWING USING JAVA WITH VARIOUS COMPONENTS
SWING USING  JAVA WITH VARIOUS COMPONENTSSWING USING  JAVA WITH VARIOUS COMPONENTS
SWING USING JAVA WITH VARIOUS COMPONENTS
 
Awt, Swing, Layout managers
Awt, Swing, Layout managersAwt, Swing, Layout managers
Awt, Swing, Layout managers
 
Swing
SwingSwing
Swing
 
Swing
SwingSwing
Swing
 
Chap1 1 1
Chap1 1 1Chap1 1 1
Chap1 1 1
 
Chap1 1.1
Chap1 1.1Chap1 1.1
Chap1 1.1
 
Ajp notes-chapter-02
Ajp notes-chapter-02Ajp notes-chapter-02
Ajp notes-chapter-02
 
Advance Java Programming (CM5I) 2.Swing
Advance Java Programming (CM5I) 2.SwingAdvance Java Programming (CM5I) 2.Swing
Advance Java Programming (CM5I) 2.Swing
 
Java AWT and Java FX
Java AWT and Java FXJava AWT and Java FX
Java AWT and Java FX
 
SWING.pptx
SWING.pptxSWING.pptx
SWING.pptx
 
java presentation on Swings chapter java presentation on Swings
java presentation on Swings chapter java presentation on Swingsjava presentation on Swings chapter java presentation on Swings
java presentation on Swings chapter java presentation on Swings
 
Computer Programming NC III - Java Swing.pptx
Computer Programming NC III - Java Swing.pptxComputer Programming NC III - Java Swing.pptx
Computer Programming NC III - Java Swing.pptx
 
14a-gui.ppt
14a-gui.ppt14a-gui.ppt
14a-gui.ppt
 
SwingApplet.pptx
SwingApplet.pptxSwingApplet.pptx
SwingApplet.pptx
 
Chapter iv(modern gui)
Chapter iv(modern gui)Chapter iv(modern gui)
Chapter iv(modern gui)
 
GUI Programming In Java
GUI Programming In JavaGUI Programming In Java
GUI Programming In Java
 
DSJ_Unit III.pdf
DSJ_Unit III.pdfDSJ_Unit III.pdf
DSJ_Unit III.pdf
 
L11cs2110sp13
L11cs2110sp13L11cs2110sp13
L11cs2110sp13
 

More from Narayana Swamy

AICTE GUIDLINES AICTE GUIDLINESAICTE GUIDLINES
AICTE GUIDLINES  AICTE GUIDLINESAICTE GUIDLINESAICTE GUIDLINES  AICTE GUIDLINESAICTE GUIDLINES
AICTE GUIDLINES AICTE GUIDLINESAICTE GUIDLINES
Narayana Swamy
 
Files io
Files ioFiles io
Files io
Narayana Swamy
 
Exceptions
ExceptionsExceptions
Exceptions
Narayana Swamy
 
Z blue interfaces and packages (37129912)
Z blue   interfaces and  packages (37129912)Z blue   interfaces and  packages (37129912)
Z blue interfaces and packages (37129912)
Narayana Swamy
 
Z blue exception
Z blue   exceptionZ blue   exception
Z blue exception
Narayana Swamy
 
Oop inheritance chapter 3
Oop inheritance chapter 3Oop inheritance chapter 3
Oop inheritance chapter 3
Narayana Swamy
 
Exceptions
ExceptionsExceptions
Exceptions
Narayana Swamy
 

More from Narayana Swamy (7)

AICTE GUIDLINES AICTE GUIDLINESAICTE GUIDLINES
AICTE GUIDLINES  AICTE GUIDLINESAICTE GUIDLINESAICTE GUIDLINES  AICTE GUIDLINESAICTE GUIDLINES
AICTE GUIDLINES AICTE GUIDLINESAICTE GUIDLINES
 
Files io
Files ioFiles io
Files io
 
Exceptions
ExceptionsExceptions
Exceptions
 
Z blue interfaces and packages (37129912)
Z blue   interfaces and  packages (37129912)Z blue   interfaces and  packages (37129912)
Z blue interfaces and packages (37129912)
 
Z blue exception
Z blue   exceptionZ blue   exception
Z blue exception
 
Oop inheritance chapter 3
Oop inheritance chapter 3Oop inheritance chapter 3
Oop inheritance chapter 3
 
Exceptions
ExceptionsExceptions
Exceptions
 

Recently uploaded

morris_worm_intro_and_source_code_analysis_.pdf
morris_worm_intro_and_source_code_analysis_.pdfmorris_worm_intro_and_source_code_analysis_.pdf
morris_worm_intro_and_source_code_analysis_.pdf
ycwu0509
 
Applications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdfApplications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdf
Atif Razi
 
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
IJECEIAES
 
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
upoux
 
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
ydzowc
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
IJECEIAES
 
Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...
bijceesjournal
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
IJECEIAES
 
Object Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOADObject Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOAD
PreethaV16
 
CEC 352 - SATELLITE COMMUNICATION UNIT 1
CEC 352 - SATELLITE COMMUNICATION UNIT 1CEC 352 - SATELLITE COMMUNICATION UNIT 1
CEC 352 - SATELLITE COMMUNICATION UNIT 1
PKavitha10
 
Generative AI Use cases applications solutions and implementation.pdf
Generative AI Use cases applications solutions and implementation.pdfGenerative AI Use cases applications solutions and implementation.pdf
Generative AI Use cases applications solutions and implementation.pdf
mahaffeycheryld
 
VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...
VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...
VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...
PIMR BHOPAL
 
Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...
Prakhyath Rai
 
Digital Twins Computer Networking Paper Presentation.pptx
Digital Twins Computer Networking Paper Presentation.pptxDigital Twins Computer Networking Paper Presentation.pptx
Digital Twins Computer Networking Paper Presentation.pptx
aryanpankaj78
 
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
Yasser Mahgoub
 
Engineering Standards Wiring methods.pdf
Engineering Standards Wiring methods.pdfEngineering Standards Wiring methods.pdf
Engineering Standards Wiring methods.pdf
edwin408357
 
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by AnantLLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
Anant Corporation
 
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
PriyankaKilaniya
 
TIME TABLE MANAGEMENT SYSTEM testing.pptx
TIME TABLE MANAGEMENT SYSTEM testing.pptxTIME TABLE MANAGEMENT SYSTEM testing.pptx
TIME TABLE MANAGEMENT SYSTEM testing.pptx
CVCSOfficial
 
Mechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdfMechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdf
21UME003TUSHARDEB
 

Recently uploaded (20)

morris_worm_intro_and_source_code_analysis_.pdf
morris_worm_intro_and_source_code_analysis_.pdfmorris_worm_intro_and_source_code_analysis_.pdf
morris_worm_intro_and_source_code_analysis_.pdf
 
Applications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdfApplications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdf
 
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
 
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
 
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
 
Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
 
Object Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOADObject Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOAD
 
CEC 352 - SATELLITE COMMUNICATION UNIT 1
CEC 352 - SATELLITE COMMUNICATION UNIT 1CEC 352 - SATELLITE COMMUNICATION UNIT 1
CEC 352 - SATELLITE COMMUNICATION UNIT 1
 
Generative AI Use cases applications solutions and implementation.pdf
Generative AI Use cases applications solutions and implementation.pdfGenerative AI Use cases applications solutions and implementation.pdf
Generative AI Use cases applications solutions and implementation.pdf
 
VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...
VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...
VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...
 
Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...
 
Digital Twins Computer Networking Paper Presentation.pptx
Digital Twins Computer Networking Paper Presentation.pptxDigital Twins Computer Networking Paper Presentation.pptx
Digital Twins Computer Networking Paper Presentation.pptx
 
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
 
Engineering Standards Wiring methods.pdf
Engineering Standards Wiring methods.pdfEngineering Standards Wiring methods.pdf
Engineering Standards Wiring methods.pdf
 
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by AnantLLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
 
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
 
TIME TABLE MANAGEMENT SYSTEM testing.pptx
TIME TABLE MANAGEMENT SYSTEM testing.pptxTIME TABLE MANAGEMENT SYSTEM testing.pptx
TIME TABLE MANAGEMENT SYSTEM testing.pptx
 
Mechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdfMechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdf
 

Z blue introduction to gui (39023299)

  • 2. Introduction to GUI A graphical user interface (GUI) presents a user-friendly mechanism for interacting with an application. A GUI (pronounced "goo-ee") gives an application a distinctive "look" and "feel." It provides different applications with consistent, intuitive user interface components allows users to be somewhat familiar with an application, so that they can learn it more quickly and use it more productively.
  • 3. Overview Of Swing Components Most Swing components are pure Java components they are written, manipulated and displayed completely in Java. They are part of the Java Foundation Classes (JFC)Java's libraries for cross-platform GUI development. There are actually two sets of GUI components in Java. Before Swing was introduced in J2SE 1.2, Java GUIs were built with components from the Abstract Window Toolkit (AWT) in package java.awt.
  • 4. When a Java application with an AWT GUI executes on different Java platforms, the application's GUI components display differently on each platform. i.e. GUI app in Windows differs from Mac. Together, the appearance and the way in which the user interacts with the application are known as that application's look-and-feel. Swing GUI components allow you to specify a uniform look- and-feel for your application across all platforms or to use each platform's custom look-and-feel. Overview Of Swing Components
  • 5. Overview Of Swing Components An application can even change the look-and-feel during execution to enable users to choose their own preferred look-and-feel. Most Swing components are not tied to actual GUI components supported by the underlying platform on which an application executes. Such GUI components are known as lightweight components. For this reason, Swing GUI components are generally preferred.
  • 6. Overview Of Swing Components In Swing, classes that represent GUI components have names beginning with the letter J. Some examples are JButton, JLabel, JFrame, JTextField, JCheckBox, JComboBox, JRadioButton, JMenu, Jslider and so on. Altogether there are more than 250 new classes and 75 interfaces in Swing, the figure shown below depicts the Java Swing class hierarchy.
  • 7. Overview Of Swing Components
  • 8. Overview Of Swing Components
  • 9. Overview Of Swing Components
  • 10. Overview Of Swing Components 1. JPanel is swing's version of the AWT class panel and uses the same default layout, Flowlayout and it is descended directly from JComponent. 2. JFrame is swing's version of frame and is descended directly from that class. 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 instead. 3. FlowLayout are used to arrange swing components from left to right until there's no more space available. Then it begins a new row below it and moves from left to right again. Each component in a FlowLayout gets as much space as it needs and no more.
  • 11. Overview Of Swing Components 4. GridLayout is a layout manager that lays out a container's components in a rectangular grid in row by column manner as per provided by the user. The container is divided into equal-sized rectangles, and one component is placed in each rectangle. 5. JLabel descended from JComponent, is used to create text labels that can not be edited live but are capable of modified in order. 6. AbstractButton is an abstract class which extends class JComponent and provides a foundation for a family of button classes, including JButton. JButton is a component the user clicks to trigger a specific action.
  • 12. Overview Of Swing Components 7. JTextField allows editing of a single line of text. New features include the ability to justify the text left, right, or center, and to set the text's font. 8. JPasswordField (a direct subclass of JTextField ) you can suppress the display of input. Each character entered can be replaced by echo character. This allows confidential input for passwords, for example. By default, the asterisk, *. 9. JTextArea allows editing of multiple lines of text. JTextArea can be used in conjunction with class JScrollPane to achieve scrolling.
  • 13. Overview Of Swing Components The underlying JScrollPane can be forced to always or never have either the vertical or horizontal scrollbar; 10.JRadioButton is similar to JCheckbox, except for the default icon for each class. A set of radio buttons can be associated as a group in which only one button at a time can be selected. 11.JCheckBox is not a member of a checkbox group. A checkbox can be selected and deselected, AND it also displays its current state.
  • 14. Overview Of Swing Components 12. JComboBox is like a drop down box. You can click a drop-down arrow and select an option from a list. For example, when the component has focus, pressing a key that corresponds to the first character in some entry’s name selects that entry. A vertical scrollbar is used for longer lists. 13. JMenubar can contain several JMenu’s. Each of the JMenu’s can contain a series of JMenuItem ’s that it can let you select one. Swing provides support for pull-down and popup menus.
  • 15. Overview Of Swing Components Java Swing Example: Below is a java swing code for the traditional Hello World program. import javax.swing.JFrame; import javax.swing.JLabel; OUTPUT: public class HelloWorldFrame extends JFrame { HelloWorldFrame(){ JLabel jlbHelloWorld = new JLabel("Hello World"); add(jlbHelloWorld); this.setSize(200, 100); setVisible(true); public static void main(String args[]) { new HelloWorldFrame(); } } }
  • 16. JFrame JFrame is a Window with border, title and buttons. When JFrame is set visible, an event dispatching 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 Constructors JFrame() : Constructs a new frame that is initially invisible. JFrame(String title) : Creates a new, initially invisible Frame with the specified title.
  • 17. JFrame Contnd… Steps to create a JFrame windows are as follows: 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.
  • 18. JFlowLayout It is an AWT component used to arrange swing components from left to right until there’s no more space available. import java.awt.FlowLayout.*; import javax.swing.*; public class FlowLayoutDemo extends JFrame{ FlowLayoutDemo() { super("FlowLayout Source Demo"); JPanel panel = new JPanel(); add(panel); panel.setLayout(new FlowLayout()); panel.add(new JLabel("JLabel 1")); panel.add(new JButton("JButton 2")); panel.add(new JCheckBox("JCheckBox 3")); panel.add(new JTextField("Long-Named JTextField 4")); panel.add(new JButton("JButton 5")); setVisible(true); setSize(300,350); } public static void main(String[] args) { FlowLayoutDemo flowLayout = new FlowLayoutDemo(); flowLayout.setDefaultCloseOperation(JFrame.EXIT_ON_ CLOSE); } }
  • 20. GridLayout It is a layout manager that lays out a container’s components in a rectangular grid. The container is divided into equal-sized rectangles, and one component is placed in each rectangle. GridLayout Constructors GridLayout() : Creates a grid layout with a default of one column per component, in a single row. GridLayout(int rows, int cols) : Creates a grid layout with the specified number of rows and columns.
  • 21. JFrame Contnd… Steps to create a JFrame windows with predefined layout are as follows: 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 up the content pane and components in a layout FlowLayout, GridLayout etc. Step 5: Set the default close operation. Step 6: Make the JFrame visible.
  • 22. JLabel JLabel, descended from JComponent, is used to create text labels. A JLabel object provides text instructions or information on a GUI; display a single line of read-only text, an image or both text and image. We use a Swing JLabel when we need a user interface component that displays a message or an image. JLabel Constructor JLabel() : Creates a JLabel instance with no image and with an empty string for the title. JLabel(Icon image) :Creates a JLabel instance with the specified image. JLabel(String text) : Creates a JLabel instance with the specified text.
  • 23. Introduction to Event Handling Normally, a user interacts with an application’s GUI to indicate the tasks that the application should perform. For example, when you write an e-mail in an e-mail application, clicking the send button tells the application to send the e-mail to the specified e-mail addresses. GUIs are event driven. When the user interacts with a GUI component, the interaction known as an event drives the program to perform a task. Some common events (user interactions) that might cause an application to perform a task include clicking a button, typing in a text field, selecting an item from a menu, closing a window and moving the mouse.  The code that performs a task in response to an event is called an event handler and the overall process of responding to events is known as event handling.
  • 24. More on Event Handling Both AWT and Swing applications uses the AWT event-handling classes (in package java.awt.event). Swing added a few new event handling classes (in package javax.swing.event), but they are not frequently used. AWT GUI Components (such as Button, TextField, ComboBox, CheckBox and Window) can trigger an AWTEvent upon user’s activation.
  • 25. More on Event Handling ActionEvent & ActionListener An ActionEvent is fired, when an action has been performed by the user. For examples, when the user clicks a button, chooses a menu item, presses enter key in a text field. The associated ActionListener interface declares only one abstract method, as follows: ActionListner Method Definition: public interface ActionListener extends java.util.eventlistener { public void actionPerformed(ActionEvent evt); // called-back when an action //has been performed }
  • 26. More on Event Handling Example source code: The example illustrates a source code fragment which clears a text in text field when the clear button pressed. JPanel panel = new JPanel(); JTextfield1 jTextField1 = new JTextField(); JButton jBtnClear new JButton(“CLEAR”) panel.setLayout(new GridLayout(1,2)); add(panel, BorderLayout.CENTER); panel.add(jBtnClear); panel.add(jTextField1); jBtnClear.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { jTextfield1.setText(""); }});
  • 27. JTextField It allows editing/displaying of a single line of text. When the user types data into them and presses the Enter key, an action event occurs. If the program registers an event listener, the listener processes the event and can use the data in the text field at the time of the event in the program. JTextField is an input area where the user can type in characters.
  • 28. JTextField Contnd… If you want to let the user enter multiple lines of text, you cannot use JTextfield’s unless you create several of them. The solution is to use JTextArea which enables the user to enter multiple lines of text. JTextField Constructor JTextField() : Constructs a new TextField. JTextField(String text): Constructs a new TextField initialized with the specified text. All search engine applications are of typical examples of JTextFields.
  • 29. JButton JButton is a component that the user clicks to trigger a specific action. A Java application can use several types of buttons, including command buttons, checkboxes, toggle buttons and radio buttons. JButton Constructors JButton() : Creates a button with no set text or icon. JButton(Action a) : Creates a button where properties are taken from the Action supplied. JButton(String text) : Creates a button with text.
  • 30. JComboBox JComboBox is like a drop down box that you can click a drop-down arrow and select an option from a list. It generates ItemEvent. For example, when the component has focus, pressing a key that corresponds to the first character in some entry’s name selects that entry. A vertical scrollbar is used for longer lists. JComboBox Constructors JComboBox() : Creates a JComboBox with a default data model. JComboBox(Object[] items) :Creates a JComboBox that contains the elements in the specified array. JComboBox(Vector items) : Creates a JComboBox that contains the elements in the specified Vector.
  • 31. JMenus Swing provides support for pull-down and popup menus. A JMenubar can contain several JMenus. And each of the JMenus can contain a series of JMenuItems that you can select. How Menus are created? 1. First, a JMenubar is created 2. Then, we attach all of the menus to this JMenubar. 3. Then we add JMenuItems to the JMenus. 4. The JMenubar is then added to the frame. By default, each JMenuItem added to a JMenu is enabled, that is, it can be selected.
  • 32. JMenus import java.awt.*; import java.awt.event.*; import javax.swing.JMenu; import javax.swing.JMenuItem; import javax.swing.JMenuBar; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.JFrame; public class JMenuDemo extends JFrame implements ActionListener{ JTextArea jtAreaOutput; JMenuBar mainMenuBar; JMenu menu1, menu2, submenu; JMenuItem New,subMenuItem; JMenuDemo() { super("Microsoft Word"); mainMenuBar = new JMenuBar(); menu1 = new JMenu("File"); menu1.setMnemonic(KeyEvent.VK_F); mainMenuBar.add(menu1); New = new JMenuItem("New"); New.addActionListener(this); menu1.add(New); menu1.addSeparator();
  • 33. JMenus // Sub Menu follows a seperator submenu = new JMenu("Send To:"); submenu.setMnemonic(KeyEvent.VK_D); subMenuItem = new JMenuItem("My Document"); subMenuItem.addActionListener(this); submenu.add(subMenuItem); menu1.add(submenu); // Build second menu in the menu bar. menu2 = new JMenu("Edit"); menu2.setMnemonic(KeyEvent.VK_E); mainMenuBar.add(menu2); // return mainMenuBar; JPanel jplContentPane = new JPanel(); jplContentPane.setLayout(new FlowLayout()); jtAreaOutput = new JTextArea(5, 30); jplContentPane.add(jtAreaOutput); add(jplContentPane); setSize(400, 300); setVisible(true); }
  • 34. JMenus public void actionPerformed(ActionEvent e) { JMenuItem source = (JMenuItem) (e.getSource()); String s = "Menu Item source: " + source.getText(); jtAreaOutput.setText(s); } public static void main(String[] args) { JMenuDemo frame= new JMenuDemo(); frame.setJMenuBar(frame.mainMenuBar); frame.setDefaultCloseOperation(JFrame.EXIT_ON_ CLOSE); } } The End