1
Języki Programowania
Graficzny interfejs użytkownika
2
GUI Principles
GUI
– Graphical User Interface
GUI’s are a good target for OO design
GUI’s main elements
Components
: GUI building blocks.
buttons, menus, sliders, etc.
Layout
: arranging components to
form a usable GUI.
using layout managers.
Events
: reacting to user input.
button presses, menu selections, etc.
There are some tools for convenient GUI-based design
JBuilder, Eclipse, NetBeans
2
3
Java GUI classes
AWT
(Abstract Window Toolkit) (java.awt.*)
pierwszy szkielet GUI dla Javy (Java 1.1)
pewne uzależnienia o implementacji języka
problemy implementacji na różnych platformach
Swing
(javax.swing.*)
kolejne GUI wprowadzone od wersji Java 1.2
zawiera cechy AWT i dodatkowe rozszerzenia
poprawione komponenty (bez uzależnienia się od implementacji)
mechanizmy do zmiany GUI w czasie działania programu
SWT
(Standard Widget Toolkit; from Eclipse)
4
AWT and Swing
AWT
was the original platform-dependent layer of
Java to build GUI’s.
Swing
(platform independent layer) was added later.
Swing is part of JFC (
Java Framework Collection
).
3
5
Event-driven programming
- sterowanie zdarzeniami
GUIs are
event-driven programs
: external events cause
methods to be executed
Components (komponenty)
: objects we create which
have a screen presence (e.g. controls, window panes),
and with which the user interacts (e.g. buttons).
Events (zdarzenia)
: when a user interacts with a
component, java creates an event object (which records
the source)
Listeners
(„słuchacze”)
: Objects we create which
respond to events. A listener has a handler method,
which Java invokes when the event occurs.
6
Container (window) with
component (button)
All this for one button click ...
user
user clicks
button
event object
is generated
ae:ActionEvent
event is
queued
for action
Listener objects
... do something
pętla obsługi
zdarzeń
: remove from
queue, invoke
handlers in
registered
listeners
4
7
... which requires:
In the program:
write a class that creates a window and a
JButton
, adds the
JButton to the window, makes the window visible
write listener classes (implements
ActionListener
) with handler
method (ActionPerformed)
create listener objects and register them with the JButton
At runtime:
user clicks the button
Java creates the ActionEvent object
Java adds object to a queue of events
Java repeatedly
takes object from queue
identifies their listeners
invokes each listener's handler with event as input
8
A first GUI ...
import
java.awt.*;
import
java.awt.event.*;
import
javax.swing.*;
public
class
SimplePanel
extends
JPanel {
private
JButton btn;
public
SimplePanel() {
super
();
btn =
new
JButton("Click me!");
add(btn);
ActionListener lstnr =
new
SimpleButtonListener();
btn.addActionListener(lstnr);
}
import AWT, AWT.event
and Swing packages
call to JPanel constructor
create a button
add it to the panel
create a listener
register it with the button
5
9
public
static
void
main(String[] args) {
JFrame frm =
new
JFrame("A simple GUI");
Container contentPane = frm.getContentPane();
JPanel pnl =
new
SimplePanel();
contentPane.add(pnl);
frm.setBounds(100, 300, 400, 200);
frm.setVisible(true);
}
}
SimplePanel continued
create a frame
get a handle on it
create a panel
add panel to frame
position it on screen
make it visible!
10
SimpleButtonListener
import
java.awt.event.*;
public class
SimpleButtonListener
implements
ActionListener {
private
int
count;
public
SimpleButtonListener() {
}
public
void
actionPerformed(ActionEvent ae) {
count++;
System.out.println(count);
}
}
tell Java it
is a Listener
state what happens
when the button is
pressed
6
11
That’s it ...
try do it in BlueJ yourself
you can also use JDK ...
no changes in source code needed
after 10 clicks
12
Components in the GUI
the GUI needs 2
JButto
ns
, a subclass of
JPanel
,
and
javax.swing.JTextField
allows display / entry / edit of a line of text
constructor specifies initial value (and number of columns)
has methods
setText(...), getText(...),
etc.
javax.swing.JLabel
allows display of text / images
user cannot edit them
constructor supplies initial text
has methods
setText(...), getText(...),
etc.
7
13
JFrame
JPanel (in the ContentPane
of the JFrame)
JButton
JTextField
JLabel
Components in the GUI
14
GUI Containers
some components are
containers
can have other components added to them
JPanel
is a container
– it has
JButton
s added to it
a container can have other containers added to it
JFrame
is a container
– it has a
JPanel
added to it (via its
content pane)
(in fact, in Swing, all components are containers)
8
15
1. The Window Listener
we mentioned that we could associate a listener with the
JFrame
the WindowListener interface specifies 7 methods, which
respond to changes in a window's state:
windowActivated(...)
: window is set to be active
windowClosed(...)
: window is closed by program
windowClosing(...)
: window closed by user
windowDeactivated(...)
: window is no longer active
windowDeiconified(...)
: window is restored to normal
windowIconified(...)
: window is minimised
windowOpened(...)
: window opened for first time
16
2. Using WindowAdapter
instead of laboriously typing default methods for the
WindowListener
, we can instead inherit from
WindowAdapter
:
implements
WindowListener
supplies 7 default methods
all we need to do is override the particular methods we
want
9
17
Good news for GUI developers
Even simple GUI seems to be very complicated to
develop in Java
Fortunetely, we have a number of IDEs (
Integrated
Development Environments
) to do it using a mouse and
„drag and pop” method
JBuilder (from
Borland
)
JCreator
Eclipse
NetBeans (
Sun Microsystems
)
18
NetBeans