-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindowExample.java
More file actions
141 lines (109 loc) · 4.07 KB
/
WindowExample.java
File metadata and controls
141 lines (109 loc) · 4.07 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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import java.awt.*; // import OS agnostic swing libraries
import java.awt.event.*;
import javax.swing.*;
import java.time.LocalDate;
import java.util.Calendar;
import java.text.SimpleDateFormat;
// Driver
public class WindowExample extends JFrame {
static Image img;
JButton buttonHappy, buttonSad, buttonOK, buttonNOT;
JPanel areaPanel, topPanel, leftPanel;
JLayeredPane bottomPanel;
public static void main(String[] args) {
WindowExample w = new WindowExample();
w.make();
}
public void make() {
this.setTitle("CS376: Example Window ");
this.setSize(800, 800);
areaPanel = new JPanel();
areaPanel.setLayout(new BorderLayout());
topPanel = new JPanel();
JLabel labelTop = new JLabel("You are a CS376 rockstar! Today:");
// 1. left panel creation here
leftPanel = new JPanel();
leftPanel.setLayout(new BoxLayout(leftPanel,0));
// 2. bottom pane creation here
bottomPanel = new JLayeredPane();
bottomPanel.setPreferredSize(new Dimension(600, 600));
// 3. happy and sad image buttons
buttonHappy = new JButton(new HappyAction());
buttonSad = new JButton(new SadAction());
// 4. happy and sad button change image
buttonOK = new JButton("OK");
buttonNOT = new JButton("Not OK");
// 5. happy sad images creation here
ImageIcon imgHappy = new ImageIcon("images/smiley.png");
JLabel imgLabelHappy = new JLabel(imgHappy);
JScrollPane scrollPaneHappy = new JScrollPane(imgLabelHappy);
scrollPaneHappy.setBounds(10, 50, imgHappy.getIconWidth() + 10, imgHappy.getIconHeight() + 10);
ImageIcon imgSad = new ImageIcon("images/frowny.png");
JLabel imgLabelSad = new JLabel(imgSad);
JScrollPane scrollPaneSad = new JScrollPane(imgLabelSad);
scrollPaneSad.setBounds(10, 50, imgSad.getIconWidth() + 10, imgSad.getIconHeight() + 10);
// 6. OK and NOT button listeners to change images
buttonNOT.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
scrollPaneSad.setVisible(false);
scrollPaneHappy.setVisible(true);
}
});
buttonOK.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
scrollPaneHappy.setVisible(false);
scrollPaneSad.setVisible(true);
}
});
// 7. labels with image signature
JLabel labelBottom = new JLabel("Image Signature ");
labelBottom.setBounds(10, 0, 300, 50);
// 8. add top panels to frame
topPanel.add(labelTop);
areaPanel.add(BorderLayout.PAGE_START, topPanel);
// 9. add buttons Happy and Sad to left panel
leftPanel.add(buttonHappy);
leftPanel.add(buttonSad);
// 10. add buttons OK and NOT to left panel
leftPanel.add(buttonOK);
leftPanel.add(buttonNOT);
// 11. add left panel to frame
areaPanel.add(BorderLayout.LINE_START,leftPanel);
// 12. add images to bottom panel
bottomPanel.add(labelBottom);
bottomPanel.add(scrollPaneHappy);
bottomPanel.add(scrollPaneSad);
// 13. add bottom panel to frame
areaPanel.add(BorderLayout.PAGE_END, bottomPanel);
this.add(areaPanel);
this.pack();
this.setVisible(true);
}
// 14. addTime method implementation
private String addTime(){
String date = new SimpleDateFormat("yyyy-MM-dd:hh:mm:ss zzz yyyy").format(Calendar.getInstance().getTime());
return date;
}
// 15. imgSignature method implementation
private String imgSignature(ImageIcon img){
int hash = System.identityHashCode(img);
return String.valueOf(hash);
}
}
// 16. Happy and Sad Action listeners implementation
class HappyAction extends AbstractAction{
HappyAction(){ super("Good"); }
@Override
public void actionPerformed(ActionEvent arg0) {
System.out.println("Good");
}
}
class SadAction extends AbstractAction{
SadAction(){ super("Sad"); }
@Override
public void actionPerformed(ActionEvent arg0) {
System.out.println("Noooot Good");
}
}