Progamminnoob Progamminnoob - 5 months ago 12
Java Question

Java calculator gui number issue

When I run the code the gui pops up like it should but when I click on a button it gives this in the textfield box:


javax.swing.JButton[,0,65,37x34,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.synth.SynthBorder@5066ef73,flags=41943328,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=1,defaultCapable=tru


...when I only want the number to show up.

So my question is, how can I get this to go away or what is making my code do this?
The only thing that should be showing up is the number '1'

Here is my code:

package Calc;


public class Calculate extends javax.swing.JFrame {
double firstNumber;
double secondNumber;
double finalResult;
String operation;
/**
* Creates new form Calculate
*/
public Calculate() {
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() {

textDisplay = new javax.swing.JTextField();
Button1 = new javax.swing.JButton();
Button2 = new javax.swing.JButton();
Button3 = new javax.swing.JButton();
Button4 = new javax.swing.JButton();
Button5 = new javax.swing.JButton();
Button6 = new javax.swing.JButton();
Button7 = new javax.swing.JButton();
Button8 = new javax.swing.JButton();
Button9 = new javax.swing.JButton();
Button10 = new javax.swing.JButton();
Button11 = new javax.swing.JButton();
Button12 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

textDisplay.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
textDisplay.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
textDisplay.setMargin(null);
textDisplay.setMaximumSize(null);
textDisplay.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
textDisplayActionPerformed(evt);
}
});

Button1.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
Button1.setText("1");
Button1.setToolTipText("");
Button1.setAutoscrolls(true);
Button1.setInheritsPopupMenu(true);
Button1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Button1ActionPerformed(evt);
}
});

Button2.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
Button2.setText("2");
Button2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Button2ActionPerformed(evt);
}
});

Button3.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
Button3.setText("3");
Button3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Button3ActionPerformed(evt);
}
});

Button4.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
Button4.setText("4");
Button4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Button4ActionPerformed(evt);
}
});

Button5.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
Button5.setText("5");
Button5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Button5ActionPerformed(evt);
}
});

Button6.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
Button6.setText("6");
Button6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Button6ActionPerformed(evt);
}
});

Button7.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
Button7.setText("7");
Button7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Button7ActionPerformed(evt);
}
});

Button8.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
Button8.setText("8");
Button8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Button8ActionPerformed(evt);
}
});

Button9.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
Button9.setText("9");
Button9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Button9ActionPerformed(evt);
}
});

Button10.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
Button10.setText("c");
Button10.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Button10ActionPerformed(evt);
}
});

Button11.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
Button11.setText("0");
Button11.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Button11ActionPerformed(evt);
}
});

Button12.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
Button12.setText("+");
Button12.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Button12ActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(textDisplay, javax.swing.GroupLayout.PREFERRED_SIZE, 255, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addComponent(Button1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Button2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Button3))
.addGroup(layout.createSequentialGroup()
.addComponent(Button4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Button5)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Button6))
.addGroup(layout.createSequentialGroup()
.addComponent(Button7)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Button8)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Button9))
.addGroup(layout.createSequentialGroup()
.addComponent(Button10)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Button11)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Button12)))
.addGap(0, 145, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(textDisplay, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Button1)
.addComponent(Button2)
.addComponent(Button3))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Button4)
.addComponent(Button5)
.addComponent(Button6))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Button7)
.addComponent(Button8)
.addComponent(Button9))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Button10)
.addComponent(Button11)
.addComponent(Button12))
.addContainerGap(88, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

private void Button5ActionPerformed(java.awt.event.ActionEvent evt) {
String inputNumber = textDisplay.getText()+ Button5;
textDisplay.setText(inputNumber);
}

private void Button1ActionPerformed(java.awt.event.ActionEvent evt) {
String inputNumber = textDisplay.getText()+ Button1;
textDisplay.setText(inputNumber);

}

private void Button2ActionPerformed(java.awt.event.ActionEvent evt) {
String inputNumber = textDisplay.getText()+ Button2;
textDisplay.setText(inputNumber);
}

private void Button3ActionPerformed(java.awt.event.ActionEvent evt) {
String inputNumber = textDisplay.getText()+ Button3;
textDisplay.setText(inputNumber);
}

private void Button4ActionPerformed(java.awt.event.ActionEvent evt) {
String inputNumber = textDisplay.getText()+ Button4;
textDisplay.setText(inputNumber);
}

private void Button6ActionPerformed(java.awt.event.ActionEvent evt) {
String inputNumber = textDisplay.getText()+ Button6;
textDisplay.setText(inputNumber);
}

private void Button7ActionPerformed(java.awt.event.ActionEvent evt) {
String inputNumber = textDisplay.getText()+ Button7;
textDisplay.setText(inputNumber);
}

private void Button8ActionPerformed(java.awt.event.ActionEvent evt) {
String inputNumber = textDisplay.getText()+ Button8;
textDisplay.setText(inputNumber);
}

private void Button9ActionPerformed(java.awt.event.ActionEvent evt) {
String inputNumber = textDisplay.getText()+ Button9;
textDisplay.setText(inputNumber);
}

private void Button10ActionPerformed(java.awt.event.ActionEvent evt) {
String inputNumber = textDisplay.getText()+ Button10;
textDisplay.setText(inputNumber);
}

private void Button11ActionPerformed(java.awt.event.ActionEvent evt) {
String inputNumber = textDisplay.getText()+ Button11;
textDisplay.setText(inputNumber);
}

private void Button12ActionPerformed(java.awt.event.ActionEvent evt) {
firstNumber = Double.parseDouble(textDisplay.getText());
textDisplay.setText("");
operation = "+";
if(operation == "+")
{
finalResult = firstNumber + secondNumber;
String answer = String.format("%.0f",finalResult);
textDisplay.setText(answer);
}
}

private void textDisplayActionPerformed(java.awt.event.ActionEvent evt) {
String inputNumber = textDisplay.getText()+ textDisplay;
textDisplay.setText(inputNumber);
}

/**
* @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(Calculate.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Calculate.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Calculate.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Calculate.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 Calculate().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JButton Button1;
private javax.swing.JButton Button10;
private javax.swing.JButton Button11;
private javax.swing.JButton Button12;
private javax.swing.JButton Button2;
private javax.swing.JButton Button3;
private javax.swing.JButton Button4;
private javax.swing.JButton Button5;
private javax.swing.JButton Button6;
private javax.swing.JButton Button7;
private javax.swing.JButton Button8;
private javax.swing.JButton Button9;
private javax.swing.JTextField textDisplay;
// End of variables declaration


}

Jay Jay
Answer

This code is your problem:

private void Button5ActionPerformed(java.awt.event.ActionEvent evt) {                                        
   String inputNumber = textDisplay.getText()+ Button5;
    textDisplay.setText(inputNumber);
} 

Solution:

Change textDisplay.getText()+ Button5 to Button5.getText()

private void Button5ActionPerformed(java.awt.event.ActionEvent evt) {                                        
   String inputNumber = Button5.getText();
    textDisplay.setText(inputNumber);
}

Explanation:

textDisplay.getText()+ Button5 really means textDisplay.getText()+ Button5.toString()

The Button5.toString() is what is generating all the junk you don't like to see.

FYI:

javax.swing.JButton button = (javax.swing.JButton)evt.getSource() will give the button that trigged the event. This way you don't need a separate ButtonXActionPerformed() method for every button. (You can can register the same method for every button.)

Comments