package Taschenrechner;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class GUI_Haupt extends JFrame {
private JPanel contentPane;
private JTextField txtZahl1;
private JTextField txtZahl2;
private JTextField txtErgebnis;
private JButton btnSubtrahiere;
private JButton btnMultipliziere;
private JButton btnDividiere;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GUI_Haupt frame = new GUI_Haupt();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public GUI_Haupt() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnAddiere = new JButton("Addiere");
btnAddiere.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int zahl1=0;
int zahl2=0;
int erg =0;
try
{zahl1=Integer.parseInt(txtZahl1.getText());
} catch (Exception e2) {
JOptionPane.showMessageDialog(null, "Zahl 1 war keine Zahl", "ERROR_", JOptionPane.ERROR_MESSAGE);// TODO: handle exception
}
try
{zahl2=Integer.parseInt(txtZahl2.getText());
} catch (Exception e2) {
JOptionPane.showMessageDialog(null, "Zahl 2 war keine Zahl", "ERROR_", JOptionPane.ERROR_MESSAGE);// TODO: handle exception
}
erg=zahl1+zahl2;
txtErgebnis.setText("Ergebnis: " + erg);
}
});
btnAddiere.setBounds(10, 68, 89, 23);
contentPane.add(btnAddiere);
txtZahl1 = new JTextField();
txtZahl1.setBounds(10, 28, 86, 20);
contentPane.add(txtZahl1);
txtZahl1.setColumns(10);
txtZahl2 = new JTextField();
txtZahl2.setBounds(102, 28, 86, 20);
contentPane.add(txtZahl2);
txtZahl2.setColumns(10);
txtErgebnis = new JTextField();
txtErgebnis.setBounds(221, 28, 156, 20);
contentPane.add(txtErgebnis);
txtErgebnis.setColumns(10);
btnSubtrahiere = new JButton("Subtrahiere");
btnSubtrahiere.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int zahl1=0;
int zahl2=0;
int erg =0;
try
{zahl1=Integer.parseInt(txtZahl1.getText());
} catch (Exception e2) {
JOptionPane.showMessageDialog(null, "Zahl 1 war keine Zahl", "ERROR_", JOptionPane.ERROR_MESSAGE);// TODO: handle exception
}
try
{zahl2=Integer.parseInt(txtZahl2.getText());
} catch (Exception e2) {
JOptionPane.showMessageDialog(null, "Zahl 2 war keine Zahl", "ERROR_", JOptionPane.ERROR_MESSAGE);// TODO: handle exception
}
erg=zahl1-zahl2;
txtErgebnis.setText("Ergebnis: " + erg);
}
});
btnSubtrahiere.setBounds(123, 68, 89, 23);
contentPane.add(btnSubtrahiere);
btnMultipliziere = new JButton("Multipliziere");
btnMultipliziere.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int zahl1=0;
int zahl2=0;
int erg =0;
try
{zahl1=Integer.parseInt(txtZahl1.getText());
} catch (Exception e2) {
JOptionPane.showMessageDialog(null, "Zahl 1 war keine Zahl", "ERROR_", JOptionPane.ERROR_MESSAGE);// TODO: handle exception
}
try
{zahl2=Integer.parseInt(txtZahl2.getText());
} catch (Exception e2) {
JOptionPane.showMessageDialog(null, "Zahl 2 war keine Zahl", "ERROR_", JOptionPane.ERROR_MESSAGE);// TODO: handle exception
}
erg=zahl1*zahl2;
txtErgebnis.setText("Ergebnis: " + erg);
}
});
btnMultipliziere.setBounds(10, 115, 89, 23);
contentPane.add(btnMultipliziere);
btnDividiere = new JButton("Dividiere");
btnDividiere.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
double zahl1=0;
double zahl2=0;
double erg =0;
try
{zahl1=Double.parseDouble(txtZahl1.getText());
} catch (Exception e2) {
JOptionPane.showMessageDialog(null, "Zahl 1 war keine Zahl", "ERROR_", JOptionPane.ERROR_MESSAGE);// TODO: handle exception
}
try
{zahl2=Double.parseDouble(txtZahl2.getText());
} catch (Exception e2) {
JOptionPane.showMessageDialog(null, "Zahl 2 war keine Zahl", "ERROR_", JOptionPane.ERROR_MESSAGE);// TODO: handle exception
}
try
{
erg=zahl1/zahl2;
} catch (Exception e2) {
JOptionPane.showMessageDialog(null, "Zahl 2 darf nicht null sein", "ERROR_", JOptionPane.ERROR_MESSAGE);// TODO: handle exception
}
txtErgebnis.setText("Ergebnis: " + erg);
}
});
btnDividiere.setBounds(123, 115, 89, 23);
contentPane.add(btnDividiere);
}
}