Java as a Second Language - OoCities

eyelashesnectarineΛογισμικό & κατασκευή λογ/κού

3 Νοε 2013 (πριν από 3 χρόνια και 11 μήνες)

79 εμφανίσεις

User Interfaces II

GUI


Awt, Swing, Web

(c) IDMS/SQL News


http://www.geocities.com/idmssql


GUI
-

Introduction
(c)http://www.geocities.com/idmssql

2

User Interfaces
-

recap


In Java one can create at least 3 types of
programs

Windows

Applet

Servlet/JSP

Java Class

GUI
-

Introduction
(c)http://www.geocities.com/idmssql

3

Graphical User Interface


Started with AWT


Abstract Windowing Toolkit


Later came Swing


Today AWT components are included in Swing


AWT components are called
heavyweight


Swing components are called
lightweight


Gui is based on events which are generated by
things like mouse, button, list selections, window
close operations, menu pulldowns, etc

GUI
-

Introduction
(c)http://www.geocities.com/idmssql

4

Top Level GUI

o
Swing Containers


Top Level

o
-

JFrame

o
-

JDialog

o
-
JApplet


All three support some common methods
like setMenuBar(), getContentPane()


‘J’ stands for components in Swing...
Without ‘J’ they are usually part of AWT

GUI
-

Introduction
(c)http://www.geocities.com/idmssql

5

GUI Jargons


One of the
negative(!)

things about new
technology is the overuse of common words
and known technical words in a totally
different sense!


So we hear terms like
component,
container, layout, listener, Frame, Panel,
Window, Dialog

... a never ending list


We have to live with this!!!

GUI
-

Introduction
(c)http://www.geocities.com/idmssql

6

GUI Objects


at lower level


GUI is based on 3 types of objects


Containers



-

JPanel, JScrollPane etc


Selectors


-

JButton, JTextField, JCheckBox etc


Info Display


JTextArea, JTable etc (editable)


JLabel, JProgressBar etc (non
-
editable)

GUI
-

Introduction
(c)http://www.geocities.com/idmssql

7

Sample Methods ...


Paint(), setFont(), setSize() ...


setLayout()


setBoarder(), setToolTipText(), setOpaque()
... the list is endless...

GUI
-

Introduction
(c)http://www.geocities.com/idmssql

8

import java.awt.*;

import javax.swing.*; // Packages used

import java.awt.event.*;

public class
GuiSample1

extends
JFrame

{


JButton myButton = new JButton("Press Here");


JTextArea myArea = new JTextArea(10,20);


public void setupGUI() {


setTitle("S212
-

Java Crash Course");


myButton.setEnabled(true);


Container c = getContentPane();


c.setLayout(new FlowLayout());


c.add(myButton);


c.add(myArea);


setSize(400,300); }

See next Page

Example : GuiSample1

GUI
-

Introduction
(c)http://www.geocities.com/idmssql

9

Gui Code continued...

// we need a main to instantiate and execute this class

public static void main(String args[]) {


GuiSample1 dec18 = new GuiSample1();


dec18.setupGUI();


dec18.setVisible(true);


dec18.myArea.setEditable(false);


// Quit the application when the user 'closes' the Window


f.addWindowListener(new WindowAdapter() {


public void windowClosing(WindowEvent e) {


System.exit(0);}


});


}//main

} //

The above code does very little, but is complete.

GUI
-

Introduction
(c)http://www.geocities.com/idmssql

10

GUI .. note ...


The class extends JFrame


Container is created by getContentPane()


setLayout method is used


Buttons etc added by add(...) command


Main () gets controll first


Main instantiate a new object ‘dec18’


Method setupGUI does the housekeeping...


We utilize a standard ‘WindowsListener’
mechanism to close.

A complete example is given as GuiSample1.java

GUI
-

Introduction
(c)http://www.geocities.com/idmssql

11

Applet


Applets are Java classes run from WWW


Applets do not have main method


Html file will have tags like
<applet
code=“Hello5.class" width=”400"
height=”325"></applet>


More on applets later ...


Sample Java code follows



GUI
-

Introduction
(c)http://www.geocities.com/idmssql

12

Applet sample code

/*Applet program hello5.java*/

import java.applet.Applet;

import java.awt.*;

public class Hello5 extends Applet {


public void paint(Graphics screen) {


screen.drawString("Hello World",50,25);


screen.drawRoundRect(20, 40, 100, 100, 30,10);


}//end paint()


} //End hello5 class.