hallo ich hab folgendes problem und zwar hab ich mir nen taschenrechner programmiert und will nun beim ergebniss(per druck auf den = button) das ergebniss anzeigen lassen. aber wie kann ich den string des textfensters indem die zahlen stehen nach den zeichen +,-,*,/ durchsuchen und je nachdem welches gefunden wurde das ergebniss berechnen?
package taschenrechnermittasten;
import java.io.IOException;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.String.*;
import java.lang.*;
import java.util.*;
public class Main extends JFrame
                  implements ActionListener{    
  JButton addition = new JButton("+");
  JButton subtraktion = new JButton("-");
  JButton multiplikation = new JButton("*");
  JButton division = new JButton("/"); 
  JButton a = new JButton("1");
  JButton b = new JButton("2");
  JButton c = new JButton("3");
  JButton d = new JButton("4");
  JButton e = new JButton("5");
  JButton f = new JButton("6");
  JButton g = new JButton("7");
  JButton h = new JButton("8");
  JButton i = new JButton("9");
  JButton j = new JButton("0");
  JButton k = new JButton(",");
  JButton l = new JButton("=");
  JTextField textFeld1 = new JTextField(20);
  JTextField textFeld2 = new JTextField(20); 
  JTextField textFeld3 = new JTextField(20); 
  public Main() {
 super("Taschenrechner");
 setBackground(Color.GRAY);
 setBackground(Color.BLACK);
 setBackground(Color.WHITE);
 JPanel contentPane = new JPanel();
 contentPane.add(addition);
 contentPane.add(subtraktion);
 contentPane.add(multiplikation);
 contentPane.add(division); 
 contentPane.add(a);
 contentPane.add(b);
 contentPane.add(c);
 contentPane.add(d);
 contentPane.add(e);
 contentPane.add(f);
 contentPane.add(g);
 contentPane.add(h);
 contentPane.add(i);
 contentPane.add(j);
 contentPane.add(k);
 contentPane.add(l);
 contentPane.add(textFeld1); 
 contentPane.add(textFeld2); 
 contentPane.add(textFeld3);
 addition.addActionListener(this);
 subtraktion.addActionListener(this);
 multiplikation.addActionListener(this);
 division.addActionListener(this);
 a.addActionListener(this);
 b.addActionListener(this);
 c.addActionListener(this);
 d.addActionListener(this);
 e.addActionListener(this);
 f.addActionListener(this);
 g.addActionListener(this);
 h.addActionListener(this);
 i.addActionListener(this);
 j.addActionListener(this);
 k.addActionListener(this);
 l.addActionListener(this);
 setContentPane(contentPane);
 contentPane.setBackground(Color.BLACK);
 addition.setBackground(Color.GRAY);
 subtraktion.setBackground(Color.GRAY);
 multiplikation.setBackground(Color.GRAY);
 division.setBackground(Color.GRAY);
 a.setBackground(Color.GRAY);
 b.setBackground(Color.GRAY);
 c.setBackground(Color.GRAY);
 d.setBackground(Color.GRAY);
 e.setBackground(Color.GRAY);
 f.setBackground(Color.GRAY);
 g.setBackground(Color.GRAY);
 h.setBackground(Color.GRAY);
 i.setBackground(Color.GRAY);
 j.setBackground(Color.GRAY);
 k.setBackground(Color.GRAY);
 l.setBackground(Color.GRAY);
 textFeld1.setBackground(Color.WHITE);
 textFeld2.setBackground(Color.WHITE);
 textFeld3.setBackground(Color.WHITE);
 setLayout(new GridLayout(5,5,5,5));
  }            
  public void actionPerformed(ActionEvent ae){
      Object eventQuelle = ae.getSource();
        
      if (eventQuelle == addition)
      {
         String text;
         text = textFeld1.getText();
         textFeld1.setText(textFeld1.getText());
         textFeld1.setText(text+"+");         
      }
      if (eventQuelle == subtraktion)
      {   
         String text;
         text = textFeld1.getText();
         textFeld1.setText(textFeld1.getText());
         textFeld1.setText(text+"-");    
      }
      if (eventQuelle == multiplikation)
      {   
         String text;
         text = textFeld1.getText();
         textFeld1.setText(textFeld1.getText());
         textFeld1.setText(text+"*");    
      } 
        if (eventQuelle == division)
      {   
         String text;
         text = textFeld1.getText();
         textFeld1.setText(textFeld1.getText());
         textFeld1.setText(text+"/");                
      }
       if (eventQuelle == a)
      {
         String text;
         text = textFeld1.getText();
         textFeld1.setText(textFeld1.getText());
         textFeld1.setText(text+"1");
      }
      if (eventQuelle == b)
      {
         String text;
         text = textFeld1.getText();
         textFeld1.setText(textFeld1.getText());
         textFeld1.setText(text+"2");    
      }
      if (eventQuelle == c)
      {
         String text;
         text = textFeld1.getText();
         textFeld1.setText(textFeld1.getText());
         textFeld1.setText(text+"3");   
      }
      if (eventQuelle == d)
      {      
         String text;
         text = textFeld1.getText();
         textFeld1.setText(textFeld1.getText());
         textFeld1.setText(text+"4");        
      }
      if (eventQuelle == e)
      {
         String text;
         text = textFeld1.getText();
         textFeld1.setText(textFeld1.getText());
         textFeld1.setText(text+"5");        
      }
      if (eventQuelle == f)
      {
         String text;
         text = textFeld1.getText();
         textFeld1.setText(textFeld1.getText());
         textFeld1.setText(text+"6");
      }
      if (eventQuelle == g)
      {        
         String text;
         text = textFeld1.getText();
         textFeld1.setText(textFeld1.getText());
         textFeld1.setText(text+"7");
      }
      if (eventQuelle == h)
      {       
         String text;
         text = textFeld1.getText();
         textFeld1.setText(textFeld1.getText());
         textFeld1.setText(text+"8");
      }
      if (eventQuelle == i)
      {        
         String text;
         text = textFeld1.getText();
         textFeld1.setText(textFeld1.getText());
         textFeld1.setText(text+"9");
      }
      if (eventQuelle == j)
      {       
         String text;
         text = textFeld1.getText();
         textFeld1.setText(textFeld1.getText());
         textFeld1.setText(text+"0" );
      }
      if (eventQuelle == k)
      {       
         String text;
         text = textFeld1.getText();
         textFeld1.setText(textFeld1.getText());
         textFeld1.setText(text+"."); 
      }
      if (eventQuelle == l)
      {
         
      }      
 }    
    public static void main(String[] args) throws IOException{
   Frame rahmen = new Main();
   WindowListener listener = new WindowAdapter()
   {
       public void windowClosing(WindowEvent we)
       {
           System.exit(0);
       }
   };
   rahmen.addWindowListener(listener);
   rahmen.setSize(250,350);   
   rahmen.setVisible(true);
   
 }           
} 
lol ich weiß er is zu kompliziert gemacht und zu lang.. egal weiß jemand Hilfe?thx im vorraus(ach ja das ergebniss rechnen soll bei if(eventQuelle == l) eingefügt werden)
 
Hering98765 Gast  | 



