einfache Taschenrechner mit netbeans
ich mache einen einfachen Rechner mit Net beans, aber es gibt ein problem zwischen Button und Textfeld .
ich will die Sache, wo ich auf die Taste "1" nach dem anklicken eines es soll zeigen, man aber nichts anzeigt und bitte um Hilfe, wie setup " + " - Taste zwischen Ihnen helfen, bitte.
/*
* Ändern Sie diese Vorlage, wählen Sie Tools | Vorlagen
* und öffnen Sie die Vorlage in den editor.
*/
/**
*
* @author Administrator
*/
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();
jButton12 = new javax.swing.JButton();
jButton10 = new javax.swing.JButton();
jButton11 = new javax.swing.JButton();
jButton9 = new javax.swing.JButton();
jButton1 = new javax.swing.JButton();
jTextField1 = new javax.swing.JTextField();
jButton8 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton7 = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
jButton13 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Rajendra Calculator", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.ABOVE_TOP));
jButton12.setFont(new java.awt.Font("Arial Black", 0, 11)); //NOI18N
jButton12.setText("=");
jButton10.setFont(new java.awt.Font("Arial Black", 0, 11)); //NOI18N
jButton10.setText("0");
jButton11.setFont(new java.awt.Font("Arial Black", 0, 11)); //NOI18N
jButton11.setText("+");
jButton11.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton11ActionPerformed(evt);
}
});
jButton9.setFont(new java.awt.Font("Arial Black", 0, 11)); //NOI18N
jButton9.setText("9");
jButton1.setFont(new java.awt.Font("Arial Black", 0, 11)); //NOI18N
jButton1.setText("1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jTextField1.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
jTextField1.setText("0");
jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(evt);
}
});
jButton8.setFont(new java.awt.Font("Arial Black", 0, 11)); //NOI18N
jButton8.setText("8");
jButton5.setFont(new java.awt.Font("Arial Black", 0, 11)); //NOI18N
jButton5.setText("5");
jButton4.setFont(new java.awt.Font("Arial Black", 0, 11)); //NOI18N
jButton4.setText("4");
jButton4.setToolTipText("");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
jButton2.setFont(new java.awt.Font("Arial Black", 0, 11)); //NOI18N
jButton2.setText("2");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton7.setFont(new java.awt.Font("Arial Black", 0, 11)); //NOI18N
jButton7.setText("7");
jButton6.setFont(new java.awt.Font("Arial Black", 0, 11)); //NOI18N
jButton6.setText("6");
jButton13.setFont(new java.awt.Font("Arial Black", 0, 11)); //NOI18N
jButton13.setText("Clear");
jButton3.setFont(new java.awt.Font("Arial Black", 0, 11)); //NOI18N
jButton3.setText("3");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jButton4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton5)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton6))
.addComponent(jTextField1)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton3))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jButton7)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton8)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton9)))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jButton11, javax.swing.GroupLayout.DEFAULT_SIZE, 65, Short.MAX_VALUE)
.addComponent(jButton10, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jButton12, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton13, javax.swing.GroupLayout.DEFAULT_SIZE, 66, Short.MAX_VALUE)))))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap(25, Short.MAX_VALUE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2)
.addComponent(jButton3))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton4)
.addComponent(jButton5)
.addComponent(jButton6))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton7)
.addComponent(jButton8)
.addComponent(jButton9))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton10)
.addComponent(jButton13))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton11)
.addComponent(jButton12))
.addContainerGap())
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(18, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}//</editor-fold>
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
//TODO add your handling code here:
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText(jTextField1.getText()+"1");
}
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
//TODO add your handling code here:
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText(jTextField1.getText()+"2"); //TODO add your handling code here:
}
private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {
int valA = Integer.parseInt(jTextField1.getText());
int valB = Integer.parseInt(jTextField1.getText()); //TODO add your handling code here:
int valC = valA+valB;
jTextField1.append(Integer.toString(valC));
jTextField1.append("\n"); //TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
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 http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
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(Calculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Calculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Calculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Calculator.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 Calculator().setVisible(true);
}
});
}
//Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton10;
private javax.swing.JButton jButton11;
private javax.swing.JButton jButton12;
private javax.swing.JButton jButton13;
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.JPanel jPanel1;
private javax.swing.JTextField jTextField1;
//End of variables declaration
}
- Bitte klären Sie Ihr problem oder weitere details zu markieren, genau das, was Sie brauchen. Wie es derzeit geschrieben, es ist schwer zu sagen, genau das, was Sie gefragt haben. (kopiert von flag-dialog)
- ich wollte nur wissen ...Wie einrichten JButton ..wo ich drücken Sie es und zeigt es "1" auf "textField"....und wie man JButton für"+", wo es sich lohnt, für die Ergänzung?? hoffen kann Sie verstehen..
Du musst angemeldet sein, um einen Kommentar abzugeben.
In der Regel, in Fällen wie diesem das Textfeld nicht editierbar, so dass Sie effektiv "append" text an das Textfeld mit:
Jedoch der bessere Ansatz ist es, gemeinsam eine Aktion für alle Tasten:
JTextField
keineappend
Methode, es hat nur einsetText
Methode.Wenn Sie wirklich brauchen, "Anhängen" - Funktion, dann müssen Sie eine
JTextArea
statt...Nehmen Sie sich Zeit und Lesen Sie sich durch die Erstellung einer GUI mit Swing und Mithilfe von text-Komponenten.
Persönlich, ich würde werfen aus der Formular-designer und erfahren Sie die API so gut wie Sie können, wird es eine größere Wertschätzung dessen, was der form designer ist und wenn es angebracht ist, es zu benutzen...IMHO
Aktualisiert
Ihre grundlegende Ablauf ist aus. Ein Benutzer gibt einen Wert ein und drücken Sie "+", werden Sie dann versuchen, und fügen Sie den Wert in das Textfeld ein, um sich selbst und re-gilt der Wert wieder auf das Textfeld...Das bedeutet, dass es unwahrscheinlich ist, dass der Benutzer tatsächlich jemals in der Lage sein, um die Summe von zwei Werten zusammen.
Stattdessen sollten Sie das speichern der Werte in das Textfeld in eine Art
List
jedes mal, wenn der Benutzer klickt auf den " + " - Taste und clearing-text-Feld für den nächsten Wert. Wenn Sie drücken Sie die " = " - Taste, sollten Sie einfach iteriert über diese Liste, und stellen Sie das Ergebnis in das Textfeld und löschen der Liste...Zum Beispiel...
Haben Sie nicht, sofern ein
actionPerformed
Methode für das " = " - Taste, aber es ist der Inhalt würde so Aussehen...jTextField1.setText(jTextField1.getText().concat(String string));
.JTextField
nicht unterstützt neue Linien...JTextField
, was darauf hindeuten würde, dass Sie wirklich wollen, eineJTextArea
append(String str)
Methode und nicht zu interpretieren und/oder Unterstützung für multi-line.