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 ); } } } }