You are on page 1of 16

*

Bayu Priyambadha, S.Kom

* A part of The JFC (Java Foundations Classes) -> Set of GUI


Components * Swing Java consists of * Look and feel * Accessibility * Java 2D * Drag and Drop, etc * AWT(Abstract Window Toolkit) to Swing

* AWT:

* Swing:

* import java.awt.*

Abstract Windowing Toolkit

* import javax.swing.*

new with Java2

J2sdk/demo/jfc/SwingSet2

Many predefined GUI components

1. 2.

Create it

* * *

Instantiate object: b = new JButton(press me); Properties: Methods: b.text = press me; b.setText(press me); [avoided in java]

Configure it

3.
4.

Add it

* *

panel.add(b); Events: Listeners

Listen to it
JButton

From Wikipedia

GUI

Internal structure

JFrame
JPanel containers

JFrame

JPanel
JButton

JButton
JLabel

JLabel

1. 2. 3. 4. 5.

Create it

Configure it
Add children (if container) Add to parent (if not JFrame)

order important

Listen to it

* Create:
* Frame * Panel * Components * Listeners

Listener

JLabel

JButton

* Add:

(bottom up)
JPanel

* listeners into components * components into panel * panel into frame

JFrame

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

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

null

FlowLayout

GridLayout

none, programmer sets x,y,w,h

Left to right, Top to bottom

BorderLayout n

CardLayout

GridBagLayout

c s

One at a time

JButton

JButton

JButton

JTextArea

JButton JFrame n JPanel: BorderLayout c

JButton

JPanel: FlowLayout

JTextArea

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); p.add(b); f.setContentPane(p);
press me

// x,y layout

JFrame f = new JFrame(title); JPanel p = new JPanel( );

FlowLayout L = new FlowLayout( );


JButton b1 = new JButton(press me); JButton b2 = new JButton(then me); p.setLayout(L); p.add(b1);

p.add(b2);
f.setContentPane(p);

press me

then me

Set layout mgr before adding components

You might also like