summaryrefslogtreecommitdiffstats
path: root/Bachelor/ERGO/FrameNav.java
blob: c8d2aa64ffcd837ddd0e5142bd56fcf5160b0709 (plain)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import java.awt.*;
import java.awt.event.*;	// f�r ActionListener
import javax.swing.*;
import java.awt.event.KeyEvent;

/**
 * Beispiel f�r einen Dialog mit Border Layout
 * und einem Action Listener f�r zwei Buttons.
 * Der Action Listener ist als innere Klasse realisiert.
 * verwendet ActionEvent.getActionCommand statt ActionEvent.getSource
 * @author Prof. Dr. Ralf Mayer
 * @version 30.10.2005 ver. 1.1
 */
public class FrameNav extends JFrame
{
   // Eine eigene (unsinnige) Navigations-Reihenfolge festlegen
   static final boolean OWN_NAVIGATION_SEQUENCE = false;

   Container c;
   Color colDef;
   MyFocusListener fl = new MyFocusListener();
   ButtonListener bl = new ButtonListener();

   JPanel pCenter = new JPanel();
   JPanel pBottom = new JPanel();
   JPanel pInput  = new JPanel();
   JPanel pButtons= new JPanel();
   JPanel pColButt= new JPanel();

   JLabel labelFname = new JLabel("Vorname");
   JLabel labelLname = new JLabel("Nachname");
   JLabel labelProfess = new JLabel("Beruf");

   JTextField tfFName = new JTextField( 50 );
   JTextField tfLName = new JTextField( 50 );
   JTextField tfProfess = new JTextField( 50 );

   JLabel beschrift = null;
   JButton bRot  	= new JButton( "Rot" );
   JButton bBlau 	= new JButton( "Blau" );
   JButton bGruen 	= new JButton( "Gr�n" );
   JButton bOK 		= new JButton( "OK" );
   JButton bCancel 	= new JButton( "Abbrechen" );

   JMenuBar menuBar = new JMenuBar();
   JMenu menuFile = new JMenu( "Datei");
   JMenu menuAction = new JMenu( "Aktionen" );

   JMenuItem itemExit 	= new JMenuItem( "Beenden", KeyEvent.VK_E );
   JMenuItem itemQuit 	= new JMenuItem( "Abbrechen" );
   JMenuItem itemRot 	= new JMenuItem( "Rot", KeyEvent.VK_R );
   JMenuItem itemBlau 	= new JMenuItem( "Blau", KeyEvent.VK_B);
   JMenuItem itemGruen 	= new JMenuItem( "Gruen", KeyEvent.VK_G );
   JMenuItem itemDefault= new JMenuItem( "Farbe zur�cksetzen", KeyEvent.VK_Z );
   JMenuItem itemClAll  = new JMenuItem( "Alle Felder zur�cksetzen", KeyEvent.VK_C );
   JMenuItem itemClear  = new JMenuItem( "Aktives Feld zur�cksetzen", KeyEvent.VK_F );

   /**
    * Konstruktor
    */
   public FrameNav()
   {
	   c = getContentPane();
	   c.setLayout( new BorderLayout() );

	   // default Button setzen
//       getRootPane().setDefaultButton( bOK );
       // evtl. statt dessen KeyListener benutzen

	   // Input panel
	   pInput.setLayout( new GridLayout(0, 2 ) );
	   pInput.setBorder(BorderFactory.createEtchedBorder());
	   pInput.add( labelFname );
	   pInput.add( tfFName );
	   pInput.add( labelLname );
	   pInput.add( tfLName );
	   pInput.add( labelProfess );
	   pInput.add( tfProfess );
	   c.add( pInput, BorderLayout.NORTH );

	   // Center panel
	   colDef = pCenter.getBackground();          // default Farbe
	   pCenter.setLayout( new GridLayout(0, 1 ) );
	   pCenter.add( new JLabel("Wahl der Hintergrundfarbe") );
	   beschrift = new JLabel( "Text aus Eingabe" );
	   pCenter.add( beschrift );
	   pColButt.add( bRot );
	   pColButt.add( bBlau );
	   pColButt.add( bGruen );
	   pCenter.add( pColButt );
	   pCenter.setBorder(BorderFactory.createEtchedBorder());
	   c.add( pCenter, BorderLayout.CENTER );

       // Button Panel
	   pButtons.setLayout( new FlowLayout() );
	   pButtons.add( bOK );
	   pButtons.add( bCancel );
	   pButtons.setBorder(BorderFactory.createLoweredBevelBorder());
	   c.add( pButtons, BorderLayout.SOUTH );

	   // Mnemonics
	   menuAction.setMnemonic( KeyEvent.VK_A );
	   menuFile.setMnemonic( KeyEvent.VK_D );
	   bOK.setMnemonic( KeyEvent.VK_O );
	   bRot.setMnemonic( KeyEvent.VK_ENTER );

	   // Accelerator
       itemExit.setAccelerator(KeyStroke.getKeyStroke(
                              KeyEvent.VK_E, ActionEvent.ALT_MASK));
       itemClear.setAccelerator(KeyStroke.getKeyStroke(
                              KeyEvent.VK_F, ActionEvent.ALT_MASK));
       itemDefault.setAccelerator(KeyStroke.getKeyStroke(
                              KeyEvent.VK_Z, ActionEvent.ALT_MASK));
       itemQuit.setAccelerator(KeyStroke.getKeyStroke(
                              KeyEvent.VK_Q, ActionEvent.CTRL_MASK));

	   // Tooltips
	   menuFile.setToolTipText("globale Funktionen");
	   menuAction.setToolTipText("spezifische Funktionen");
	   bOK.setToolTipText("Eingabe best�tigen und Anwendung beenden");
	   itemExit.setToolTipText("Eingabe best�tigen und Anwendung beenden");
	   bCancel.setToolTipText("Abbruch der Anwendung ohne Speichern");
	   itemClear.setToolTipText("L�scht aktives Textfeld");
	   itemClAll.setToolTipText("L�scht alle Textfelder");
	   tfProfess.setToolTipText("Beruf ggf. mit \"-\", z.B. Diplom-Architekt");
	   itemDefault.setToolTipText("Zur�cksetzen der Hintergrundfarbe");


	   // Menuleiste aufbauen
	   menuAction.add( itemRot );
       menuAction.add( itemBlau );
       menuAction.add( itemGruen );
       menuAction.addSeparator();
       menuAction.add( itemDefault );
       menuAction.addSeparator();
       menuAction.add( itemClear );

       menuFile.add( itemClAll );
       menuFile.addSeparator();
	   menuFile.add( itemQuit );
	   menuFile.add( itemExit );

	   menuBar.add( menuFile );
	   menuBar.add( menuAction );

	   setJMenuBar(menuBar);

	   // Buttons/Menus ein Komando zuweisen
	   bRot.setActionCommand( "rot" );
	   itemRot.setActionCommand( "rot" );
	   bBlau.setActionCommand( "blau" );
	   itemBlau.setActionCommand( "blau" );
	   bGruen.setActionCommand( "blau" );
	   itemGruen.setActionCommand( "gruen" );
	   itemExit.setActionCommand( "Fertig" );
	   bOK.setActionCommand( "Fertig" );
	   bCancel.setActionCommand( "cancel" );
	   itemQuit.setActionCommand( "cancel" );
	   itemClAll.setActionCommand( "clear all" );
       itemClear.setActionCommand( "clear" );
       itemDefault.setActionCommand( "default color" );

	   /* ActionListener erzeugen
	    * siehe Attribute
	    */
	   // Den Schaltfl�chen den Listener bekannt machen.
	   bRot.addActionListener( bl );
	   bBlau.addActionListener( bl );
	   bGruen.addActionListener( bl );
	   bOK.addActionListener( bl );
	   bCancel.addActionListener( bl );
	   itemRot.addActionListener( bl );
	   itemBlau.addActionListener( bl );
	   itemGruen.addActionListener( bl );
	   itemExit.addActionListener( bl );
	   itemClAll.addActionListener( bl );
	   itemClear.addActionListener( bl );
	   itemDefault.addActionListener( bl );
	   itemQuit.addActionListener( bl );

	   /* FocusListener erzeugen
	    * siehe Attribute
	    */
	   // den Textfeldern den Listener bekannt machen
	   tfFName.addFocusListener( fl );
	   tfLName.addFocusListener( fl );
	   tfProfess.addFocusListener( fl );

	   // Key Listener erzeugen
	   MyKeyListener kl = new MyKeyListener();
	   // den Buttons den Listener bekannt machen
	   bRot.addKeyListener( kl );
	   bGruen.addKeyListener( kl );
	   bBlau.addKeyListener( kl );
	   bOK.addKeyListener( kl );

	   // Focus setzen (hier steht der Cursor)
	   tfFName.requestFocusInWindow();

/*
       // Eine eigene (unsinnige) Navigations-Reihenfolge festlegen
       //   Achtung: Methoden seit JDK 1.4 veraltet (deprecated)
       if (OWN_NAVIGATION_SEQUENCE)
       {
	      tfFName.setNextFocusableComponent( bGruen );
	      bGruen.setNextFocusableComponent( tfLName );
	      tfLName.setNextFocusableComponent( bRot );
	      bRot.setNextFocusableComponent( tfProfess );
	      tfProfess.setNextFocusableComponent( bBlau );
	      bBlau.setNextFocusableComponent( bCancel );
	      bCancel.setNextFocusableComponent( bOK );
	   }

   }
 */

   /**
    * main-Methode.
    */
   public static void main( String[] argv )
   {
	   FrameNav fenster = new FrameNav();
	   fenster.setTitle( "Demo Menu, Shortcuts, Focus and Help " );
	   fenster.setSize( 450, 250 );
	   fenster.setVisible( true );
	   fenster.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
   }

   /**
    * Innere Klasse, implementiert Action Listener
    */
   class ButtonListener implements ActionListener
   {
	   /**
	    * actionPerformed ist die einzige Methode von ActionListener
	    * @param e ActionEvent. Event wird von Komponente an
	    * den Listener gesendet.
	    */
	   public void actionPerformed( ActionEvent e )
	   {
		   if ( e.getActionCommand().equals( "rot" ) )
		      pCenter.setBackground( Color.RED );
		   else if (e.getActionCommand().equals( "blau" ) )
		      pCenter.setBackground( Color.BLUE );
		   else if (e.getActionCommand().equals( "gruen" ) )
		      pCenter.setBackground( Color.GREEN );
		   else if (e.getActionCommand().equals( "default color" ) )
		      pCenter.setBackground( colDef );
		   else if (e.getActionCommand().equals( "cancel" ) )
		      System.exit(0);
		   else if (e.getActionCommand().equals( "Fertig" ) )
		      System.exit(0);
		   else if (e.getActionCommand().equals( "clear all" ) )
		   {
              tfFName.setText("");
              tfLName.setText("");
              tfProfess.setText("");
			  fl.focusLost( null );       // Anzeige updaten
		   }
		   // L�scht das Feld, das gerade den Fokus hat
		   else if (e.getActionCommand().equals( "clear" ) )
		   {
			  if (tfFName.hasFocus() )
			     tfFName.setText("");
			  else if (tfLName.hasFocus() )
			     tfLName.setText("");
			  else if (tfProfess.hasFocus() )
			     tfProfess.setText("");

			  fl.focusLost( null );       // Anzeige updaten
		   }

	   }
   }

   /**
    * Focus Listener
    * Verarbeitet Ereignisse von Komponenten die den
    * Fokus erhalten oder verlieren
    */
   class MyFocusListener implements FocusListener
   {
	   /**
	    * Ereignis: Fokus erhalten. Implementiert FocusListener.
	    * (in diesem Beispiel keine Verwendung)
	    */
	   public void focusGained(FocusEvent e)
	   { }

	   /**
	    * Ereignis: Fokus verloren. Implementiert FocusListener.
	    * (hier: Textkomponente wird ausgewertet.
	    */
	   public void focusLost(FocusEvent e) {
		   beschrift.setText("EINGABE: " +
		                tfLName.getText() + ", " +
		                tfFName.getText() + " (" +
		                tfProfess.getText() + ")"
		                    );
	   }
   }


   class MyKeyListener implements KeyListener
   {

	   public void keyPressed(KeyEvent e)
	   { }	// implementiert, aber hier nicht ben�tigt

	   public void keyReleased(KeyEvent e)
	   { }  // implementiert, aber hier nicht ben�tigt

	   public void keyTyped(KeyEvent e)
	   {
		   System.out.println( e.getKeyChar() );
		   if ( e.getKeyChar() == KeyEvent.VK_ENTER )
		   {
			   JButton source = (JButton) e.getSource();
			   // Aus dem Event ein Action event erzeugen und ...
			   ActionEvent ae = new ActionEvent(
				   source, e.getID(), source.getActionCommand()
		   		  );
		   	   // ... an unseren ButtonListener weiterleiten
		   	   bl.actionPerformed( ae );
	   	   }
	   }

   }


}