Einfacher Taschenrechner + - * /

barosch

Grünschnabel
Hallo erstmal,
ich schreib in zwei tagen ne info klausur und da wird wahrschienlich ein taschenrechner programmiert werden als letzte aufgabe.
Wir sind erst seit nem halben jahr mit java beschäftigt...
Die Oberfläche besteht, aber ich weiß nicht wie ich mit dem actionListener
umgehen soll, wie schaff ich es das er addiert subrahier ohne das ich die zahlen von hand eingebe?

hier der code bis jetzt!
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Rechner extends JFrame implements ActionListener
{
    private JLabel rechner;
    private JButton z0, z1, z2, z3, z4, z5, z6, z7, z8, z9, za, zs, zm, zd, ze;
    private JTextField ausgabe;
    private JPanel tastenPanel, anzeigePanel ;
    
    public Rechner()
    {
        super("X Taschenrechner X");
        setSize(300,300);
        tastenPanel = new JPanel();
        anzeigePanel = new JPanel();
        
        getContentPane().add("South", tastenPanel); 
        tastenPanel.setLayout(new GridLayout(5,1));
        
        getContentPane().add("North", anzeigePanel);
        anzeigePanel.setLayout(new GridLayout(1,1));
        
        ausgabe = new JTextField(20);
        anzeigePanel.add(ausgabe);
        
        z0 = new JButton("0");
        z0.addActionListener(this);
        tastenPanel.add(z0);
        
        z1 = new JButton("1");
        z1.addActionListener(this);
        tastenPanel.add(z1);
        
        z2 = new JButton("2");
        z2.addActionListener(this);
        tastenPanel.add(z2);
        
        z3 = new JButton("3");
        z3.addActionListener(this);
        tastenPanel.add(z3);
        
        z4 = new JButton("4");
        z4.addActionListener(this);
        tastenPanel.add(z4);
        
        z5 = new JButton("5");
        z5.addActionListener(this);
        tastenPanel.add(z5);
        
        z6 = new JButton("6");
        z6.addActionListener(this);
        tastenPanel.add(z6);
        
        z7 = new JButton("7");
        z7.addActionListener(this);
        tastenPanel.add(z7);
        
        z8 = new JButton("8");
        z8.addActionListener(this);
        tastenPanel.add(z8);
        
        z9 = new JButton("9");
        z9.addActionListener(this);
        tastenPanel.add(z9);
        
        za = new JButton("+");
        za.addActionListener(this);
        tastenPanel.add(za);
        
        zs = new JButton("-");
        zs.addActionListener(this);
        tastenPanel.add(zs);
        
        zm = new JButton("*");
        zm.addActionListener(this);
        tastenPanel.add(zm);
        
        zd = new JButton("/");
        zd.addActionListener(this);
        tastenPanel.add(zd);
        
        ze = new JButton("=");
        ze.addActionListener(this);
        tastenPanel.add(ze);
        
        setVisible(true);
    }

    public void actionPerformed(ActionEvent e)
    {
       e.getActionCommand();    
    }

habt ihr tipps für mich oder sonstiges

Danke schon mal
 
Java:
	public Rechner() {
		....
		....
		z0 = new JButton("0");
		z0.addActionListener(this);
		z0.setActionCommand("z0");
		tastenPanel.add(z0);
		...
		...
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("z0")){
			ausgabe.setText("0");
		}else if(e.getActionCommand().equals("z1")){
			// ....
		}
	}
 
Zurück