-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOctalObserver.java
More file actions
75 lines (65 loc) · 2.16 KB
/
OctalObserver.java
File metadata and controls
75 lines (65 loc) · 2.16 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
package simple_implimentation;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class OctalObserver extends JFrame implements Observer , ActionListener{
private Subject subject;
private JLabel label = new JLabel("State Octal Value : Undefined");
private JPanel labelPanel = new JPanel();
private JPanel buttonPanel = new JPanel();
private JPanel emptyPanel = new JPanel();
private JButton subscribe = new JButton("Subscribe-Me");
private JButton unsubscribe = new JButton("Unsubscribe-Me");
public OctalObserver(Subject subject){
super("Welcome to the Octal Observer!");
this.subject = subject;
this.setLayout(new FlowLayout());
labelPanel.setLayout(new FlowLayout());
buttonPanel.setLayout(new FlowLayout());
labelPanel.add(label);
buttonPanel.add(subscribe);
buttonPanel.add(unsubscribe);
emptyPanel.setPreferredSize(new Dimension(300,150));
subscribe.addActionListener(this);
unsubscribe.addActionListener(this);
this.add(labelPanel);
this.add(emptyPanel);
this.add(buttonPanel);
this.subscribe.setEnabled(true);
this.unsubscribe.setEnabled(false);
this.setSize(300, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
@Override
public void update() {
this.getLabel().setText( "State Octal Value : "
+ Integer.toOctalString( subject.getState() ) );
}
public JLabel getLabel() {
return label;
}
public void setLabel(JLabel label) {
this.label = label;
}
@Override
public void actionPerformed(ActionEvent event) {
if (event.getSource() == subscribe) {
this.subject.subscribe(this);
this.subscribe.setEnabled(false);
this.unsubscribe.setEnabled(true);
}
else if (event.getSource() == unsubscribe) {
this.subject.unsubscribe(this);
this.getLabel().setText( "State Octal Value : Undefined");
this.subscribe.setEnabled(true);
this.unsubscribe.setEnabled(false);
}
}
}