# Java Taschenrechner - JFrame



## beezie (11. Feb 2009)

Hallo,

bin grad dabei einen Taschenrechner zu programmieren, habe aber noch ein paar Probleme.
Mir fehlen noch die Buttons * und /. und ich weiß nicht so wirklich wie ich anfangen soll, 
Sonst geht es eigentlich soweit außer ein paar Kleinigkeiten,
Hoffe es kann mir jemand helfen.
vielen dank im voraus, liebe Grüße


Code:

```
/*
 * Calculator.java
 *
 * Created on 28. Januar 2009, 11:35
 */



/**
 *
 * @author  p180400
 */
public class Calculator extends javax.swing.JFrame {

  
    /** Creates new form Calculator */
    public Calculator() {
        initComponents();
    }

 

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  private void initComponents() {

    jPanel1 = new javax.swing.JPanel();
    button1 = new javax.swing.JButton();
    button4 = new javax.swing.JButton();
    button7 = new javax.swing.JButton();
    button2 = new javax.swing.JButton();
    button5 = new javax.swing.JButton();
    button8 = new javax.swing.JButton();
    button3 = new javax.swing.JButton();
    button6 = new javax.swing.JButton();
    button9 = new javax.swing.JButton();
    button0 = new javax.swing.JButton();
    buttonDivision = new javax.swing.JButton();
    buttonPlus = new javax.swing.JButton();
    buttonMinus = new javax.swing.JButton();
    buttonMultiplikation = new javax.swing.JButton();
    buttonGleich = new javax.swing.JButton();
    buttonClear = new javax.swing.JButton();
    buttonKomma = new javax.swing.JButton();
    buttonPlusMinus = new javax.swing.JButton();
    buttonCA = new javax.swing.JButton();
    jScrollPane1 = new javax.swing.JScrollPane();
    jTextPane1 = new javax.swing.JTextPane();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    button1.setText("1");
    button1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        button1ActionPerformed(evt);
      }
    });

    button4.setText("4");
    button4.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        button4ActionPerformed(evt);
      }
    });

    button7.setText("7");
    button7.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        button7ActionPerformed(evt);
      }
    });

    button2.setText("2");
    button2.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        button2ActionPerformed(evt);
      }
    });

    button5.setText("5");
    button5.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        button5ActionPerformed(evt);
      }
    });

    button8.setText("8");
    button8.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        button8ActionPerformed(evt);
      }
    });

    button3.setText("3");
    button3.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        button3ActionPerformed(evt);
      }
    });

    button6.setText("6");
    button6.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        button6ActionPerformed(evt);
      }
    });

    button9.setText("9");
    button9.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        button9ActionPerformed(evt);
      }
    });

    button0.setText("0");
    button0.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        button0ActionPerformed(evt);
      }
    });

    buttonDivision.setText("/");
    buttonDivision.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        buttonDivisionActionPerformed(evt);
      }
    });

    buttonPlus.setText("+");
    buttonPlus.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        buttonPlusActionPerformed(evt);
      }
    });

    buttonMinus.setText("-");
    buttonMinus.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        buttonMinusActionPerformed(evt);
      }
    });

    buttonMultiplikation.setText("*");
    buttonMultiplikation.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        buttonMultiplikationActionPerformed(evt);
      }
    });

    buttonGleich.setText("=");

    buttonClear.setText("C");
    buttonClear.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        buttonClearActionPerformed(evt);
      }
    });

    buttonKomma.setText(",");
    buttonKomma.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        buttonKommaActionPerformed(evt);
      }
    });

    buttonPlusMinus.setText("+ / -");
    buttonPlusMinus.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        buttonPlusMinusActionPerformed(evt);
      }
    });

    buttonCA.setText("CA");
    buttonCA.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        buttonCAActionPerformed(evt);
      }
    });

    org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
      jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(jPanel1Layout.createSequentialGroup()
        .addContainerGap()
        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
          .add(jPanel1Layout.createSequentialGroup()
            .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
              .add(button9)
              .add(button6)
              .add(button3))
            .add(6, 6, 6)
            .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
              .add(jPanel1Layout.createSequentialGroup()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                  .add(button8)
                  .add(button2)
                  .add(jPanel1Layout.createSequentialGroup()
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(buttonKomma)))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                  .add(button7)
                  .add(button1)
                  .add(buttonGleich)))
              .add(jPanel1Layout.createSequentialGroup()
                .add(button5)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(button4))))
          .add(jPanel1Layout.createSequentialGroup()
            .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
              .add(buttonClear, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
              .add(button0, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
            .add(buttonCA)))
        .add(38, 38, 38)
        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
          .add(buttonPlus)
          .add(buttonMinus)
          .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(buttonDivision)
            .add(buttonMultiplikation))
          .add(buttonPlusMinus))
        .add(28, 28, 28))
    );
    jPanel1Layout.setVerticalGroup(
      jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(jPanel1Layout.createSequentialGroup()
        .addContainerGap(24, Short.MAX_VALUE)
        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
          .add(button9)
          .add(button8)
          .add(button7)
          .add(buttonDivision))
        .add(18, 18, 18)
        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
          .add(button6)
          .add(button5)
          .add(button4)
          .add(buttonMultiplikation))
        .add(19, 19, 19)
        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
          .add(button3)
          .add(button2)
          .add(button1)
          .add(buttonMinus))
        .add(18, 18, 18)
        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
          .add(button0)
          .add(buttonKomma)
          .add(buttonPlus)
          .add(buttonGleich))
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
          .add(buttonClear)
          .add(buttonCA)
          .add(buttonPlusMinus))
        .add(40, 40, 40))
    );

    jScrollPane1.setViewportView(jTextPane1);

    org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(layout.createSequentialGroup()
        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
          .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
          .add(layout.createSequentialGroup()
            .add(27, 27, 27)
            .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 214, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
        .addContainerGap())
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(layout.createSequentialGroup()
        .addContainerGap()
        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 38, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
        .add(11, 11, 11)
        .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );

    pack();
  }// </editor-fold>                        

private void buttonClearActionPerformed(java.awt.event.ActionEvent evt) {                                            
  String input = jTextPane1.getText();
  jTextPane1.setText(input.substring(0, input.length() - 1));
}                                           

private void button9ActionPerformed(java.awt.event.ActionEvent evt) {                                        
jTextPane1.setText(jTextPane1.getText() + "9");
}                                       

private void button1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  jTextPane1.setText(jTextPane1.getText() + "1");
}                                       

private void button2ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  jTextPane1.setText(jTextPane1.getText() + "2");
}                                       

private void button8ActionPerformed(java.awt.event.ActionEvent evt) {                                        
jTextPane1.setText(jTextPane1.getText() + "8");
}                                       

private void button7ActionPerformed(java.awt.event.ActionEvent evt) {                                        
jTextPane1.setText(jTextPane1.getText() + "7");
}                                       

private void button6ActionPerformed(java.awt.event.ActionEvent evt) {                                        
jTextPane1.setText(jTextPane1.getText() + "6");
}                                       

private void button5ActionPerformed(java.awt.event.ActionEvent evt) {                                        
jTextPane1.setText(jTextPane1.getText() + "5");
}                                       

private void button4ActionPerformed(java.awt.event.ActionEvent evt) {                                        
jTextPane1.setText(jTextPane1.getText() + "4");
}                                       

private void button3ActionPerformed(java.awt.event.ActionEvent evt) {                                        
jTextPane1.setText(jTextPane1.getText() + "3");
}                                       

private void button0ActionPerformed(java.awt.event.ActionEvent evt) {                                        
jTextPane1.setText(jTextPane1.getText() + "0");
}                                       

private void buttonCAActionPerformed(java.awt.event.ActionEvent evt) {                                         
  jTextPane1.setText("");
}                                        

private void buttonKommaActionPerformed(java.awt.event.ActionEvent evt) {                                            
  String input = jTextPane1.getText();
  if (input.indexOf(',') >= 0)
    return;
  jTextPane1.setText(input + ","); 
}                                           

private void buttonPlusActionPerformed(java.awt.event.ActionEvent evt) {                                           
  jTextPane1.setText(jTextPane1.getText() + jTextPane1.getText());
}                                          

private void buttonMinusActionPerformed(java.awt.event.ActionEvent evt) {                                            
 // jTextPane1.setText(jTextPane1.getText() - jTextPane1.getText());
}                                           

private void buttonMultiplikationActionPerformed(java.awt.event.ActionEvent evt) {                                                     

  
}                                                    

private void buttonDivisionActionPerformed(java.awt.event.ActionEvent evt) {                                               
  //jTextPane1.setText(jTextPane1.getText() / jTextPane1.getText());
}                                              

private void buttonPlusMinusActionPerformed(java.awt.event.ActionEvent evt) {                                                
// TODO add your handling code here:
}                                               

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Calculator().setVisible(true);
            }
        });
    }

  // Variables declaration - do not modify                     
  private javax.swing.JButton button0;
  private javax.swing.JButton button1;
  private javax.swing.JButton button2;
  private javax.swing.JButton button3;
  private javax.swing.JButton button4;
  private javax.swing.JButton button5;
  private javax.swing.JButton button6;
  private javax.swing.JButton button7;
  private javax.swing.JButton button8;
  private javax.swing.JButton button9;
  private javax.swing.JButton buttonCA;
  private javax.swing.JButton buttonClear;
  private javax.swing.JButton buttonDivision;
  private javax.swing.JButton buttonGleich;
  private javax.swing.JButton buttonKomma;
  private javax.swing.JButton buttonMinus;
  private javax.swing.JButton buttonMultiplikation;
  private javax.swing.JButton buttonPlus;
  private javax.swing.JButton buttonPlusMinus;
  private javax.swing.JPanel jPanel1;
  private javax.swing.JScrollPane jScrollPane1;
  private javax.swing.JTextPane jTextPane1;
  // End of variables declaration                   

}
```


----------



## Guest (11. Feb 2009)

Ich weiß, hilft dir nicht direkt bei deinem Problem, aber guck dir mal den Code an um zu sehen wie man sich solche extreme Tipparbeit wie du mit deinen 9 einzelnen Ziffer-Buttons erspart:

```
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class Rechner{

	public static void main(String[] args){new Rechner();}

	private JFrame window = new JFrame();
	private JPanel panel = new JPanel();
	private JTextField textField = new JTextField();

	public Rechner(){
		window.setLocation(100,100);
		window.add(panel);

		GridBagLayout layout;
		panel.setLayout(layout = new GridBagLayout());

		JButton[] zifferButtons = new JButton[9];
		for(int i=0;i<zifferButtons.length;++i){
			zifferButtons[i] = new JButton(""+(i+1));
			zifferButtons[i].setActionCommand(""+(i+1));
			zifferButtons[i].addActionListener(zifferActionListener);

			layout.setConstraints(zifferButtons[i], new GridBagConstraints(i%3, i/3, 1, 1, 50, 50, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(2,2,2,2), 0, 0));
			panel.add(zifferButtons[i]);
		}

		layout.setConstraints(textField, new GridBagConstraints(0, 3, 3, 1, 50, 10, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0), 0, 0));
		panel.add(textField);


		window.pack();
		window.setVisible(true);
	}

	private ActionListener zifferActionListener = new ActionListener(){
		public void actionPerformed(ActionEvent event){
			textField.setText(textField.getText()+((JButton)event.getSource()).getActionCommand());
		}
	};
}
```


----------



## beezie (11. Feb 2009)

ja da hast du Recht, ist viel weniger.
Ich bin noch Anfänger und hab mir gedacht mach es erstmal ausführlich, dann kann ich immer noch abkürzen.

Danke trotzdem 

lg


----------



## Schandro (11. Feb 2009)

> Ich bin noch Anfänger und hab mir gedacht mach es erstmal ausführlich, dann kann ich immer noch abkürzen.


Das gilt imho für andere Sachen, für Programmieren aber nicht. (Jedenfalls nicht bei solchen Sachen). Der Code von mir (ich werd in diesem Board immer wieder automatisch aus- und eingeloggt....  :applaus: )  ist nämlich nicht nur kürzer, sondern auch viel leichter zu warten, irgendwelche Tippfehler zu suchen geht viel schneller und vorallem: Er ist dynamischer. Ich bräuchte nur eine Zahl im Code ändern (die Länge des Arrays..) und schon wär da ein funktionierender "10er" Button. Deswegen ist es imho sinnlos, sich erst die riesen Tipparbeit auf sich zu nehmen, wenn es nur schadet


----------



## Leroy42 (11. Feb 2009)

beezie hat gesagt.:
			
		

> Ich bin noch Anfänger ...



... und dann kriegst du ein derart komplexes Layout hin?  :shock: 



			
				beezie hat gesagt.:
			
		

> ```
> org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
> jPanel1.setLayout(jPanel1Layout);
> jPanel1Layout.setHorizontalGroup(
> ...



Woow! Ich verstand damals unter dem Begriff _Anfänger_ etwas völlig anderes!


----------



## Antoras (11. Feb 2009)

Leroy42 hat gesagt.:
			
		

> beezie hat gesagt.:
> 
> 
> 
> ...


Also, ich würde jetzt mal schwer auf GUI-Builder tippen. Das komplette Layout ist so umständlich - ich bezweifle, dass das jemand freiwillig von Hand schreibt.

@beezie
Zu deinem Problem: Du solltest dir erst mal überlegen was du genau erreichen möchtest. 
Ich würde dir empfehlen hier erst mal klein anzufangen. Als erstes nur mit 2, 3 Zahlen rechnen, dann nach und nach alles wichtige implementieren und erst zum Schluss erstellst einen "richtigen" Taschenrechner. Sonst bekommst einen Overkill und blickst am Schluss überhaupt nichts mehr.


----------

