Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Java Swing-Java Programming-Lecture Slides, Slides of Computer Engineering and Programming

This lecture was delivered by Aniruddh Parmar at B. R. Ambedkar Bihar University for Data Transfer Programming course. It includes: Swing, Abstract, Windowing, Toolkit, Extends, Event, Listeners, Properties, Methods, Configure, Create, Layout

Typology: Slides

2011/2012

Uploaded on 07/11/2012

dhananad
dhananad 🇮🇳

4

(4)

39 documents

1 / 31

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Java Swing
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f

Partial preview of the text

Download Java Swing-Java Programming-Lecture Slides and more Slides Computer Engineering and Programming in PDF only on Docsity!

Java Swing

AWT to Swing

• AWT: Abstract Windowing Toolkit

  • import java.awt.*

• Swing: new with Java

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

GUI Component API

• Java: GUI component = class

• Properties

• Methods

• Events

JButton

Using a GUI Component

1. Create it

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

2. Configure it

  • Properties: b.text = “press me”; [avoided in java]
  • Methods: b.setText(“press me”);

3. Add it

  • panel.add(b);

4. Listen to it

  • Events: Listeners

JButton

Using a GUI Component 2

1. Create it

2. Configure it

3. Add children (if container)

4. Add to parent (if not JFrame)

5. Listen to it

order important

Build from bottom up

• Create:

  • Frame
  • Panel
  • Components
  • Listeners

• Add: (bottom up)

  • listeners into components
  • components into panel
  • panel into frame

JPanel

JButton

Listener

JFrame

JLabel

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); // add button to panel f.setContentPane(p); // add panel to frame

f.show(); } }

press me

Layout Managers

• Automatically control placement of components

in a panel

• Why?

Combinations

JButton JButton

JTextArea

Combinations

n

JPanel: BorderLayout

c

JFrame

JPanel: FlowLayout

JButton JButton

JTextArea

Code: FlowLayout

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);

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( )

import javax.swing.;*

class hello extends JApplet {

public void init(){ JButton b = new JButton(“press me”); getContentPane().add(b); }

}

JApplet

contentPane

JButton

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); } }*

JApplet

contentPane

JPanel

JFrame

JButton

or

Command line Browser

Applet Security

• No read/write on client machine

• Can‟t execute programs on client machine

• Communicate only with server

• “Java applet window” Warning