JP SS 10 Interfejs graficzny


Języki Programowania
Graficzny interfejs użytkownika
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
1
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)
3
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).
4
2
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.
5
All this for one button click ...
Container (window) with
component (button)
Listener objects
user
user clicks
button
pętla obsługi
zdarzeń: remove from ... do something
ae:ActionEvent
event is
queue, invoke
queued
handlers in
for action
registered
listeners
event object
is generated
6
3
... 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
7
A first GUI ...
import AWT, AWT.event
import java.awt.*;
and Swing packages
import java.awt.event.*;
import javax.swing.*;
public class SimplePanel extends JPanel {
private JButton btn;
call to JPanel constructor
public SimplePanel() {
create a button
super();
btn = new JButton("Click me!");
add it to the panel
add(btn);
ActionListener lstnr = new SimpleButtonListener();
btn.addActionListener(lstnr);
create a listener
}
register it with the button
8
4
SimplePanel continued
create a frame
get a handle on it
public static void main(String[] args) {
JFrame frm = new JFrame("A simple GUI");
Container contentPane = frm.getContentPane();
JPanel pnl = new SimplePanel();
contentPane.add(pnl);
create a panel
frm.setBounds(100, 300, 400, 200);
frm.setVisible(true);
}
}
position it on screen
add panel to frame
make it visible!
9
SimpleButtonListener
import java.awt.event.*;
tell Java it
is a Listener
public class SimpleButtonListener
implements ActionListener {
private int count;
public SimpleButtonListener() {
}
public void actionPerformed(ActionEvent ae) {
count++;
System.out.println(count);
state what happens
}
} when the button is
pressed
10
5
That s it ...
try do it in BlueJ yourself
you can also use JDK ...
no changes in source code needed
after 10 clicks
11
Components in the GUI
żðthe GUI needs 2 JButtons, 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.
12
6
Components in the GUI
JFrame
JLabel
JTextField
JPanel (in the ContentPane
JButton
of the JFrame)
13
GUI Containers
żðsome components are containers
żðcan have other components added to them
żðJPanel is a container  it has JButtons 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)
14
7
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
15
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
16
8
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)
17
NetBeans
18
9


Wyszukiwarka

Podobne podstrony:
JP SS 6 Klasy i obiekty
JP SS 2 algorytmy i podstawy programowania
JP SS 5 podstawy Java
Interfejs graficzny
JP SS 4 start Java
JP SS 5 podstawy Java (3)
JP SS 1 podstawy JP
JP SS 4 wprowadzenie Java
JP SS 1 podstawy JP
interface graficzny
JP SS 5 podstawy Java
JP SS 7 Klasy i obiekty
JP SS 6 BlueJ
JP SS 3 jezyk C
JP SS 3 jezyk C
JP SS 4 start Java
JP SS 8 Instrukcje, pętle
JP SS 7 Typy i operacje

więcej podobnych podstron