Advertisement
Bujkoffer

calc.exe v2

Jan 19th, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.97 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.EventQueue;
  3.  
  4. import javax.swing.JFrame;
  5. import javax.swing.JPanel;
  6. import javax.swing.border.EmptyBorder;
  7. import javax.swing.JTextField;
  8. import javax.swing.JButton;
  9. import java.awt.event.ActionListener;
  10. import java.awt.event.ActionEvent;
  11. import java.awt.Color;
  12.  
  13. public class Visualisierung extends JFrame {
  14.  
  15.     private JPanel contentPane;
  16.     private JTextField textField;
  17.  
  18.     private int ZahlEins;
  19.     private int ZahlZwei;
  20.     private int Ergebnis;
  21.  
  22.     private boolean hatZahlEins;
  23.     private boolean hatZahlZwei;
  24.     private boolean hatErgebnis;
  25.  
  26.     char Rechenart;
  27.     boolean hatRechenart;
  28.  
  29.     /**
  30.      * Launch the application.
  31.      */
  32.     public static void main(String[] args) {
  33.         EventQueue.invokeLater(new Runnable() {
  34.             public void run() {
  35.                 try {
  36.                     Visualisierung frame = new Visualisierung();
  37.                     frame.setVisible(true);
  38.                 } catch (Exception e) {
  39.                     e.printStackTrace();
  40.                 }
  41.             }
  42.         });
  43.     }
  44.  
  45.     /**
  46.      * Create the frame.
  47.      */
  48.     public Visualisierung() {
  49.         setBackground(Color.YELLOW);
  50.         setTitle("Bujkoffer\u00B4s Rechner");
  51.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  52.         setBounds(100, 100, 400, 314);
  53.         contentPane = new JPanel();
  54.         contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  55.         contentPane.setLayout(new BorderLayout(0, 0));
  56.         setContentPane(contentPane);
  57.  
  58.         JPanel panel = new JPanel();
  59.         panel.setBackground(Color.YELLOW);
  60.         contentPane.add(panel, BorderLayout.CENTER);
  61.  
  62.         JButton btnNewButton_1 = new JButton("2");
  63.         btnNewButton_1.setBounds(84, 180, 47, 23);
  64.         btnNewButton_1.addActionListener(new ActionListener() {
  65.             public void actionPerformed(ActionEvent arg0) {
  66.                 eingabeZahl(2);
  67.             }
  68.         });
  69.         panel.setLayout(null);
  70.         panel.add(btnNewButton_1);
  71.  
  72.         JButton btnNewButton = new JButton("8");
  73.         btnNewButton.setBounds(84, 112, 47, 23);
  74.         btnNewButton.addActionListener(new ActionListener() {
  75.             public void actionPerformed(ActionEvent arg0) {
  76.                 eingabeZahl(8);
  77.             }
  78.         });
  79.         panel.add(btnNewButton);
  80.  
  81.         JButton btnNewButton_2 = new JButton("1");
  82.         btnNewButton_2.setBounds(26, 180, 48, 23);
  83.         btnNewButton_2.addActionListener(new ActionListener() {
  84.             public void actionPerformed(ActionEvent arg0) {
  85.                 eingabeZahl(1);
  86.             }
  87.         });
  88.         panel.add(btnNewButton_2);
  89.  
  90.         JButton btnNewButton_3 = new JButton("3");
  91.         btnNewButton_3.setBounds(141, 180, 44, 23);
  92.         btnNewButton_3.addActionListener(new ActionListener() {
  93.             public void actionPerformed(ActionEvent arg0) {
  94.                 eingabeZahl(3);
  95.             }
  96.         });
  97.         panel.add(btnNewButton_3);
  98.  
  99.         JButton btnNewButton_4 = new JButton("6");
  100.         btnNewButton_4.setBounds(141, 146, 44, 23);
  101.         btnNewButton_4.addActionListener(new ActionListener() {
  102.             public void actionPerformed(ActionEvent arg0) {
  103.                 eingabeZahl(6);
  104.             }
  105.         });
  106.         panel.add(btnNewButton_4);
  107.  
  108.         JButton btnNewButton_5 = new JButton("5");
  109.         btnNewButton_5.setBounds(84, 146, 47, 23);
  110.         btnNewButton_5.addActionListener(new ActionListener() {
  111.             public void actionPerformed(ActionEvent arg0) {
  112.                 eingabeZahl(5);
  113.             }
  114.         });
  115.         panel.add(btnNewButton_5);
  116.  
  117.         JButton btnNewButton_6 = new JButton("9");
  118.         btnNewButton_6.setBounds(141, 112, 44, 23);
  119.         btnNewButton_6.addActionListener(new ActionListener() {
  120.             public void actionPerformed(ActionEvent arg0) {
  121.                 eingabeZahl(9);
  122.             }
  123.         });
  124.         panel.add(btnNewButton_6);
  125.  
  126.         JButton btnNewButton_7 = new JButton("4");
  127.         btnNewButton_7.setBounds(26, 146, 48, 23);
  128.         btnNewButton_7.addActionListener(new ActionListener() {
  129.             public void actionPerformed(ActionEvent arg0) {
  130.                 eingabeZahl(4);
  131.             }
  132.  
  133.         });
  134.         panel.add(btnNewButton_7);
  135.  
  136.         JButton btnNewButton_8 = new JButton("0");
  137.         btnNewButton_8.setBounds(26, 220, 159, 23);
  138.         panel.add(btnNewButton_8);
  139.  
  140.         JButton btnNewButton_9 = new JButton("7");
  141.         btnNewButton_9.setBounds(26, 112, 48, 23);
  142.         btnNewButton_9.addActionListener(new ActionListener() {
  143.             public void actionPerformed(ActionEvent arg0) {
  144.                 eingabeZahl(7);
  145.             }
  146.         });
  147.         panel.add(btnNewButton_9);
  148.  
  149.         JButton btnNewButton_10 = new JButton("/");
  150.         btnNewButton_10.addActionListener(new ActionListener() {
  151.             public void actionPerformed(ActionEvent arg0) {
  152.                 eingabeRechenart('/');
  153.             }
  154.         });
  155.         btnNewButton_10.setBounds(214, 220, 51, 23);
  156.         panel.add(btnNewButton_10);
  157.  
  158.         JButton btnNewButton_11 = new JButton("*");
  159.         btnNewButton_11.addActionListener(new ActionListener() {
  160.             public void actionPerformed(ActionEvent arg0) {
  161.                 eingabeRechenart('*');
  162.             }
  163.         });
  164.         btnNewButton_11.setBounds(214, 180, 51, 23);
  165.         panel.add(btnNewButton_11);
  166.  
  167.         JButton btnNewButton_12 = new JButton("-");
  168.         btnNewButton_12.setBounds(214, 146, 51, 23);
  169.         btnNewButton_12.addActionListener(new ActionListener() {
  170.             public void actionPerformed(ActionEvent arg0) {
  171.                 eingabeRechenart('-');
  172.             }
  173.         });
  174.         panel.add(btnNewButton_12);
  175.  
  176.         JButton button = new JButton("+");
  177.         button.addActionListener(new ActionListener() {
  178.             public void actionPerformed(ActionEvent arg0) {
  179.                 eingabeRechenart('+');
  180.             }
  181.         });
  182.         button.setBounds(214, 112, 51, 23);
  183.         panel.add(button);
  184.  
  185.         JButton button_1 = new JButton("=");
  186.         button_1.setBounds(275, 220, 89, 23);
  187.         button_1.addActionListener(new ActionListener() {
  188.             public void actionPerformed(ActionEvent arg0) {
  189.                 ausrechnen();
  190.             }
  191.         });
  192.         panel.add(button_1);
  193.  
  194.         textField = new JTextField();
  195.         textField.setBounds(100, 26, 172, 53);
  196.         panel.add(textField);
  197.         textField.setColumns(10);
  198.        
  199.         JButton btnNewButton_13 = new JButton("RESET");
  200.         btnNewButton_13.setBounds(275, 108, 89, 95);
  201.         panel.add(btnNewButton_13);
  202.     }
  203.  
  204.     public void eingabeRechenart(char rechenart) {
  205.         if (!hatRechenart) {
  206.             Rechenart = rechenart;
  207.             aktualisiereText();
  208.             hatRechenart = true;
  209.         }
  210.     }
  211.  
  212.     public void eingabeZahl(int zahl) {
  213.  
  214.         if (!hatZahlEins) {
  215.             ZahlEins = zahl;
  216.             hatZahlEins = true;
  217.         } else if (!hatZahlZwei) {
  218.             ZahlZwei = zahl;
  219.             hatZahlZwei = true;
  220.         }
  221.  
  222.         aktualisiereText();
  223.     }
  224.  
  225.     public void aktualisiereText() {
  226.  
  227.         String text = "";
  228.         if (hatZahlEins) {
  229.             text += ZahlEins;
  230.  
  231.         }
  232.         if (hatRechenart) {
  233.             text += Rechenart;
  234.         }
  235.         if (hatZahlZwei) {
  236.             text += ZahlZwei;
  237.  
  238.         }
  239.  
  240.         if (hatErgebnis) {
  241.             text += " = " + Ergebnis;
  242.         }
  243.         textField.setText(text);
  244.     }
  245.  
  246.     public void ausrechnen() {
  247.         switch (Rechenart) {
  248.         case '+':
  249.             Ergebnis = addieren(ZahlEins, ZahlZwei);
  250.             break;
  251.         case '-':
  252.             Ergebnis = subtrahieren(ZahlEins, ZahlZwei);
  253.             break;
  254.         case '*':
  255.             Ergebnis = multiplizieren(ZahlEins, ZahlZwei);
  256.             break;
  257.         case '/':
  258.             Ergebnis = dividieren(ZahlEins, ZahlZwei);
  259.  
  260.         default:
  261.             // System.out.println("Was ein Scheiß !");
  262.             break;
  263.         }
  264.         hatErgebnis = true;
  265.        
  266.         aktualisiereText();
  267.  
  268.     }
  269.  
  270.     private static int addieren(int eins, int zwei) {
  271.         return eins + zwei;
  272.  
  273.     }
  274.  
  275.     private static int subtrahieren(int eins, int zwei) {
  276.         return eins - zwei;
  277.     }
  278.  
  279.     private static int multiplizieren(int eins, int zwei) {
  280.         return eins * zwei;
  281.     }
  282.  
  283.     private static int dividieren(int eins, int zwei) {
  284.         return eins / zwei;
  285.     }
  286. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement