lab12.pdf

(148 KB) Pobierz
Programowanie obiektowe /Java/
Laboratorium nr 12
1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Aplikacja graficzna
package
pl.kielce.tu.lab12;
import
javax.swing.JButton;
import
javax.swing.JFrame;
import
javax.swing.JWindow;
public class
TestSwing {
public static void
test() {
JFrame f =
new
JFrame("JFrame");
f.add(new JButton("JFrameButton"));
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setBounds(0, 0, 320, 240);
f.setVisible(true);
JWindow w =
new
JWindow();
w.add(new JButton("JWindowButton"));
w.setBounds(0, 240, 320, 240);
w.setVisible(true);
}
public static void
main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void
run() {
test();
}
});
}
}
Przykład 1: src/pl/kielce/tu/lab12/TestSwing.java
{link}
Jaka jest różnica pomiędzy
JF rame
a
JW indow?
2
Aplety
„(...) apletach
1
, czyli programach, rozprowadzanych za pośrednictwem Internetu, a przeznaczonych do
uruchamiania w przeglądarkach WWW (ze względów bezpieczeństwa środowisko uruchomieniowe apletu,
tzw. piaskownica - ang. sandbox - jest mocno ograniczone)”
Podstawowe metody klasy
JApplet:
-
init()
– metoda wywoływana w celu poinformowania apletu, że został załadowany,
-
start()
– metoda wywoływana w celu poinformowania apletu, że powinien rozpocząć działanie, wywo-
ływana po metodzie
init()
oraz za każdym razem, gdy strona zwierająca aplet jest odwiedzana,
-
stop()
– metoda wywoływana w celu poinformowania apletu, że powinien zatrzymać działanie, wywo-
ływana przed metodą
destroy()
oraz za każdym razem, gdy strona zwierająca aplet jest zastępowana
przez inną,
-
destroy()
– metoda wywoływana w celu poinformowania apletu, że powinien zwolnić wszystkie zaalo-
kowane zasoby, wywoływana przed zniszczeniem apletu.
1
Thinking
in Java, Bruce Eckel, Wydanie IV, Helion, 2006
1
3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
Obsługa zdarzeń
Uwaga!!! Poniższe programy nie są kompletne. Brakuje kodu odpowiedzialnego za obsługę zdarzeń.
package
pl.kielce.tu.lab12;
import
import
import
import
import
import
import
import
import
import
import
java.awt.event.FocusEvent;
java.awt.event.FocusListener;
java.awt.event.KeyEvent;
java.awt.event.KeyListener;
java.awt.event.MouseEvent;
java.awt.event.MouseListener;
java.awt.event.MouseMotionListener;
java.awt.event.MouseWheelEvent;
java.awt.event.MouseWheelListener;
java.awt.event.WindowEvent;
java.awt.event.WindowListener;
import
javax.swing.JFrame;
public class
TestListeners
extends
JFrame {
private static final long
serialVersionUID = 1L;
TestListeners() {
// zdarzenia związane z aktywnością okna
this.addFocusListener(new
FocusListener() {
public void
focusGained(FocusEvent event) {
}
public void
focusLost(FocusEvent event) {
}
});
// zdarzenia związane z oknem
this.addWindowListener(new
WindowListener() {
public void
windowActivated(WindowEvent event) {
}
public void
windowClosed(WindowEvent event) {
}
public void
windowClosing(WindowEvent event) {
}
public void
windowDeactivated(WindowEvent event) {
}
public void
windowDeiconified(WindowEvent event) {
}
public void
windowIconified(WindowEvent event) {
}
public void
windowOpened(WindowEvent event) {
}
});
// zdarzenia związane z myszką
this.addMouseListener(new
MouseListener() {
public void
mouseClicked(MouseEvent event) {
}
public void
mouseEntered(MouseEvent event) {
}
public void
mouseExited(MouseEvent event) {
}
public void
mousePressed(MouseEvent event) {
}
public void
mouseReleased(MouseEvent event) {
}
});
this.addMouseMotionListener(new
MouseMotionListener() {
2
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
public void
mouseDragged(MouseEvent event) {
}
public void
mouseMoved(MouseEvent event) {
}
});
this.addMouseWheelListener(new
MouseWheelListener() {
public void
mouseWheelMoved(MouseWheelEvent event) {
}
});
// zdarzenia związane z klawiaturą
this.addKeyListener(new
KeyListener() {
public void
keyPressed(KeyEvent event) {
}
public void
keyReleased(KeyEvent event) {
}
public void
keyTyped(KeyEvent event) {
}
});
}
public static void
test() {
TestListeners f =
new
TestListeners();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setBounds(0, 0, 800, 600);
f.setVisible(true);
}
public static void
main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void
run() {
test();
}
});
}
}
Przykład 2: src/pl/kielce/tu/lab12/TestListeners.java
{link}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package
pl.kielce.tu.lab12;
import
import
import
import
import
java.awt.event.FocusAdapter;
java.awt.event.KeyAdapter;
java.awt.event.MouseAdapter;
java.awt.event.MouseMotionAdapter;
java.awt.event.WindowAdapter;
import
javax.swing.JFrame;
public class
TestAdapters
extends
JFrame {
private static final long
serialVersionUID = 1L;
TestAdapters() {
// zdarzenia związane z aktywnością okna
this.addFocusListener(new
FocusAdapter() {
});
// zdarzenia związane z oknem
this.addWindowListener(new
WindowAdapter() {
});
// zdarzenia związane z myszką
this.addMouseListener(new
MouseAdapter() {
});
3
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
this.addMouseMotionListener(new
MouseMotionAdapter() {
});
// zdarzenia związane z klawiaturą
this.addKeyListener(new
KeyAdapter() {
});
}
public static void
test() {
TestAdapters f =
new
TestAdapters();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setBounds(0, 0, 800, 600);
f.setVisible(true);
}
public static void
main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void
run() {
test();
}
});
}
}
Przykład 3: src/pl/kielce/tu/lab12/TestAdapters.java
{link}
Jaka jest różnica pomiędzy
Listener’em
a
Adapter’em
np.
KeyListener’em
a
KeyAdapter’em?
4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
Układy graficzne
package
pl.kielce.tu.lab12;
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
java.awt.BorderLayout;
java.awt.CardLayout;
java.awt.Color;
java.awt.FlowLayout;
java.awt.GridBagConstraints;
java.awt.GridBagLayout;
java.awt.GridLayout;
java.awt.event.ActionEvent;
java.awt.event.ActionListener;
java.util.Random;
javax.swing.BoxLayout;
javax.swing.JButton;
javax.swing.JFrame;
javax.swing.JPanel;
javax.swing.JScrollPane;
javax.swing.JTabbedPane;
public class
TestLayouts {
private static
JFrame createFrame(int x,
int
y, String text) {
JFrame frame =
new
JFrame(text);
frame.setBounds(x, y, 640, 480);
return
frame;
}
private static void
showFrame(JFrame frame) {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
4
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
private static void
test() {
JFrame frame =
null;
int
x = 0;
int
y = 0;
frame = createFrame(x, x,
"BorderLayout");
testBorderLayout(frame);
showFrame(frame);
x += 30;
y += 30;
frame = createFrame(x, x,
"FlowLayout");
testFlowLayout(frame);
showFrame(frame);
x += 30;
y += 30;
frame = createFrame(x, x,
"GridLayout");
testGridLayout(frame);
showFrame(frame);
x += 30;
y += 30;
frame = createFrame(x, x,
"BoxLayout");
testBoxLayout(frame);
showFrame(frame);
x += 30;
y += 30;
frame = createFrame(x, x,
"GridBagLayout");
testGridBagLayout(frame);
showFrame(frame);
x += 30;
y += 30;
frame = createFrame(x, x,
"CardLayout");
testCardLayout(frame);
showFrame(frame);
x += 30;
y += 30;
frame = createFrame(x, x,
"NoLayout");
testNoLayout(frame);
showFrame(frame);
x += 30;
y += 30;
frame = createFrame(x, x,
"JScrollPane");
testJScrollPane(frame);
showFrame(frame);
x += 30;
y += 30;
frame = createFrame(x, x,
"JTabbedPane");
testJTabbedPane(frame);
showFrame(frame);
x += 30;
y += 30;
}
public static void
main(String[] args) {
5
Zgłoś jeśli naruszono regulamin