1+ import java .awt .Frame ;
2+ import java .awt .Label ;
3+ import java .awt .Color ;
4+ import java .awt .Button ;
5+ import java .awt .TextField ;
6+ import java .awt .TextArea ;
7+ import java .awt .Panel ;
8+ import java .awt .BorderLayout ;
9+ import java .awt .event .ActionListener ;
10+ import java .awt .event .ActionEvent ;
11+
12+ public class SafeFrame extends Frame implements ActionListener , Context {
13+ private TextField clock = new TextField (60 );
14+ private TextArea screen = new TextArea (10 , 60 );
15+ private Button btnUse = new Button ("金庫使用" );
16+ private Button btnAlarm = new Button ("非常ベル" );
17+ private Button btnPhone = new Button ("通常電話" );
18+ private Button btnExit = new Button ("終了" );
19+
20+ // private State currentState = DayState.getInstance();
21+ private State currentState = (State )NightState .getInstance ();
22+
23+ public SafeFrame (String title ) {
24+ super (title );
25+ this .setBackground (Color .lightGray );
26+ this .setLayout (new BorderLayout ());
27+
28+ this .add (this .clock , BorderLayout .NORTH );
29+ this .clock .setEditable (false );
30+
31+ this .add (this .screen , BorderLayout .CENTER );
32+ this .screen .setEditable (false );
33+
34+ Panel panel = new Panel ();
35+ panel .add (this .btnUse );
36+ panel .add (this .btnAlarm );
37+ panel .add (this .btnPhone );
38+ panel .add (this .btnExit );
39+ this .add (panel , BorderLayout .SOUTH );
40+
41+ this .pack ();
42+ // this.show();
43+ this .setVisible (true );
44+
45+ this .btnUse .addActionListener (this );
46+ this .btnAlarm .addActionListener (this );
47+ this .btnPhone .addActionListener (this );
48+ this .btnExit .addActionListener (this );
49+ }
50+
51+ public void actionPerformed (ActionEvent e ) {
52+ if (e .getSource () == this .btnUse ) {
53+ this .currentState .doUse (this );
54+ } else if (e .getSource () == this .btnAlarm ) {
55+ this .currentState .doAlarm (this );
56+ } else if (e .getSource () == this .btnPhone ) {
57+ this .currentState .doPhone (this );
58+ } else if (e .getSource () == this .btnExit ) {
59+ System .exit (0 );
60+ } else {
61+ System .out .println ("????" );
62+ }
63+ }
64+
65+ public void setClock (int hour ) {
66+ String clockstring = "現在時刻は" ;
67+ if (hour < 10 ) {
68+ clockstring += "0" + hour + ":00" ;
69+ } else {
70+ clockstring += hour + ":00" ;
71+ }
72+ this .clock .setText (clockstring );
73+ this .currentState .doClock (this , hour );
74+ }
75+ public void changeState (State state ) {
76+ System .out .println ("state has changed to " + state );
77+ this .currentState = state ;
78+ }
79+ public void callSecurityCenter (String msg ) {
80+ this .screen .append ("Call! " + msg + "\n " );
81+ }
82+ public void recordLog (String msg ) {
83+ this .screen .append ("Log.. " + msg + "\n " );
84+ }
85+ }
0 commit comments