

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


// für datum
import java.util.Calendar;
import java.util.Date;

// Beispiel mit JSpinner



public class GUI_07 extends JFrame {
  
    JLabel label1 = new JLabel("Numerische Eingabe");
    JSpinner sp1;

  SpinnerListModel spmdl1 = new SpinnerListModel( 
             new Object[] {"Kirschen", "Äpfel", "Birnen", "Bananen", "Pferdeäpfel", "Zitronen"} 
             );
    JLabel label2 = new JLabel("Listen-Eingabe");
    JSpinner sp2 = new JSpinner( spmdl1 );
    
    
    long now = (new Date()).getTime();
  // Spinner von jetzt +/- einem Jahr
  // und einer Veränderung von einem
  // Tag beim Scrollen, imme rum einen ganzen Tag weiter
  long tagMillis = 24*60*60*1000;
  SpinnerDateModel spmdl2 = new SpinnerDateModel(
    new Date(now), 
    new Date(now-tagMillis*356),
    new Date(now+tagMillis*356),
    Calendar.DATE);
  
  JLabel label3 = new JLabel("Datum Eingabe");
    JSpinner sp3 = new JSpinner( spmdl2 );
  private JButton BnOk = new JButton("Ok");
  private JButton BnESC = new JButton("Abbruch");
    
  public GUI_07() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        
    setLocation(200,200);
    setSize(400, 300);
    setGUI();
  }

  private void setGUI() {
    setTitle("GUI mit JSpinner");
    this.getContentPane().setLayout( new GridBagLayout() );

    sp1 = new JSpinner( new SpinnerNumberModel(0,0, 100, 1)   );

    this.getContentPane().add(label1,
           new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
            ,GridBagConstraints.NORTHEAST, GridBagConstraints.NONE,
            new Insets(30,10,0,10), 0, 0));

    this.getContentPane().add(sp1,
           new GridBagConstraints(1, 0, 1, 1, 100.0, 0.0
            ,GridBagConstraints.NORTHEAST, GridBagConstraints.HORIZONTAL,
            new Insets(30,10,0,10), 0, 0));

    label1.setFont( new Font("Arial", Font.BOLD,18)  );
    sp1.setFont( new Font("Arial", Font.BOLD,18)  );


    // --------------------------------
    
    this.getContentPane().add(label2,
           new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
            ,GridBagConstraints.NORTHEAST, GridBagConstraints.NONE,
            new Insets(30,10,0,10), 0, 0));

    this.getContentPane().add(sp2,
           new GridBagConstraints(1, 1, 1, 1, 100.0, 0.0
            ,GridBagConstraints.NORTHEAST, GridBagConstraints.HORIZONTAL,
            new Insets(30,10,0,10), 0, 0));

    label2.setFont( new Font("Arial", Font.BOLD,18)  );
    sp2.setFont( new Font("Arial", Font.BOLD,18)  );


    // --------------------------------
    
    this.getContentPane().add(label3,
           new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0
            ,GridBagConstraints.NORTHEAST, GridBagConstraints.NONE,
            new Insets(30,10,0,10), 0, 0));

    this.getContentPane().add(sp3,
           new GridBagConstraints(1, 2, 1, 1, 100.0, 0.0
            ,GridBagConstraints.NORTHEAST, GridBagConstraints.HORIZONTAL,
            new Insets(30,10,0,10), 0, 0));

    label3.setFont( new Font("Arial", Font.BOLD,18)  );
    sp3.setFont( new Font("Arial", Font.BOLD,18)  );

    
// -----------------------------------
    
   sp1.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                changeState1(evt);
            }
        });

    sp2.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                changeState2(evt);
            }
        });

    sp3.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                changeState3(evt);
            }
        });

    // -----------------------------------
        
        
    // Schalter
    BnOk.setFont( new Font("Arial", Font.BOLD,18)  );
    BnESC.setFont( new Font("Arial", Font.BOLD,18)  );
    JPanel pn = new JPanel();
    pn.setLayout( new FlowLayout() );
    pn.add(BnOk);
    pn.add(BnESC);
//    pn.setBackground(Color.green);
    this.getContentPane().add(pn,
           new GridBagConstraints(0, 3, 2, 1, 1.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
            new Insets(30,0,0,0), 0, 0));

    BnOk.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        BnOk_Click();
      }
    });
    BnESC.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        BnESC_Click();
      }
    });
    
  } // setGUI


  // JSpinner ist numerisch
    private void changeState1(javax.swing.event.ChangeEvent evt) {
        Integer I = (Integer) sp1.getValue();
        System.out.println( I.intValue() );
    }

    // JSpinner ist eine Liste
    private void changeState2(javax.swing.event.ChangeEvent evt) {
        String s = (String) sp2.getValue();
        System.out.println( s );
    }

    // JSpinner ist ein Datum
    private void changeState3(javax.swing.event.ChangeEvent evt) {
        Date d = (Date) sp3.getValue();
        System.out.println( d );
    }

  private void BnOk_Click() {
    System.exit(0);
  }

  private void BnESC_Click() {
    System.exit(0);
  }

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

}

