/Users/lyon/j4p/src/gui/keyboard/QwertyKeyboard.java
|
1 package gui.keyboard;
2
3 import gui.run.RunButton;
4 import gui.run.RunTextFieldOld;
5
6 import javax.swing.JButton;
7 import javax.swing.JFrame;
8 import javax.swing.JPanel;
9 import java.awt.*;
10 import java.awt.event.ActionEvent;
11 import java.awt.event.ActionListener;
12 import java.util.Vector;
13
14
15 public class QwertyKeyboard {
16 static class TextModel {
17 private String s = "";
18 private RunTextFieldOld readOut;
19
20
21 public void setReadOut(
22 RunTextFieldOld _readOut) {
23 readOut = _readOut;
24 }
25
26 public void ScreenKey(String _s) {
27 s = s + _s;
28 updateReadOut();
29 }
30
31 public void setState(String _s) {
32 s = _s;
33 }
34
35 public void updateReadOut() {
36 if (readOut == null) return;
37 readOut.setText(s);
38 }
39
40 public String getS() {
41 return s;
42 }
43
44 public void setS(String s) {
45 this.s = s;
46 }
47
48 public RunTextFieldOld getReadOut() {
49 return readOut;
50 }
51 }
52
53 class ButtonListener
54 implements ActionListener {
55 public void actionPerformed(
56 ActionEvent e) {
57 Object o = e.getSource();
58 if (o instanceof JButton) {
59 JButton jb = (JButton) o;
60 QwertyKeyboard.tm.ScreenKey(
61 jb.getText());
62 }
63 }
64 }
65
66 class ButtonFrame extends JFrame {
67 Vector buttonList = new Vector();
68
69 ButtonFrame() {
70 Container c = this.getContentPane();
71 c.setLayout(new GridLayout(5, 13));
72 setSize(200, 200);
73 setVisible(true);
74 }
75
76 private boolean isUpper = false;
77
78 public void addButton(Container c,
79 char ch,
80 ButtonListener bc) {
81 JButton b = new JButton(ch + "");
82 b.addActionListener(bc);
83 c.add(b);
84 buttonList.addElement(b);
85 }
86
87 char[] specArray = {0, 1, 2, 3, 4, 5, 6,
88 7, 8, 9, 10, 11, 12,
89 23, 24, 25, 35, 36,
90 44, 45, 46};
91 boolean special = false;
92
93 public void upcaseButtons() {
94
95 for (int i = 0; i <
96 buttonList.size() -
97 1; i++) {
98 RunButton rb = (RunButton) buttonList.elementAt(
99 i);
100 String s = rb.getText();
101 for (int x = 0; x <
102 specArray.length; x++) {
103 // determines if it is non-letter button
104 if (i == (int) specArray[x])
105 special = true;
106 }
107 if (isUpper) {
108 if (special) {
109 rb.setText(
110 down.substring(i,
111 i +
112 1));
113 } else {
114 rb.setText(
115 s.toLowerCase());
116 }
117 } else {
118 if (special) {
119 rb.setText(up.substring(
120 i,
121 i +
122 1));
123 } else {
124 rb.setText(
125 s.toUpperCase());
126 }
127
128 }
129 special = false;
130 }
131 if (isUpper)
132 isUpper = false;
133 else
134 isUpper = true;
135 }
136
137
138 public void addButton(JPanel p,
139 char ch,
140 ButtonListener bc) {
141 RunButton b = new RunButton("[" + ch) {
142 public void run() {
143
144 }
145
146 public Dimension getPreferredSize() {
147 return new Dimension(20, 20);
148 }
149 };
150
151 b.addActionListener(bc);
152 p.add(b);
153 buttonList.addElement(b);
154 }
155
156 }
157
158 ButtonFrame bf = new ButtonFrame();
159 ButtonListener bc = new ButtonListener();
160 Container c = bf.getContentPane();
161
162 JPanel FKeys = new JPanel();
163 JPanel Letters = new JPanel();
164 JPanel numbers = new JPanel();
165 JPanel p = new JPanel();
166 public static JPanel j = new JPanel();
167
168 String down = "`1234567890-=qwertyuiop[]\\asdfghjkl;'zxcvbnm,./";
169 String up = "~!@#$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:\"ZXCVBNM<>?";
170
171 public static String st = "What is your name?";
172
173 static TextModel tm = new TextModel();
174 RunTextFieldOld textViewOld = new RunTextFieldOld() {
175 public void run() {
176 tm.setState(getText());
177 System.out.println(
178 "getText=" + getText());
179 }
180 };
181
182 public String getUppercase() {
183 return down.toUpperCase();
184 }
185
186 public JPanel addRow(char ca[],
187 int min,
188 int max) {
189 JPanel p = new JPanel();
190 p.setLayout(new FlowLayout());
191 for (int x = min; x < max; x++)
192 bf.addButton(p, ca[x], bc);
193 p.invalidate();
194 return p;
195 }
196
197 QwertyKeyboard() {
198 char ca[] = down.toCharArray();
199
200 JPanel keypadPanel = new JPanel();
201 keypadPanel.setLayout(new FlowLayout());
202 keypadPanel.add(addRow(ca, 0, 13));
203 keypadPanel.add(addRow(ca, 13, 26));
204 keypadPanel.add(addRow(ca, 26, 37));
205 keypadPanel.add(
206 addRow(ca, 37, ca.length));
207 keypadPanel.add(new RunButton(
208 "shift lock") {
209 public void run() {
210 bf.upcaseButtons();
211 }
212 });
213
214 bf.addButton(c, ' ', bc);
215 tm.setReadOut(textViewOld);
216
217 c.setLayout(new BorderLayout());
218 c.add(textViewOld, BorderLayout.NORTH);
219 c.add(keypadPanel, BorderLayout.CENTER);
220
221 bf.setSize(650, 300);
222 bf.setVisible(true);
223 bf.invalidate();
224 }
225
226 public static void main(String args[]) {
227 new QwertyKeyboard();
228 }
229 }
230