Hab heute mit Java Programmierung angefangen und hab einiges im Internet gelesen und habe soweit das die Tasten meines Taschenrechners alle funktionieren, nur weiß ich nicht wie ich machen kann, dass das Programm die Eingabe im Textfeld als Rechnung sieht wenn ich auf den Ergebnis Button drücke.
Was muss ich machen ?
Danke schonmal für die Antworten.
P.Srogrammiere mit Netbeans.
Was muss ich machen ?
Danke schonmal für die Antworten.
P.Srogrammiere mit Netbeans.
Java:
package taschenrechner.mit.oberfläsche;
public class Taschenrechner extends javax.swing.JFrame {
private StringBuilder builder;
public Taschenrechner() {
initComponents();
builder = new StringBuilder();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton0 = new javax.swing.JButton();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
jButton7 = new javax.swing.JButton();
jButton8 = new javax.swing.JButton();
jButton9 = new javax.swing.JButton();
jButtonPunkt = new javax.swing.JButton();
jButtonGleich = new javax.swing.JButton();
jButtonKlammerAuf = new javax.swing.JButton();
jButtonKlammerZu = new javax.swing.JButton();
jButtonPlus = new javax.swing.JButton();
jButtonMinus = new javax.swing.JButton();
jButtonClear = new javax.swing.JButton();
jButtonGeteilt = new javax.swing.JButton();
jButtonMal = new javax.swing.JButton();
textfeld = new javax.swing.JTextField();
jButton5 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton0.setText("0");
jButton0.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton0ActionPerformed(evt);
}
});
jButton1.setText("1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("2");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton3.setText("3");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jButton4.setText("4");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
jButton6.setText("6");
jButton6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton6ActionPerformed(evt);
}
});
jButton7.setText("7");
jButton7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton7ActionPerformed(evt);
}
});
jButton8.setText("8");
jButton8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton8ActionPerformed(evt);
}
});
jButton9.setText("9");
jButton9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton9ActionPerformed(evt);
}
});
jButtonPunkt.setText(".");
jButtonPunkt.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonPunktActionPerformed(evt);
}
});
jButtonGleich.setText("=");
jButtonGleich.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonGleichActionPerformed(evt);
}
});
jButtonKlammerAuf.setText("(");
jButtonKlammerAuf.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonKlammerAufActionPerformed(evt);
}
});
jButtonKlammerZu.setText(")");
jButtonKlammerZu.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonKlammerZuActionPerformed(evt);
}
});
jButtonPlus.setText("+");
jButtonPlus.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonPlusActionPerformed(evt);
}
});
jButtonMinus.setText("-");
jButtonMinus.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonMinusActionPerformed(evt);
}
});
jButtonClear.setText("C");
jButtonClear.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonClearActionPerformed(evt);
}
});
jButtonGeteilt.setText("/");
jButtonGeteilt.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonGeteiltActionPerformed(evt);
}
});
jButtonMal.setText("*");
jButtonMal.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonMalActionPerformed(evt);
}
});
jButton5.setText("5");
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(textfeld)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jButton7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton8)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton9))
.addGroup(layout.createSequentialGroup()
.addComponent(jButton5)
.addGap(12, 12, 12)
.addComponent(jButton6))))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jButtonClear)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButtonGeteilt, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButtonMal))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton1)
.addComponent(jButton0))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButtonPunkt, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton3)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButtonGleich)
.addComponent(jButtonPlus)
.addComponent(jButtonKlammerAuf, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButtonKlammerZu, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButtonMinus, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(32, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(textfeld, javax.swing.GroupLayout.DEFAULT_SIZE, 66, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButtonMal)
.addComponent(jButtonGeteilt)
.addComponent(jButtonClear)
.addComponent(jButtonMinus))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton7)
.addComponent(jButton8)
.addComponent(jButton9)
.addComponent(jButtonPlus))
.addGap(4, 4, 4)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton6)
.addComponent(jButton5)
.addComponent(jButton4)
.addComponent(jButtonKlammerAuf))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2)
.addComponent(jButton3)
.addComponent(jButtonKlammerZu))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton0)
.addComponent(jButtonPunkt)
.addComponent(jButtonGleich))
.addContainerGap())
);
pack();
}// </editor-fold>
private void jButton0ActionPerformed(java.awt.event.ActionEvent evt) {
builder.append("0");
textfeld.setText(builder.toString());
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
builder.append("1");
textfeld.setText(builder.toString());
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
builder.append("2");
textfeld.setText(builder.toString());
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
builder.append("3");
textfeld.setText(builder.toString());
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
builder.append("4");
textfeld.setText(builder.toString());
}
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
builder.append("5");
textfeld.setText(builder.toString());
}
private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
builder.append("6");
textfeld.setText(builder.toString());
}
private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {
builder.append("7");
textfeld.setText(builder.toString());
}
private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {
builder.append("8");
textfeld.setText(builder.toString());
}
private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {
builder.append("9");
textfeld.setText(builder.toString());
}
private void jButtonPunktActionPerformed(java.awt.event.ActionEvent evt) {
builder.append(".");
textfeld.setText(builder.toString());
}
private void jButtonClearActionPerformed(java.awt.event.ActionEvent evt) {
builder = new StringBuilder();
textfeld.setText(builder.toString());
}
private void jButtonPlusActionPerformed(java.awt.event.ActionEvent evt) {
builder.append("+");
textfeld.setText(builder.toString());
}
private void jButtonMinusActionPerformed(java.awt.event.ActionEvent evt) {
builder.append("-");
textfeld.setText(builder.toString());
}
private void jButtonMalActionPerformed(java.awt.event.ActionEvent evt) {
builder.append("*");
textfeld.setText(builder.toString());
}
private void jButtonGeteiltActionPerformed(java.awt.event.ActionEvent evt) {
builder.append("/");
textfeld.setText(builder.toString());
}
private void jButtonKlammerAufActionPerformed(java.awt.event.ActionEvent evt) {
builder.append("(");
textfeld.setText(builder.toString());
}
private void jButtonKlammerZuActionPerformed(java.awt.event.ActionEvent evt) {
builder.append(")");
textfeld.setText(builder.toString());
}
private void jButtonGleichActionPerformed(java.awt.event.ActionEvent evt) {
}
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see [url=http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html]How to Set the Look and Feel (The Java™ Tutorials > Creating a GUI With JFC/Swing > Modifying the Look and Feel)[/url]
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Taschenrechner.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Taschenrechner.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Taschenrechner.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Taschenrechner.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Taschenrechner().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton0;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JButton jButton7;
private javax.swing.JButton jButton8;
private javax.swing.JButton jButton9;
private javax.swing.JButton jButtonClear;
private javax.swing.JButton jButtonGeteilt;
private javax.swing.JButton jButtonGleich;
private javax.swing.JButton jButtonKlammerAuf;
private javax.swing.JButton jButtonKlammerZu;
private javax.swing.JButton jButtonMal;
private javax.swing.JButton jButtonMinus;
private javax.swing.JButton jButtonPlus;
private javax.swing.JButton jButtonPunkt;
private javax.swing.JTextField textfeld;
// End of variables declaration
}