-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCarWindow.java
More file actions
executable file
·55 lines (43 loc) · 1.31 KB
/
CarWindow.java
File metadata and controls
executable file
·55 lines (43 loc) · 1.31 KB
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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
/**
* CarWindow.java — Swing shell around CarWorld.
*
* THREAD-SAFETY SUMMARY:
* - ALL UI CONSTRUCTION/STANDARD EVENT HANDLERS RUN ON THE EDT.
* - BUTTON LISTENERS CALL CarWorld.addCar, WHICH USES invokeLater TO STAY ON THE EDT
* WHILE THE CAR THREADS THEMSELVES RUN IN PARALLEL.
*/
public class CarWindow extends JFrame {
CarWorld display;
JButton addLeft;
JButton addRight;
public CarWindow() {
Container c = getContentPane();
c.setLayout(new BorderLayout());
display = new CarWorld();
c.add("Center",display);
addLeft = new JButton("Add Left");
addRight = new JButton("Add Right");
addLeft.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
display.addCar(Car.REDCAR);
}
}
);
addRight.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
display.addCar(Car.BLUECAR);
}
}
);
JPanel p1 = new JPanel();
p1.setLayout(new FlowLayout());
p1.add(addLeft);
p1.add(addRight);
c.add("South",p1);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}