in jTextField mit Hilfe von Buttons schreiben

Status
Nicht offen für weitere Antworten.

AMDFan

Neues Mitglied
Hallo

Ich habe mal eine Frage, also ich möchte einen Taschenrechner auf Java Basis programmieren. Also ich lerne jetzt Java gerade mal seit Anfang des Jahres 😳 .
Das alles programmier ich mit Hilfe des JavaEditors, da wir den in der Schule auch benutzen.

Ich möchte nun, dass wenn ich zB den Button für die "1" kommt, das klappt wunderbar. Aber wenn ich dann noch weitere Zahlen da hinter schreiben möchte, dann wird die anstatt geschrieben.
Mit dem Editor kann man sich das alles zusammenbauen.

Der Code enthält noch nicht viel, da ich mich bei diesem Problem fest gebissen habe:
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

/**
  *
  * Beschreibung
  *
  * @version 1.0 vom 26.06.2008
  * @Steve
  */

public class Taschenrechner extends JFrame /*implements KeyListener*/ {
 // Anfang Variablen
 /*textField.setHorizontalAlignment(JTextField.RIGHT);
 frame.add(textField);*/
 private JButton jButton1 = new JButton();
 private JButton jButton2 = new JButton();
 private JButton jButton3 = new JButton();
 private JButton jButton4 = new JButton();
 private JButton jButton5 = new JButton();
 private JButton jButton6 = new JButton();
 private JButton jButton7 = new JButton();
 private JButton jButton8 = new JButton();
 private JButton jButton9 = new JButton();
 private JButton jButton10 = new JButton();
 private JButton jButton11 = new JButton();
 private JButton jButton12 = new JButton();
 private JButton jButton13 = new JButton();
 private JButton jButton14 = new JButton();
 private JButton jButton15 = new JButton();
 private JButton jButton16 = new JButton();
 private JProgressBar jProgressBar1 = new JProgressBar();
 private JTextField jTextField1 = new JTextField("",20);
 // Ende Variablen

 public Taschenrechner(String title) {
  // Frame-Initialisierung
  super(title);
  addWindowListener(new WindowAdapter() {
   public void windowClosing(WindowEvent evt) { System.exit(0); }
  });
    int frameWidth = 328;
    int frameHeight = 400;
  setSize(frameWidth, frameHeight);
  Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
  int x = (d.width - getSize().width) / 2;
  int y = (d.height - getSize().height) / 2 ;
  setLocation(x, y);
  Container cp = getContentPane();
  cp.setLayout(null);
  // Anfang Komponenten

  jButton1.setBounds(30, 70, 50, 50);
  jButton1.setFont(new Font("MS Sans Serif", Font.PLAIN, 23));
  jButton1.setText("c");
  cp.add(jButton1);
  jButton1.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent evt) {
    jButton1ActionPerformed(evt);
   }
  });

  jButton2.setBounds(100, 70, 50, 50);
  jButton2.setFont(new Font("MS Sans Serif", Font.PLAIN, 23));
  jButton2.setText(".");
  cp.add(jButton2);
  jButton2.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent evt) {
    jButton2ActionPerformed(evt);
   }
  });

  jButton3.setBounds(170, 70, 50, 50);
  jButton3.setFont(new Font("MS Sans Serif", Font.PLAIN, 23));
  jButton3.setText("=");
  cp.add(jButton3);
  jButton3.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent evt) {
    jButton3ActionPerformed(evt);
   }
  });

  jButton4.setBounds(240, 70, 50, 50);
  jButton4.setFont(new Font("MS Sans Serif", Font.PLAIN, 23));
  jButton4.setText("+");
  cp.add(jButton4);
  jButton4.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent evt) {
    jButton4ActionPerformed(evt);
   }
  });

  jButton5.setBounds(30, 140, 50, 50);
  jButton5.setFont(new Font("MS Sans Serif", Font.PLAIN, 23));
  jButton5.setText("7");
  cp.add(jButton5);
  jButton5.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent evt) {
    jButton5ActionPerformed(evt);
   }
  });

  jButton6.setBounds(100, 140, 50, 50);
  jButton6.setFont(new Font("MS Sans Serif", Font.PLAIN, 23));
  jButton6.setText("8");
  cp.add(jButton6);
  jButton6.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent evt) {
    jButton6ActionPerformed(evt);
   }
  });

  jButton7.setBounds(170, 140, 50, 50);
  jButton7.setFont(new Font("MS Sans Serif", Font.PLAIN, 23));
  jButton7.setText("9");
  cp.add(jButton7);
  jButton7.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent evt) {
    jButton7ActionPerformed(evt);
   }
  });

  jButton8.setBounds(240, 140, 50, 50);
  jButton8.setFont(new Font("MS Sans Serif", Font.PLAIN, 23));
  jButton8.setText("-");
  cp.add(jButton8);
  jButton8.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent evt) {
    jButton8ActionPerformed(evt);
   }
  });

  jButton9.setBounds(30, 210, 50, 50);
  jButton9.setFont(new Font("MS Sans Serif", Font.PLAIN, 23));
  jButton9.setText("4");
  cp.add(jButton9);
  jButton9.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent evt) {
    jButton9ActionPerformed(evt);
   }
  });

  jButton10.setBounds(100, 210, 50, 50);
  jButton10.setFont(new Font("MS Sans Serif", Font.PLAIN, 23));
  jButton10.setText("5");
  cp.add(jButton10);
  jButton10.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent evt) {
    jButton10ActionPerformed(evt);
   }
  });

  jButton11.setBounds(170, 210, 50, 50);
  jButton11.setFont(new Font("MS Sans Serif", Font.PLAIN, 23));
  jButton11.setText("6");
  cp.add(jButton11);
  jButton11.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent evt) {
    jButton11ActionPerformed(evt);
   }
  });

  jButton12.setBounds(240, 210, 50, 50);
  jButton12.setFont(new Font("MS Sans Serif", Font.PLAIN, 23));
  jButton12.setText("x");
  cp.add(jButton12);
  jButton12.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent evt) {
    jButton12ActionPerformed(evt);
   }
  });

  jButton13.setBounds(32, 280, 50, 50);
  jButton13.setFont(new Font("MS Sans Serif", Font.PLAIN, 23));
  jButton13.setText("1");
  cp.add(jButton13);
  jButton13.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent evt) {
    jButton13ActionPerformed(evt);
   }
  });

  jButton14.setBounds(100, 280, 50, 50);
  jButton14.setFont(new Font("MS Sans Serif", Font.PLAIN, 23));
  jButton14.setText("2");
  cp.add(jButton14);
  jButton14.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent evt) {
    jButton14ActionPerformed(evt);
   }
  });

  jButton15.setBounds(170, 280, 50, 50);
  jButton15.setFont(new Font("MS Sans Serif", Font.PLAIN, 23));
  jButton15.setText("3");
  cp.add(jButton15);
  jButton15.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent evt) {
    jButton15ActionPerformed(evt);
   }
  });

  jButton16.setBounds(240, 280, 50, 50);
  jButton16.setFont(new Font("MS Sans Serif", Font.PLAIN, 23));
  jButton16.setText("/");
  cp.add(jButton16);
  jButton16.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent evt) {
    jButton16ActionPerformed(evt);
   }
  });

  jProgressBar1.setBounds(20, 340, 280, 20);
  cp.add(jProgressBar1);
  jTextField1.setBounds(20, 20, 280, 32);
  jTextField1.setBackground(Color.WHITE);
  //jTextField1.setEditable(false);
  jTextField1.setFont(new Font("MS Sans Serif", Font.PLAIN, 18));
  jTextField1.setText("");
  jTextField1.setHorizontalAlignment(JTextField.RIGHT);
  //jTextField1.addActionListener();
  //jTextField1.addKeyListener(this);
  //jTextField1.requestFocus();
  cp.add(jTextField1);
  // Ende Komponenten

  setResizable(false);
  setVisible(true);
 }

 // Anfang Ereignisprozeduren
 public void jButton1ActionPerformed(ActionEvent evt) {
        this.jTextField1.setText("");
 }

 public void jButton2ActionPerformed(ActionEvent evt) {

 }

 public void jButton3ActionPerformed(ActionEvent evt) {

 }

 public void jButton4ActionPerformed(ActionEvent evt) {
        int summandEins, summandZwei;
        int a = Integer.parseInt(this.jTextField1.getText());
 }

 public void jButton5ActionPerformed(ActionEvent evt) {
       int zahlSieben = 7;
       this.jTextField1.setText(zahlSieben+"");
 }

 public void jButton6ActionPerformed(ActionEvent evt) {
       int zahlAcht = 8;
       this.jTextField1.setText(zahlAcht+"");
 }

 public void jButton7ActionPerformed(ActionEvent evt) {
       int zahlNeun = 9;
       this.jTextField1.setText(zahlNeun+"");
 }

 public void jButton8ActionPerformed(ActionEvent evt) {

 }

 public void jButton9ActionPerformed(ActionEvent evt) {
       int zahlVier = 4;
       this.jTextField1.setText(zahlVier+"");
 }

 public void jButton10ActionPerformed(ActionEvent evt) {
       int zahlFuenf = 5;
       this.jTextField1.setText(zahlFuenf+"");
 }

 public void jButton11ActionPerformed(ActionEvent evt) {
       int zahlSechs = 6;
       this.jTextField1.setText(zahlSechs+"");
 }

 public void jButton12ActionPerformed(ActionEvent evt) {

 }

 public void jButton13ActionPerformed(ActionEvent evt) {
       int zahlEins = 1;

 }


 public void jButton14ActionPerformed(ActionEvent evt) {
       int zahlZwei = 2;
       this.jTextField1.setText(zahlZwei+"");
 }

 public void jButton15ActionPerformed(ActionEvent evt) {
       int zahlDrei = 3;
       this.jTextField1.setText(zahlDrei+"");
       for(int i = 0; i > 100; i++){
               try{Thread.sleep(100);}
                 catch (InterruptedException ex) {}
               jProgressBar1.setValue(i);
       }

 }

 public void jButton16ActionPerformed(ActionEvent evt) {

 }

 // Ende Ereignisprozeduren


 public static void main(String[] args) {
  new Taschenrechner("Taschenrechner");
  }
  
 }
}
 
du meist wohl
this.jTextField1.setText(this.jTextField1.geText()+zahlSieben);
statt
> this.jTextField1.setText(zahlSieben+"");

allgemein ist dein Code allerdings ganz schön übel wiederholungslastig,
naja, mit einfachen Kenntnissen wohl nur schwer zu korrigieren
 
Oh Danke für die Antwort, also der großteil des Codes wurde von dem JavaEditor erzeugt und ich kenn mich da leider noch nicht so aus.
 
Status
Nicht offen für weitere Antworten.

Zurück
Oben