我正在使用计算器并使用NetBeans GUI生成器,因此可能有点不清楚和令人困惑。 (完整代码http://pastebin.com/ZLcM74ZQ,因为它太长了)
package rechnerfinal;
/**
*
* @author Fredrik
*/
public class RechnerJFrame extends javax.swing.JFrame {
/**
* Creates new form RechnerJFrame
*/
private int value1;
private int value2;
private int Rechenoperation;
public RechnerJFrame() {
initComponents();
}
//Generated Code....
private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//7
String labeltextNeu = null;
String labeltext = jLabel1.getText();
if(labeltext == "0") {
setlabeltext(7);
} else {
labeltextNeu = labeltext + "7";
setlabeltext(Integer.valueOf(labeltextNeu));
}
}
private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {
//÷
value1 = Integer.valueOf(jLabel1.getText());
Rechenoperation = 4;
jLabel1.setText("0");
}
private void jButton21ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//1
String labeltextNeu = null;
String labeltext = jLabel1.getText();
if(labeltext == "0") {
setlabeltext(1);
} else {
labeltextNeu = labeltext + "0";
setlabeltext(Integer.valueOf(labeltextNeu));
}
}
private void jButton17ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//1
String labeltextNeu = null;
String labeltext = jLabel1.getText();
if(labeltext == "0") {
setlabeltext(1);
} else {
labeltextNeu = labeltext + "1";
setlabeltext(Integer.valueOf(labeltextNeu));
}
}
private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//8
String labeltextNeu = null;
String labeltext = jLabel1.getText();
if(labeltext == "0") {
setlabeltext(8);
} else {
labeltextNeu = labeltext + "8";
setlabeltext(Integer.valueOf(labeltextNeu));
}
}
private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//9
String labeltextNeu = null;
String labeltext = jLabel1.getText();
if(labeltext == "0") {
setlabeltext(9);
} else {
labeltextNeu = labeltext + "9";
setlabeltext(Integer.valueOf(labeltextNeu));
}
}
private void jButton13ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//4
String labeltextNeu = null;
String labeltext = jLabel1.getText();
if(labeltext == "0") {
setlabeltext(4);
} else {
labeltextNeu = labeltext + "4";
setlabeltext(Integer.valueOf(labeltextNeu));
}
}
private void jButton14ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//5
String labeltextNeu = null;
String labeltext = jLabel1.getText();
if(labeltext == "0") {
setlabeltext(5);
} else {
labeltextNeu = labeltext + "5";
setlabeltext(Integer.valueOf(labeltextNeu));
}
}
private void jButton15ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//6
String labeltextNeu = null;
String labeltext = jLabel1.getText();
if(labeltext == "0") {
setlabeltext(6);
} else {
labeltextNeu = labeltext + "6";
setlabeltext(Integer.valueOf(labeltextNeu));
}
}
private void jButton18ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//2
String labeltextNeu = null;
String labeltext = jLabel1.getText();
if(labeltext == "0") {
setlabeltext(2);
} else {
labeltextNeu = labeltext + "2";
setlabeltext(Integer.valueOf(labeltextNeu));
}
}
private void jButton19ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//3
String labeltextNeu = null;
String labeltext = jLabel1.getText();
if(labeltext == "0") {
setlabeltext(3);
} else {
labeltextNeu = labeltext + "3";
setlabeltext(Integer.valueOf(labeltextNeu));
}
}
private void jButton16ActionPerformed(java.awt.event.ActionEvent evt) {
//+
value1 = Integer.valueOf(jLabel1.getText());
Rechenoperation = 1;
jLabel1.setText("0");
}
private void jButton24ActionPerformed(java.awt.event.ActionEvent evt) {
//=
int Ergebnis;
switch(Rechenoperation) {
case 1: //+
value2 = Integer.valueOf(jLabel1.getText());
Ergebnis = value1 + value2;
setlabeltext(Ergebnis);
case 2: //-
value2 = Integer.valueOf(jLabel1.getText());
Ergebnis = value1 - value2;
setlabeltext(Ergebnis);
case 3: //x
value2 = Integer.valueOf(jLabel1.getText());
Ergebnis = value1 * value2;
setlabeltext(Ergebnis);
case 4: //:
value2 = Integer.valueOf(jLabel1.getText());
Ergebnis = value1 / value2;
setlabeltext(Ergebnis);
}
Ergebnis = 0;
}
private void jButton23ActionPerformed(java.awt.event.ActionEvent evt) {
//AC
jLabel1.setText("0");
value1 = 0;
value2 = 0;
Rechenoperation = 0;
}
private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) {
//-
value1 = Integer.valueOf(jLabel1.getText());
Rechenoperation = 2;
jLabel1.setText("0");
}
private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {
//*
value1 = Integer.valueOf(jLabel1.getText());
Rechenoperation = 3;
jLabel1.setText("0");
}
private void jButton22ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
public void setlabeltext(int labelwert) {
jLabel1.setText(Integer.toString(labelwert));
}
/**
* @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(RechnerJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(RechnerJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(RechnerJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(RechnerJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new RechnerJFrame().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 jButton14;
private javax.swing.JButton jButton15;
private javax.swing.JButton jButton16;
private javax.swing.JButton jButton17;
private javax.swing.JButton jButton18;
private javax.swing.JButton jButton19;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton20;
private javax.swing.JButton jButton21;
private javax.swing.JButton jButton22;
private javax.swing.JButton jButton23;
private javax.swing.JButton jButton24;
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.JLabel jLabel1;
// End of variables declaration
}
这是我得到的错误:
Exception in thread "AWT-EventQueue-0" java.lang.ArithmeticException: / by zero
at rechnerfinal.RechnerJFrame.jButton24ActionPerformed(RechnerJFrame.java:451)
at rechnerfinal.RechnerJFrame.access$1700(RechnerJFrame.java:8)
at rechnerfinal.RechnerJFrame$18.actionPerformed(RechnerJFrame.java:212)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
此外,jLabel
未显示计算结果,但在使用System.out.println
进行测试时,它为我提供了正确的解决方案。我完全没有想到可能导致这种情况的想法。
答案 0 :(得分:1)
您的switch
声明中有几个问题:
switch(Rechenoperation) {
case 1: //+
value2 = Integer.valueOf(jLabel1.getText());
Ergebnis = value1 + value2;
setlabeltext(Ergebnis);
break;
case 2: //-
value2 = Integer.valueOf(jLabel1.getText());
Ergebnis = value1 - value2;
setlabeltext(Ergebnis);
break;
case 3: //x
value2 = Integer.valueOf(jLabel1.getText());
Ergebnis = value1 * value2;
setlabeltext(Ergebnis);
break;
case 4: //:
value2 = Integer.valueOf(jLabel1.getText());
if(value2 != 0){ //Add a safeguard if value2 is '0'
Ergebnis = value1 / value2;
setlabeltext(Ergebnis);
} else {
setlabeltext("NaN");
}
break;
}
在每种情况下你都需要打破逻辑。这使事情变得更有效率并且是良好的做法。另外,在分割之前检查value2
是否为零。
正如FredK所指出的,你可以在switch语句之后setlabeltext
并在switch语句之前得到value2
。无论如何,效率与最高效率无关,由于您的break
语句,这些将被执行一次,但它仍然以更好的方式进行维护。