
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

/*
Durch die beiden Schalter wird das Label geändert.
Dabei werden auch die Schriften geändert
Jeder Schalter hat ein eigenen Action_listener
*/

public class JButton_Bsp01 extends JFrame {
  JToolBar toolBar = new JToolBar();
  JButton Bn1 = new JButton();
  JButton Bn2 = new JButton();
  JLabel label1;


  //Frame konstruieren
  public JButton_Bsp01() {
      // Statt windowsClosing, ab JDK 1,4
    //setDefaultCloseOperation( JFrame.HIDE_ON_CLOSE );
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    this.setSize(new Dimension(400, 300));
    this.setTitle("JButton_Bsp01");
      setGUI();
  }
    /*
     DO_NOTHING_ON_CLOSE - do not do anything - require the program to handle
                           the operation in the windowClosing method of a registered
                           WindowListener object.
     HIDE_ON_CLOSE       - automatically hide the frame after invoking any
                           registered WindowListener objects
     DISPOSE_ON_CLOSE    - automatically hide and dispose the frame after invoking
                           any registered WindowListener objects
     EXIT_ON_CLOSE
    */

  //Initialisierung der Komponente
  private void setGUI() {
    Bn1.setText("1. Labeländerung");
    Bn1.setToolTipText("Ändert den Text im Anzeigeelement");
    Bn1.addActionListener(new java.awt.event.ActionListener() {

      public void actionPerformed(ActionEvent e) {
        Bn1_actionPerformed(e);
      }
    });
    toolBar.add(Bn1);
    Bn1.setFont( new Font("Arial", Font.BOLD,18)  );


    Bn2.setText("2. Labeländerung");
    Bn2.setToolTipText("Ändert den Text im Anzeigeelement");
    Bn2.addActionListener(new java.awt.event.ActionListener() {

      public void actionPerformed(ActionEvent e) {
        Bn2_actionPerformed(e);
      }
    });
    toolBar.add(Bn2);
    Bn2.setFont( new Font("Arial", Font.BOLD,18)  );


    this.getContentPane().add(toolBar, BorderLayout.NORTH);

    label1 = new JLabel("Text");
    label1.setText("Dies ist ein Anzeigetext");
    label1.setFont( new Font("Arial", Font.BOLD,18)  );


    this.getContentPane().add(label1, BorderLayout.CENTER);

     Bn1.setFont( new Font("Arial", Font.BOLD,24)  );
     Bn1.setForeground( Color.blue );
     Bn1.setBackground( Color.yellow );  // wird nicht gesetzt

  }  // jbInit



 void Bn1_actionPerformed(ActionEvent e) {
     // e.getSource()
     System.out.println("Schalter1 angeklickt");
     label1.setText("JButton1 angeklickt");
     label1.setFont( new Font("TimesRoman", Font.ITALIC,32)  );
     label1.setForeground( Color.red );

 }


 void Bn2_actionPerformed(ActionEvent e) {
     System.out.println("Schalter2 angeklickt");
     label1.setText("JButton2 angeklickt");
     label1.setFont( new Font("Arial", Font.PLAIN,14)  );
     label1.setForeground( Color.blue );
     label1.setBackground( Color.yellow );  // wird nicht gesetzt
  }



  public static void main(String[] args) {
    JButton_Bsp01 frame = new JButton_Bsp01();
    frame.setVisible(true);
    frame.show();
  }


}

   

