Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.Desktop;
- import java.awt.EventQueue;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.IOException;
- import java.net.URI;
- import java.net.URISyntaxException;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JTextField;
- import javax.swing.border.EmptyBorder;
- public class Visualisierung extends JFrame {
- private JPanel contentPane;
- private JTextField textField;
- private int ZahlEins;
- private int ZahlZwei;
- private int Ergebnis;
- private boolean hatZahlEins;
- private boolean hatZahlZwei;
- private boolean hatErgebnis;
- char Rechenart;
- boolean hatRechenart;
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- Visualisierung frame = new Visualisierung();
- frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- /**
- * Create the frame.
- */
- public Visualisierung() {
- setBackground(Color.YELLOW);
- setTitle("Bujkoffer\u00B4s Rechner");
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(100, 100, 400, 314);
- contentPane = new JPanel();
- contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
- contentPane.setLayout(new BorderLayout(0, 0));
- setContentPane(contentPane);
- JPanel panel = new JPanel();
- panel.setBackground(Color.YELLOW);
- contentPane.add(panel, BorderLayout.CENTER);
- JButton Button2 = new JButton("2");
- Button2.setBounds(84, 180, 47, 23);
- Button2.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- eingabeZahl(2);
- }
- });
- panel.setLayout(null);
- panel.add(Button2);
- JButton Button8 = new JButton("8");
- Button8.setBounds(84, 112, 47, 23);
- Button8.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- eingabeZahl(8);
- }
- });
- panel.add(Button8);
- JButton Button1 = new JButton("1");
- Button1.setBounds(26, 180, 48, 23);
- Button1.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- eingabeZahl(1);
- }
- });
- panel.add(Button1);
- JButton Button3 = new JButton("3");
- Button3.setBounds(141, 180, 44, 23);
- Button3.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- eingabeZahl(3);
- }
- });
- panel.add(Button3);
- JButton Button6 = new JButton("6");
- Button6.setBounds(141, 146, 44, 23);
- Button6.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- eingabeZahl(6);
- }
- });
- panel.add(Button6);
- JButton Button5 = new JButton("5");
- Button5.setBounds(84, 146, 47, 23);
- Button5.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- eingabeZahl(5);
- }
- });
- panel.add(Button5);
- JButton Button9 = new JButton("9");
- Button9.setBounds(141, 112, 44, 23);
- Button9.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- eingabeZahl(9);
- }
- });
- panel.add(Button9);
- JButton Button4 = new JButton("4");
- Button4.setBounds(26, 146, 48, 23);
- Button4.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- eingabeZahl(4);
- }
- });
- panel.add(Button4);
- JButton btnNewButton_8 = new JButton("0");
- btnNewButton_8.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- eingabeZahl(0);
- }
- });
- btnNewButton_8.setBounds(26, 220, 159, 23);
- panel.add(btnNewButton_8);
- JButton Button7 = new JButton("7");
- Button7.setBounds(26, 112, 48, 23);
- Button7.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- eingabeZahl(7);
- }
- });
- panel.add(Button7);
- JButton ButtonGeteilt = new JButton("/");
- ButtonGeteilt.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- eingabeRechenart('/');
- }
- });
- ButtonGeteilt.setBounds(214, 220, 51, 23);
- panel.add(ButtonGeteilt);
- JButton ButtonMal = new JButton("*");
- ButtonMal.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- eingabeRechenart('*');
- }
- });
- ButtonMal.setBounds(214, 180, 51, 23);
- panel.add(ButtonMal);
- JButton ButtonMinus = new JButton("-");
- ButtonMinus.setBounds(214, 146, 51, 23);
- ButtonMinus.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- eingabeRechenart('-');
- }
- });
- panel.add(ButtonMinus);
- JButton ButtonPlus = new JButton("+");
- ButtonPlus.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- eingabeRechenart('+');
- }
- });
- ButtonPlus.setBounds(214, 112, 51, 23);
- panel.add(ButtonPlus);
- JButton ButtonGleich = new JButton("=");
- ButtonGleich.setBounds(275, 220, 89, 23);
- ButtonGleich.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- ausrechnen();
- }
- });
- panel.add(ButtonGleich);
- textField = new JTextField();
- textField.setBounds(100, 26, 172, 53);
- panel.add(textField);
- textField.setColumns(10);
- JButton ButtonReset = new JButton("RESET");
- ButtonReset.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- Neustart();
- }
- });
- ButtonReset.setBounds(275, 108, 89, 95);
- panel.add(ButtonReset);
- JButton ButtonDeveloper = new JButton("Developer");
- ButtonDeveloper.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- if (Desktop.isDesktopSupported()) {
- try {
- int showConfirmDialog = JOptionPane.showConfirmDialog(contentPane, "Wirklich?");
- if (showConfirmDialog == JOptionPane.YES_OPTION) {
- Desktop.getDesktop().browse(new URI(
- "https://www.youtube.com/channel/UCPNfso7tNCbIDc5wmzrQGiA?sub_confirmation=1"));
- }
- } catch (IOException | URISyntaxException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- });
- ButtonDeveloper.setBounds(264, 0, 110, 23);
- panel.add(ButtonDeveloper);
- }
- public void Neustart() {
- ZahlEins = ZahlZwei = 0;
- hatZahlEins = hatZahlZwei = hatRechenart = hatErgebnis = false;
- textField.setText("");
- }
- public void eingabeRechenart(char rechenart) {
- if (!hatRechenart) {
- Rechenart = rechenart;
- hatZahlEins = true;
- // aktualisiereText();
- textField.setText(textField.getText() + Rechenart);
- hatRechenart = true;
- }
- }
- public void eingabeZahl(int zahl) {
- if (!hatZahlEins) {
- ZahlEins = ZahlEins * 10 + zahl;
- // hatZahlEins = true;
- } else if (!hatZahlZwei) {
- ZahlZwei = ZahlZwei * 10 + zahl;
- // hatZahlZwei = true;
- }
- // aktualisiereText();
- textField.setText(textField.getText() + zahl);
- }
- /*
- * public void aktualisiereText() {
- *
- * String text = ""; if (hatZahlEins) { text += ZahlEins;
- *
- * } if (hatRechenart) { text += Rechenart; } if (hatZahlZwei) { text +=
- * ZahlZwei;
- *
- * }
- *
- * if (hatErgebnis) { text += " = " + Ergebnis; } textField.setText(text); }
- */
- public void ausrechnen() {
- hatZahlZwei = true;
- switch (Rechenart) {
- case '+':
- Ergebnis = addieren(ZahlEins, ZahlZwei);
- break;
- case '-':
- Ergebnis = subtrahieren(ZahlEins, ZahlZwei);
- break;
- case '*':
- Ergebnis = multiplizieren(ZahlEins, ZahlZwei);
- break;
- case '/':
- Ergebnis = dividieren(ZahlEins, ZahlZwei);
- default:
- // System.out.println("Was ein Scheiß !");
- break;
- }
- hatErgebnis = true;
- textField.setText(textField.getText() + "=" + Ergebnis);
- // aktualisiereText();
- }
- private static int addieren(int eins, int zwei) {
- return eins + zwei;
- }
- private static int subtrahieren(int eins, int zwei) {
- return eins - zwei;
- }
- private static int multiplizieren(int eins, int zwei) {
- return eins * zwei;
- }
- private static int dividieren(int eins, int zwei) {
- return eins / zwei;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement