- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用计算器并使用 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
进行测试时,它给了我正确的解决方案。我完全不知道是什么原因导致这种情况。
最佳答案
您的 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
语句,这些将被执行一次,但这种方式仍然更适合维护。
关于java.lang.ArithmeticException 看不出为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37619989/
在 Tomcat 6/Ubuntu 12.04 上启动 Grails 2.1.0 应用程序时出现以下错误。 Error 500 - Internal Server Error. groovy.lang
在运行 Storm 拓扑时,我收到此错误。拓扑完美运行 5 分钟,没有任何错误,然后失败。我正在使用 Config.TOPOLOGY_TICK_TUPLE_FREQ_SECS as 300 sec i
我有一个 jsp 代码在其中一台机器上运行良好。但是当我复制到另一台机器时,我得到了这个 no such method found 异常。我是 Spring 的新手。有人可以解释我错过了什么吗? 以下
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我的代码在下面给出了一个错误; Exception in thread "main" java.lang.NoSuchMethodError: com/myApp/Client.cypherCBC(L
我正在尝试一个 Restful web 服务示例,所以当我要访问 url 时,我遇到了异常 java.lang.NoSuchMethodError: jersey.repackaged.com.goo
我正在将一个 Spring web 项目转换为一个 Maven 项目,但我收到了这个错误: java.lang.NoSuchMethodError: org.jboss.logging.Logger.
在我的项目中,我有一个像这样的枚举: public enum MyEnum { FIRST(1), SECOND(2); private int value; private MyEnum(int v
我创建了这个简单的示例,用于读取 Linux 正常运行时间: public String getMachineUptime() throws IOException { String[] di
我正在使用 Eclipse,并且正在使用 Java。我的目标是使用 bogoSort 方法对 vector 进行排序在一个 vector (vectorExample)中适应我的 vector 类型,
我正在运行以下查询。它显示一条错误消息。如何解决这个错误? ListrouteList=null; List companyList = session.createS
我有以下模型类: @Entity @Table(name="user_content") @org.hibernate.annotations.NamedQueries({ @org.
我有那个错误。这是我的代码: GmailSettingsService service = new GmailSettingsService(APPLICATION_NAME, DOMAIN_NAME
实际上我在执行我的java程序时遇到了下面提到的错误 Exception in thread "pool-1-thread-1" java.lang.ClassCastException: jav
java.lang.ClassCastException: java.lang.Float cannot be cast to java.lang.String 我在以下代码中遇到此异常: Strin
我正在尝试从 linkedhashset 中检索随机元素。下面是我的代码,但它每次都给我异常。 private static void generateRandomUserId(Set userIds
我已经完成了 Android 中的代码: List spinnerArray = new ArrayList(); for (int i = 0; i item = (LinkedTreeMap)
这个问题已经有答案了: Explanation of ClassCastException in Java (12 个回答) 已关闭 6 年前。 我已经编写了 java 到 Json 的代码,同时从页
这个问题在这里已经有了答案: ClassCastException java.lang.Long cannot be cast to clojure.lang.IFn (4 个答案) 关闭 6 年前
我在运行时遇到问题来编译这段代码,这给我一个错误,java.lang.Integer 无法转换为 Java.lang.Double。如果有人帮助我更正此代码,我将非常高兴 double x; pu
我是一名优秀的程序员,十分优秀!