summaryrefslogtreecommitdiffstats
path: root/lib/example/TypicalGui.java
diff options
context:
space:
mode:
Diffstat (limited to 'lib/example/TypicalGui.java')
-rw-r--r--lib/example/TypicalGui.java179
1 files changed, 179 insertions, 0 deletions
diff --git a/lib/example/TypicalGui.java b/lib/example/TypicalGui.java
new file mode 100644
index 0000000..ab80ff1
--- /dev/null
+++ b/lib/example/TypicalGui.java
@@ -0,0 +1,179 @@
1package example;
2
3
4
5import java.awt.*;
6import java.awt.event.*;
7import javax.swing.*;
8import layout.TableLayout;
9
10
11
12public class TypicalGui extends JFrame
13{
14
15
16
17 public static void main (String args[])
18 {
19 new TypicalGui();
20 }
21
22
23
24 public TypicalGui ()
25 {
26 super ("A Typical GUI");
27
28 Container pane = getContentPane();
29
30 double b = 10;
31 double f = TableLayout.FILL;
32 double p = TableLayout.PREFERRED;
33 double size[][] = {{b, f, 5, p, 5, p, b}, {p, b, f, 10, p, b}};
34 TableLayout layout = new TableLayout(size);
35 pane.setLayout (layout);
36
37 addMenu(pane);
38 addCommandButtons(pane, layout);
39 addColorBoxes(pane, layout);
40 addTextArea(pane, layout);
41
42 allowClosing();
43 setSize (640, 480);
44 show();
45 }
46
47
48
49 public void addMenu (Container pane)
50 {
51 JMenuBar menuBar = new JMenuBar();
52
53 String menuText[] = {"File", "Edit", "View", "Help"};
54 String itemText[][] =
55 {{"New", "Open", "Save", "Print", "Exit"},
56 {"Cut", "Copy", "Paste"},
57 {"Zoom in", "Zoom out"},
58 {"About", "Index", "Search"}};
59
60 for (int i = 0; i < menuText.length; i++)
61 {
62 JMenu menu = new JMenu(menuText[i]);
63 menuBar.add (menu);
64
65 for (int j = 0; j < itemText[i].length; j++)
66 {
67 JMenuItem item = new JMenuItem(itemText[i][j]);
68 menu.add (item);
69 }
70 }
71
72 pane.add (menuBar, "0, 0, 6, 0");
73 }
74
75
76
77 public void addCommandButtons (Container pane, TableLayout layout)
78 {
79 JPanel buttonPanel = new JPanel();
80 pane.add (buttonPanel, "1, 4, 5, 4");
81
82 for (int i = 1; i <= 5; i++)
83 {
84 JButton button = new JButton("Button " + i);
85 buttonPanel.add (button);
86 }
87 }
88
89
90
91 public void addColorBoxes (Container pane, TableLayout layout)
92 {
93 Color color[][] =
94 {{Color.white, Color.black},
95 {Color.green, Color.blue},
96 {Color.red, Color.yellow},
97 {Color.pink, Color.orange},
98 {Color.magenta, Color.cyan}};
99
100 for (int i = 0; i < color.length; i++)
101 {
102 // Add a row for spacing and a row for the color boxes
103 layout.insertRow (2, TableLayout.PREFERRED);
104 layout.insertRow (2, 5);
105
106 // Add color boxes
107 pane.add (new ColorBox(color[i][0]), "3, 3");
108 pane.add (new ColorBox(color[i][1]), "5, 3");
109 }
110
111 // Remove the unnecessary leading space
112 layout.deleteRow (2);
113 }
114
115
116
117 public void addTextArea (Container pane, TableLayout layout)
118 {
119 int numRow = layout.getRow().length;
120 JTextPane textArea = new JTextPane();
121 pane.add (textArea, "1, 2, 1, " + (numRow - 4));
122 }
123
124
125
126 public void allowClosing ()
127 {
128 addWindowListener
129 (new WindowAdapter()
130 {
131 public void windowClosing (WindowEvent e)
132 {
133 System.exit (0);
134 }
135 }
136 );
137 }
138
139
140
141 //**************************************************************************
142 //*** Inner classes ***
143 //**************************************************************************
144
145
146
147 protected class ColorBox extends Component
148 {
149 protected Color color;
150
151 protected ColorBox (Color color)
152 {
153 this.color = color;
154 }
155
156 public void update (Graphics g)
157 {
158 paint (g);
159 }
160
161 public void paint (Graphics g)
162 {
163 Dimension d = getSize();
164 g.setColor (Color.black);
165 g.drawRect (0, 0, d.width - 1, d.height - 1);
166
167 g.setColor (color);
168 g.fillRect (1, 1, d.width - 1, d.height - 1);
169 }
170
171 public Dimension getPreferredSize ()
172 {
173 return new Dimension(40, 20);;
174 }
175 }
176
177
178
179}