Advertisement
Bujkoffer

Visualisierung Notfall

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