D:\NewZebra\JavaApplications\DesignPatternsProject\src\behavior_patterns\command_pattern_nested_menu\Two_Item_3.java |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package behavior_patterns.command_pattern_nested_menu;
21
22
23
24 @author
25
26
27 import java.text.*;
28 import java.util.*;
29 import java.awt.event.*;
30 import java.awt.*;
31 import javax.swing.*;
32
33
34 public class Two_Item_3 implements Command {
35
36 String currentFunctionName;
37 JPanel functionsPanel = null;
38 JPanel informationJPanel = null;
39 int whichOne;
40
41
42 public Two_Item_3() {
43 }
44
45
46
47 public void execute(CommandPanel passedCommandPanel){
48 if(passedCommandPanel != null) {
49 passedCommandPanel.removeAll();
50 passedCommandPanel.setBackground(Color.PINK);
51
52 functionsPanel = new JPanel();
53 functionsPanel.setBounds(20,20,200,500);
54 functionsPanel.setLayout(null);
55
56 informationJPanel = new JPanel();
57 informationJPanel.setBounds(240,20,530,500);
58 informationJPanel.setLayout(null);
59
60 whichOne = 1;
61 addFnctionJButtons(functionsPanel, "Command 1", 20);
62 whichOne = 2;
63 addFnctionJButtons(functionsPanel, "Command 2", 80);
64 whichOne = 3;
65 addFnctionJButtons(functionsPanel, "Command 3", 140);
66 whichOne = 4;
67 addFnctionJButtons(functionsPanel, "Command 4", 200);
68 passedCommandPanel.add(informationJPanel);
69 passedCommandPanel.add(functionsPanel);
70
71 }
72 else
73 reportError();
74 }
75
76
77
78 public void reportError(){
79 JOptionPane.showMessageDialog(null, "Two_Item_3 ", "Field Error", JOptionPane.ERROR_MESSAGE);
80 }
81
82
83
84 void addFnctionJButtons(JPanel PassedFunctionPanel, String currentFunctionName, int functions_y_position){
85
86
87 Cursor handCursor = new Cursor(Cursor.HAND_CURSOR);
88
89
90 JButton aButton= new JButton(currentFunctionName);
91 aButton.setCursor(handCursor);
92
93 aButton.setBounds(20, functions_y_position, 160, 20);
94 PassedFunctionPanel.add(aButton);
95 final int localOne = whichOne;
96 aButton.addActionListener(new ActionListener()
97 {
98 public void actionPerformed(ActionEvent PassedActionEvent)
99 {
100 whichOne = localOne;
101 writelabel();
102 }
103 });
104 }
105 public void writelabel(){
106
107 String tString = "Function: ";
108 switch(whichOne)
109 {
110 case 2:
111 informationJPanel.setBackground(Color.white);
112 tString += "2";
113 break;
114 case 3:
115 informationJPanel.setBackground(Color.gray);
116 tString += "3";
117 break;
118 case 4:
119 informationJPanel.setBackground(Color.red);
120 tString += "4";
121 break;
122 case 1:
123 default:
124 informationJPanel.setBackground(Color.green);
125 tString += "1";
126 }
127 JLabel aJLabel = new JLabel(tString);
128 aJLabel.setFont(new Font("TimesRoman", Font.BOLD, 20));
129 aJLabel.setBounds(100, 200, 250,30);
130 aJLabel.setForeground(Color.black);
131 if(informationJPanel != null){
132 informationJPanel.removeAll();
133 informationJPanel.add(aJLabel);
134 }
135 }
136
137 }
138
139