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

Java Swing

1. Java Swing provides GUI components that extend the Abstract Window Toolkit (AWT). It includes prebuilt components, standard dialog boxes, and improved look-and-feel capabilities. 2. Building a GUI with Swing components involves creating the components, configuring their properties, adding them to containers in a hierarchy, and adding event listeners. 3. Layout managers automatically position and resize components within containers. Common layouts include FlowLayout, GridLayout, BorderLayout, and CardLayout.

Uploaded by

Divya Gangatkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
137 views

Java Swing

1. Java Swing provides GUI components that extend the Abstract Window Toolkit (AWT). It includes prebuilt components, standard dialog boxes, and improved look-and-feel capabilities. 2. Building a GUI with Swing components involves creating the components, configuring their properties, adding them to containers in a hierarchy, and adding event listeners. 3. Layout managers automatically position and resize components within containers. Common layouts include FlowLayout, GridLayout, BorderLayout, and CardLayout.

Uploaded by

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

Java Swing

AWT to Swing
AWT: Abstract Windowing Toolkit
import java.awt.*

Swing: new with Java2


import javax.swing.* Extends AWT Tons o new improved components Standard dialog boxes, tooltips, Look-and-feel, skins Event listeners

API:
http://java.sun.com/j2se/1.3/docs/api/index.html

Swing Set Demo


J2sdk/demo/jfc/SwingSet2

Many predefined GUI components

GUI Component API


Java: GUI component = class Properties

Methods

JButton

Events

Using a GUI Component


1. Create it

Instantiate object: b = new JButton(press me);


Properties: b.text = press me; Methods: b.setText(press me); panel.add(b); [avoided in java]

2. Configure it

3. Add it

4. Listen to it
Events: Listeners

JButton

Anatomy of an Application GUI


GUI Internal structure

JFrame JPanel

JFrame
containers

JPanel
JButton

JButton
JLabel

JLabel

Using a GUI Component 2


1. 2. 3. 4. 5. Create it Configure it Add children (if container) Add to parent (if not JFrame) Listen to it

order important

Build from bottom up


Create:
Frame Panel Components Listeners
JLabel Listener

JButton

Add: (bottom up)


listeners into components components into panel panel into frame

JPanel

JFrame

Code
JFrame f = new JFrame(title); JPanel p = new JPanel( ); JButton b = new JButton(press me); p.add(b); f.setContentPane(p); f.show(); press me // add button to panel // add panel to frame

Application Code
import javax.swing.*;

class hello { public static void main(String[] args){ JFrame f = new JFrame(title); JPanel p = new JPanel(); JButton b = new JButton(press me);
p.add(b); f.setContentPane(p); f.show(); // add button to panel // add panel to frame

}
}

press me

Layout Managers
Automatically control placement of components in a panel Why?

Layout Manager Heuristics


null none, programmer sets x,y,w,h FlowLayout GridLayout

Left to right, Top to bottom

BorderLayout n w c s e

CardLayout

GridBagLayout

One at a time

JButton

Combinations
JButton JButton

JTextArea

Combinations
JButton JFrame n JPanel: FlowLayout JButton

JPanel: BorderLayout

c JTextArea

Code: null layout


JFrame f = new JFrame(title); JPanel p = new JPanel( ); JButton b = new JButton(press me); b.setBounds(new Rectangle(10,10, 100,50)); p.setLayout(null); // x,y layout p.add(b); f.setContentPane(p); press me

Code: FlowLayout
JFrame f = JPanel p = FlowLayout JButton b1 JButton b2 new JFrame(title); new JPanel( ); L = new FlowLayout( ); = new JButton(press me); = new JButton(then me);

p.setLayout(L); p.add(b1); p.add(b2); f.setContentPane(p); Set layout mgr before adding components

press me then me

Applets
JApplet is like a JFrame Already has a panel
Access panel with JApplet.getContentPane( )

JApplet

contentPane

import javax.swing.*; JButton class hello extends JApplet { public void init(){ JButton b = new JButton(press me); getContentPane().add(b); } }

Applet Methods
Called by browser: init( ) - initialization start( ) - resume processing (e.g. animations) stop( ) - pause destroy( ) - cleanup paint( ) - redraw stuff (expose event)

Application + Applet
import javax.swing.*; class helloApp { public static void main(String[] args){ // create Frame and put my mainPanel in it JFrame f = new JFrame(title); mainPanel p = new mainPanel(); f.setContentPane(p); f.show(); } } class helloApplet extends JApplet { public void init(){ // put my mainPanel in the Applet mainPanel p = new mainPanel(); getContentPane().add(p); } } // my main GUI is in here: class mainPanel extends JPanel { mainPanel(){ setLayout(new FlowLayout()); JButton b = new JButton(press me); add(b); } }

Command line

Browser

JFrame

or

JApplet

contentPane

JPanel
JButton

Applet Security
No read/write on client machine Cant execute programs on client machine Communicate only with server Java applet window Warning

You might also like