File #obooek51-2867 - JAVA - Sourcecode

Uploaded by greenlight - 07/03/2010 18:26 - 38 Views
Source code
  1. package de.musin.bsinfo.rechner.lib;
  2.  
  3.  
  4. import java.awt.Dimension;
  5. import java.awt.GridBagConstraints;
  6. import java.awt.GridBagLayout;
  7. import java.awt.Insets;
  8. import java.awt.Toolkit;
  9. import java.awt.event.WindowAdapter;
  10. import java.awt.event.WindowEvent;
  11.  
  12. import javax.swing.JFrame;
  13. import javax.swing.JTextField;
  14.  
  15.  
  16. /**
  17.  * Frame für den Taschenrechner. <br>
  18.  * Implementiert {@link Runnable} um als eigenständiges {@link Thread} zu laufen.<br>
  19.  * Implementiert {@link ViewControl} damit der Controller zugriff auf das Text-Feld hat.
  20.  * 
  21.  * @author Andreas Fischer @ 2010
  22.  * 
  23.  */
  24. public final class View extends JFrame implements ViewControl, Runnable
  25. {
  26.  
  27.     private static final long serialVersionUID = 5579061763087229590L;
  28.  
  29.     private final JTextField textField;
  30.  
  31.     private final Control control;
  32.  
  33.  
  34.     /**
  35.      * Erstellt das Frame.
  36.      * 
  37.      * @param control
  38.      *            {@link Control} für den Listener.
  39.      */
  40.     public View(Control control)
  41.     {
  42.         super("Taschenrechner");
  43.         addWindowListener(new WindowAdapter()
  44.         {
  45.             public void windowClosing(WindowEvent e)
  46.             {
  47.                 System.exit(0);
  48.             }
  49.         });
  50.         setLayout(new GridBagLayout());
  51.  
  52.         // control als Instanzvariable speichern
  53.         this.control = control;
  54.  
  55.         // Textfeld rechtsbündig und nicht editierbar
  56.         textField = new JTextField("0");
  57.         textField.setHorizontalAlignment(JTextField.RIGHT);
  58.         textField.setEditable(false);
  59.         add(textField, createGBC(0, 0, 5, 1));
  60.  
  61.         // Alle Buttons in Reihenfolge
  62.         addButton(0, 1, 1, 1, "1");
  63.         addButton(1, 1, 1, 1, "2");
  64.         addButton(2, 1, 1, 1, "3");
  65.         addButton(3, 1, 2, 1, "C");
  66.         addButton(0, 2, 1, 1, "4");
  67.         addButton(1, 2, 1, 1, "5");
  68.         addButton(2, 2, 1, 1, "6");
  69.         addButton(3, 2, 1, 1, "+");
  70.         addButton(4, 2, 1, 1, "-");
  71.         addButton(0, 3, 1, 1, "7");
  72.         addButton(1, 3, 1, 1, "8");
  73.         addButton(2, 3, 1, 1, "9");
  74.         addButton(3, 3, 1, 1, "÷");
  75.         addButton(4, 3, 1, 1, "*");
  76.         addButton(0, 4, 1, 1, "0");
  77.         addButton(1, 4, 1, 1, ",");
  78.         addButton(2, 4, 1, 1, "±");
  79.         addButton(3, 4, 2, 1, "=");
  80.  
  81.         pack();
  82.         setResizable(false);
  83.     }
  84.  
  85.  
  86.     public String getText()
  87.     {
  88.         return textField.getText();
  89.     }
  90.  
  91.  
  92.     public void setText(String text)
  93.     {
  94.         textField.setText(text);
  95.     }
  96.  
  97.  
  98.     public void append(String text)
  99.     {
  100.         textField.setText(getText() + text);
  101.     }
  102.  
  103.  
  104.     public void clear()
  105.     {
  106.         textField.setText("0");
  107.     }
  108.  
  109.  
  110.     /**
  111.      * Fügt einen neuen {@link CoolButton} in das ContentPane des Frames ein.
  112.      * 
  113.      * @param x
  114.      *            Position X im {@link GridBagLayout}
  115.      * @param y
  116.      *            Position Y im {@link GridBagLayout}
  117.      * @param width
  118.      *            Zellen-Spannweite horizontal
  119.      * @param height
  120.      *            Zellen-Spannweite vertikal
  121.      * @param value
  122.      *            Button Text
  123.      */
  124.     private void addButton(int x, int y, int width, int height, String value)
  125.     {
  126.         CoolButton btn = new CoolButton(value);
  127.         btn.addActionListener(control);
  128.         GridBagConstraints gbc = createGBC(x, y, width, height);
  129.         // noch ein wenig die Größe verändern
  130.         gbc.ipadx = 30;
  131.         gbc.ipady = 12;
  132.         add(btn, gbc);
  133.     }
  134.  
  135.  
  136.     /**
  137.      * Erstellt einen generisches {@link GridBagConstraints} Objekt mit default Werten.
  138.      * 
  139.      * @param x
  140.      *            Position X
  141.      * @param y
  142.      *            Position Y
  143.      * @param width
  144.      *            Zellen-Spannweite horizontal
  145.      * @param height
  146.      *            Zellen-Spannweite vertikal
  147.      * @return Ein neues {@link GridBagConstraints} Objekt
  148.      */
  149.     private GridBagConstraints createGBC(int x, int y, int width, int height)
  150.     {
  151.         GridBagConstraints gbc = new GridBagConstraints();
  152.         gbc.gridx = x;
  153.         gbc.gridy = y;
  154.         gbc.gridwidth = width;
  155.         gbc.gridheight = height;
  156.         gbc.fill = GridBagConstraints.BOTH;
  157.         gbc.anchor = GridBagConstraints.CENTER;
  158.         gbc.insets = new Insets(2, 2, 2, 2);
  159.         return gbc;
  160.     }
  161.  
  162.  
  163.     public void run()
  164.     {
  165.         // Frame zentrieren und anzeigen
  166.         Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
  167.         setLocation(screen.width / 2 - getWidth() / 2, screen.height / 2 - getHeight() / 2);
  168.         setVisible(true);
  169.     }
  170.  
  171. }