package taschenrechner;
public class TaschenRechner extends javax.swing.JFrame {
public double zahl1 = 0;
public int operator = 0;
public int eingabe = 0;
public String s = "";
public TaschenRechner() {
initComponents();
setLocationRelativeTo(null);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jTextField1 = new javax.swing.JTextField();
btnPlus = new javax.swing.JButton();
btnminus = new javax.swing.JButton();
btnGeteilt = new javax.swing.JButton();
btnMal = new javax.swing.JButton();
btnIstGleich = new javax.swing.JButton();
btn7 = new javax.swing.JButton();
btn8 = new javax.swing.JButton();
btn9 = new javax.swing.JButton();
btn4 = new javax.swing.JButton();
btn5 = new javax.swing.JButton();
btn6 = new javax.swing.JButton();
btn1 = new javax.swing.JButton();
btn2 = new javax.swing.JButton();
btn3 = new javax.swing.JButton();
btn0 = new javax.swing.JButton();
btnPlusMinus = new javax.swing.JButton();
btnPunkt = new javax.swing.JButton();
btnReset = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setAlwaysOnTop(true);
setMinimumSize(new java.awt.Dimension(262, 230));
setResizable(false);
btnPlus.setText("+");
btnPlus.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnPlusActionPerformed(evt);
}
});
btnminus.setText("-");
btnminus.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnminusActionPerformed(evt);
}
});
btnGeteilt.setText("/");
btnGeteilt.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnGeteiltActionPerformed(evt);
}
});
btnMal.setText("*");
btnMal.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnMalActionPerformed(evt);
}
});
btnIstGleich.setText("=");
btnIstGleich.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnIstGleichActionPerformed(evt);
}
});
btn7.setText("7");
btn7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn7ActionPerformed(evt);
}
});
btn8.setText("8");
btn8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn8ActionPerformed(evt);
}
});
btn9.setText("9");
btn9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn9ActionPerformed(evt);
}
});
btn4.setText("4");
btn4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn4ActionPerformed(evt);
}
});
btn5.setText("5");
btn5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn5ActionPerformed(evt);
}
});
btn6.setText("6");
btn6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn6ActionPerformed(evt);
}
});
btn1.setText("1");
btn1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn1ActionPerformed(evt);
}
});
btn2.setText("2");
btn2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn2ActionPerformed(evt);
}
});
btn3.setText("3");
btn3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn3ActionPerformed(evt);
}
});
btn0.setText("0");
btn0.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn0ActionPerformed(evt);
}
});
btnPlusMinus.setText("+/-");
btnPlusMinus.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnPlusMinusActionPerformed(evt);
}
});
btnPunkt.setText(".");
btnPunkt.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnPunktActionPerformed(evt);
}
});
btnReset.setText("Reset");
btnReset.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnResetActionPerformed(evt);
}
});
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setText("created by Samuel Machauer");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(btnReset, javax.swing.GroupLayout.DEFAULT_SIZE, 240, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGap(10, 10, 10)
.addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 240, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(btn7, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btn8, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btn9, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnPlus, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(btn4, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btn5, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btn6, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnminus, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(btn1, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btn2, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btn3, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnMal, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(btn0, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnPlusMinus, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnPunkt, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnGeteilt, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(btnIstGleich, javax.swing.GroupLayout.DEFAULT_SIZE, 240, Short.MAX_VALUE)))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 240, Short.MAX_VALUE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(10, 10, 10)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnReset, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btn7, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btn8, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btn9, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnPlus, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btn4, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btn5, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btn6, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnminus, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btn1, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btn2, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btn3, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnMal, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btn0, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnPlusMinus, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnPunkt, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnGeteilt, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(btnIstGleich, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel1)
.addGap(9, 9, 9))
);
pack();
}// </editor-fold>
private void btnPlusActionPerformed(java.awt.event.ActionEvent evt) {
if(eingabe > 0 && operator == 1){
operator = 1;
zahl1 = zahl1 + Double.parseDouble(jTextField1.getText());
jTextField1.setText("");
jTextField1.requestFocus();
}else{
operator = 1;
zahl1 = Double.parseDouble(jTextField1.getText());
jTextField1.setText("");
jTextField1.requestFocus();
eingabe = 1;
}
}
private void btnminusActionPerformed(java.awt.event.ActionEvent evt) {
if(eingabe > 0 && operator == 2){
operator = 2;
zahl1 = zahl1 - Double.parseDouble(jTextField1.getText());
jTextField1.setText("");
jTextField1.requestFocus();
}else{
operator = 2;
zahl1 = Double.parseDouble(jTextField1.getText());
jTextField1.setText("");
jTextField1.requestFocus();
eingabe = 1;
}
}
private void btnGeteiltActionPerformed(java.awt.event.ActionEvent evt) {
if(eingabe > 0 && operator == 3){
operator = 3;
zahl1 = zahl1 / Double.parseDouble(jTextField1.getText());
jTextField1.setText("");
jTextField1.requestFocus();
}else{
operator = 3;
zahl1 = Double.parseDouble(jTextField1.getText());
jTextField1.setText("");
jTextField1.requestFocus();
eingabe = 1;
}
}
private void btnMalActionPerformed(java.awt.event.ActionEvent evt) {
if(eingabe > 0 && operator == 4){
operator = 4;
zahl1 = zahl1 * Double.parseDouble(jTextField1.getText());
jTextField1.setText("");
jTextField1.requestFocus();
}else{
operator = 4;
zahl1 = Double.parseDouble(jTextField1.getText());
jTextField1.setText("");
jTextField1.requestFocus();
eingabe = 1;
}
}
private void btnIstGleichActionPerformed(java.awt.event.ActionEvent evt) {
//IstGleich
berechnen();
jTextField1.setText(String.valueOf(zahl1));
jTextField1.requestFocus();
}
private void btn7ActionPerformed(java.awt.event.ActionEvent evt) {
s = jTextField1.getText();
jTextField1.setText(s+"7");
}
private void btn8ActionPerformed(java.awt.event.ActionEvent evt) {
s = jTextField1.getText();
jTextField1.setText(s+"8");
}
private void btn9ActionPerformed(java.awt.event.ActionEvent evt) {
s = jTextField1.getText();
jTextField1.setText(s+"9");
}
private void btn4ActionPerformed(java.awt.event.ActionEvent evt) {
s = jTextField1.getText();
jTextField1.setText(s+"4");
}
private void btn5ActionPerformed(java.awt.event.ActionEvent evt) {
s = jTextField1.getText();
jTextField1.setText(s+"5");
}
private void btn6ActionPerformed(java.awt.event.ActionEvent evt) {
s = jTextField1.getText();
jTextField1.setText(s+"6");
}
private void btn1ActionPerformed(java.awt.event.ActionEvent evt) {
s = jTextField1.getText();
jTextField1.setText(s+"1");
}
private void btn2ActionPerformed(java.awt.event.ActionEvent evt) {
s = jTextField1.getText();
jTextField1.setText(s+"2");
}
private void btn3ActionPerformed(java.awt.event.ActionEvent evt) {
s = jTextField1.getText();
jTextField1.setText(s+"3");
}
private void btn0ActionPerformed(java.awt.event.ActionEvent evt) {
s = jTextField1.getText();
jTextField1.setText(s+"0");
}
private void btnPlusMinusActionPerformed(java.awt.event.ActionEvent evt) {
s = jTextField1.getText();
if(s.charAt(0)== '-'){
zahl1 = Double.parseDouble(jTextField1.getText())*-1;
jTextField1.setText(String.valueOf(zahl1));
}else{
zahl1 = Double.parseDouble(jTextField1.getText())*-1;
jTextField1.setText(String.valueOf(zahl1));
}
}
private void btnPunktActionPerformed(java.awt.event.ActionEvent evt) {
s = jTextField1.getText();
jTextField1.setText(s+".");
}
private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {
zahl1 = 0;
operator = 0;
eingabe = 0;
String s = "";
jTextField1.setText(s);
jTextField1.requestFocus();
}
public void berechnen(){
if(operator == 1){
zahl1 = zahl1 + Double.parseDouble(jTextField1.getText());
}else if(operator == 2){
zahl1 = zahl1 - Double.parseDouble(jTextField1.getText());
}else if(operator == 3){
zahl1 = zahl1 / Double.parseDouble(jTextField1.getText());
}else if(operator == 4){
zahl1 = zahl1 * Double.parseDouble(jTextField1.getText());
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TaschenRechner().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btn0;
private javax.swing.JButton btn1;
private javax.swing.JButton btn2;
private javax.swing.JButton btn3;
private javax.swing.JButton btn4;
private javax.swing.JButton btn5;
private javax.swing.JButton btn6;
private javax.swing.JButton btn7;
private javax.swing.JButton btn8;
private javax.swing.JButton btn9;
private javax.swing.JButton btnGeteilt;
private javax.swing.JButton btnIstGleich;
private javax.swing.JButton btnMal;
private javax.swing.JButton btnPlus;
private javax.swing.JButton btnPlusMinus;
private javax.swing.JButton btnPunkt;
private javax.swing.JButton btnReset;
private javax.swing.JButton btnminus;
private javax.swing.JLabel jLabel1;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}