当我运行代码时,gui会弹出,但是当我点击一个按钮时,它会在文本框中显示:
javax.swing.JButton中[,0,65,37x34,alignmentX = 0.0,alignmentY = 0.5,边界= javax.swing.plaf.synth.SynthBorder @ 5066ef73,旗帜= 41943328,MAXIMUMSIZE =,=的minimumSize,首选大小= ,的DefaultIcon =,disabledIcon =,disabledSelectedIcon =,余量= javax.swing.plaf.InsetsUIResource [顶= 0,左= 0,底部= 0,右= 0],的paintBorder =真,paintFocus =真,pressedIcon =,rolloverEnabled =真,rolloverIcon =,rolloverSelectedIcon =,selectedIcon =,文本= 1,defaultCapable = TRU
...当我只想要显示号码时。
所以我的问题是,我怎样才能让它消失或者我的代码是做什么的呢? 唯一应该出现的是数字'1'
这是我的代码:
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
}
答案 0 :(得分:1)
private void Button5ActionPerformed(java.awt.event.ActionEvent evt) {
String inputNumber = textDisplay.getText()+ Button5;
textDisplay.setText(inputNumber);
}
将textDisplay.getText()+ Button5
更改为Button5.getText()
private void Button5ActionPerformed(java.awt.event.ActionEvent evt) {
String inputNumber = Button5.getText();
textDisplay.setText(inputNumber);
}
textDisplay.getText()+ Button5
实际上意味着textDisplay.getText()+ Button5.toString()
Button5.toString()
正在产生你不喜欢看到的所有垃圾。
javax.swing.JButton button = (javax.swing.JButton)evt.getSource()
会给出触发事件的按钮。这样,您就不需要为每个按钮使用单独的ButtonXActionPerformed()
方法。 (您可以为每个按钮注册相同的方法。)