动作侦听器出错

时间:2014-02-18 15:17:02

标签: java user-interface

所以我制作了这个基本上解决了SUVAT问题的Java GUI(对于那些不熟悉SUVAT的人来说,它基本上可以告诉你一个汽车在速度/时间内加速多少的近似值,或者找到一个多高的值球将以给定的速度等进行。你使用mehcanics)。 我知道我的程序中存在缺陷,例如只生成整数变量(只是为了保持整洁),所以答案可能会被四舍五入。布局不好,但我刚刚开始编程。 GUI完全加载,但是当用户按下计算按钮时,就会崩溃。我怀疑它与按钮上的动作监听器有关,因为我认为它使它有点复杂然后它应该。

用户基本上选择他想要解决的问题(例如,如果我想找到汽车的加速度,那么我会检查加速度单选按钮,并输入我知道的给定变量(即初始速度为15,最终速度为30,时间为2,因此加速度应为7.5(在这种情况下为8,因为使用了整数)。程序选择最合适的方程式来查找您想知道的变量。

这是代码:

   import javax.swing.JFrame;


public class GUImain {

public static void main(String[] args) {

    GUI go = new GUI();
    go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    go.setSize(400,400);
    go.setVisible(true);

}

} 

import java.awt.FlowLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

public class GUI extends JFrame implements ActionListener {

JTextField DISPLACEMENT = new JTextField("Displacement", 20);
JTextField INITALVELOCITY = new JTextField("Intital Velocity", 20);
JTextField FINALVELOCITY = new JTextField("Final Velocity", 20);
JTextField ACCELERATION = new JTextField("Acceleration", 20);
JTextField TIME = new JTextField("Time");

JRadioButton S = new JRadioButton("Displacement", true);
JRadioButton U = new JRadioButton("Inital Velocity", false);
JRadioButton V = new JRadioButton("Final Velocity", false);
JRadioButton A = new JRadioButton("Acceleration", false);
JRadioButton T = new JRadioButton("Time", false);
ButtonGroup VARIABLEFINDER;
JButton CALCULATE = new JButton("Calculate");

GUI() {
    super("SUVAT Solver");
    setLayout(new FlowLayout());
    add(DISPLACEMENT);
    add(INITALVELOCITY);
    add(FINALVELOCITY);
    add(ACCELERATION);
    add(TIME);
    add(CALCULATE);

    add(S);
    add(U);
    add(V);
    add(A);
    add(T);

    VARIABLEFINDER = new ButtonGroup();
    VARIABLEFINDER.add(S);
    VARIABLEFINDER.add(U);
    VARIABLEFINDER.add(V);
    VARIABLEFINDER.add(A);
    VARIABLEFINDER.add(T);

    // Adding Listeners to the RadioButtons
    CALCULATE.addActionListener(this);

}

@Override
public void actionPerformed(ActionEvent e) {

    String dis = DISPLACEMENT.getText();
    int SS = Integer.parseInt(dis);

    String initv = INITALVELOCITY.getText();
    int UU = Integer.parseInt(initv);

    String finalv = FINALVELOCITY.getText();
    int VV = Integer.parseInt(finalv);

    String acc = ACCELERATION.getText();
    int AA = Integer.parseInt(acc);

    String timme = TIME.getText();
    int TT = Integer.parseInt(timme);

    // CALCULATING DISPLACEMENT
    if (e.getSource() == CALCULATE && S.isSelected()) {
        if (FINALVELOCITY.getText().equals("Final Velocity")) {
            SS = (int) ((int) (UU * TT) + (0.5 * AA * (TT * TT)));
            JOptionPane.showMessageDialog(null,
                    "The displacement claulcated is " + SS);
        } else if (ACCELERATION.getText().equals("Acceleration")) {
            SS = (int) 0.5 * (UU + VV) * TT;
            JOptionPane.showMessageDialog(null,
                    "The displacement claulcated is " + SS);
        } else if (INITALVELOCITY.getText().equals("Inital Velocity")) {
            SS = (int) ((VV * TT) - (0.5 * AA * (TT * TT)));
            JOptionPane.showMessageDialog(null,
                    "The displacement claulcated is " + SS);
        } else if (TIME.getText().equals("Time")) {
            SS = (((VV * VV) - (UU * UU)) / (2 * AA));
            JOptionPane.showMessageDialog(null,
                    "The displacement claulcated is " + SS);
        }

    }
    // CALCULATING THE INITAL VELOCITY
    if (e.getSource() == CALCULATE && U.isSelected()) {
        if (FINALVELOCITY.getText().equals("Final Velocity")) {
            UU = (int) ((SS - (0.5 * AA * (TT * TT))) / (TT));
            JOptionPane.showMessageDialog(null,
                    "The intial velocity claulcated is " + UU);
        } else if (ACCELERATION.getText().equals("Acceleration")) {
            UU = ((2 * SS) / TT) - VV;
            JOptionPane.showMessageDialog(null,
                    "The intial velocity claulcated is " + UU);
        } else if (TIME.getText().equals("Time")) {
            UU = (int) Math.sqrt(((VV * VV) - (2 * AA * SS)));
            JOptionPane.showMessageDialog(null,
                    "The intial velocity claulcated is " + UU);
        } else if (DISPLACEMENT.getText().equals("Displacement")) {
            UU = (AA * TT) - VV;
            JOptionPane.showMessageDialog(null,
                    "The intial velocity claulcated is " + UU);
        }
    }

    // CALCULATING THE FINAL VELOCITY
    if (e.getSource() == CALCULATE && V.isSelected()) {
        if (INITALVELOCITY.getText().equals("Inital Velocity")) {
            VV = (int) ((SS + (0.5 * AA * (TT * TT))) / TT);
            JOptionPane.showMessageDialog(null,
                    "The final velocity claulcated is " + VV);
        } else if (TIME.getText().equals("Time")) {
            VV = (int) Math.sqrt(((UU * UU) + (2 * AA * SS)));
            JOptionPane.showMessageDialog(null,
                    "The final velocity claulcated is " + VV);
        } else if (DISPLACEMENT.getText().equals("Displacement")) {
            VV = (AA * TT) + UU;
            JOptionPane.showMessageDialog(null,
                    "The final velocity claulcated is " + VV);
        } else if (ACCELERATION.getText().equals("Acceleration")) {
            VV = ((2 * SS) / TT) - UU;
            JOptionPane.showMessageDialog(null,
                    "The final velocity claulcated is " + VV);
        }
    }

    // CALCULATING THE ACCELERATION
    if (e.getSource() == CALCULATE && A.isSelected()) {
        if (INITALVELOCITY.getText().equals("Inital Velocity")) {
            AA = (int) ((SS - (VV * TT)) / (-0.5 * (TT * TT)));
            JOptionPane.showMessageDialog(null,
                    "The acceleration claulcated is " + AA);
        } else if (TIME.getText().equals("Time")) {
            AA = (((VV * VV) - (UU * UU)) / (2 * SS));
            JOptionPane.showMessageDialog(null,
                    "The acceleration claulcated is " + AA);
        } else if (DISPLACEMENT.getText().equals("Displacement")) {
            AA = (VV - UU) / TT;
            JOptionPane.showMessageDialog(null,
                    "The acceleration claulcated is " + AA);
        } else if (FINALVELOCITY.getText().equals("Final Velocity")) {
            VV = (int) ((SS - (UU * TT)) / (0.5 * (TT * TT)));
            JOptionPane.showMessageDialog(null,
                    "The acceleration claulcated is " + AA);
        }
    }

    // CALCULATING THE TIME
    if (CALCULATE.getAction() && A.isSelected()) {
        if (INITALVELOCITY.getText().equals("Inital Velocity")) {
            AA = (int) ((SS - (VV * TT)) / (-0.5 * (TT * TT)));
            JOptionPane.showMessageDialog(null,
                    "havent done the time calculations yet");
        } else if (TIME.getText().equals("Time")) {
            AA = (((VV * VV) - (UU * UU)) / (2 * SS));
            JOptionPane.showMessageDialog(null,
                    "havent done the time calculations yet");
        } else if (DISPLACEMENT.getText().equals("Displacement")) {
            AA = (VV - UU) / TT;
            JOptionPane.showMessageDialog(null,
                    "havent done the time calculations yet");
        } else if (FINALVELOCITY.getText().equals("Final Velocity")) {
            VV = (int) ((SS - (UU * TT)) / (0.5 * (TT * TT)));
            JOptionPane.showMessageDialog(null,
                    "havent done the time calculations yet");
        }
    }

    }

 }

我认为当有人想要使用不包含位移的等式((VU)/ T)找到加速度时,错误就会出现,编译器仍在将文本字段中的默认文本识别为字符串并且因为单击计算按钮时,将字符串转换为int,文本字段中的所有内容都将转换为int,并且我在动作侦听器中的参数非常具体。

这是错误:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "Displacement"
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at GUI.actionPerformed(GUI.java:66)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

编辑=感谢鲍里斯的蜘蛛,你让我意识到我试图将一个字符串转换成一个int,其中该字符串不包含任何数字。傻我。多数令人尴尬。这就是问题

无论如何,该程序运行正常,给出了应该做的圆形答案。但我很确定我拖动应用程序的代码有点太多了。这是工作代码的工作部分动作部分监听器:

public void actionPerformed(ActionEvent e) {



    // CALCULATING DISPLACEMENT
    if (e.getSource() == CALCULATE && S.isSelected()) {
        if (FINALVELOCITY.getText().equals("")) {
            String initv = INITALVELOCITY.getText();
            int UU = Integer.parseInt(initv);
            String timme = TIME.getText();
            int TT = Integer.parseInt(timme);
            String acc = ACCELERATION.getText();
            int AA = Integer.parseInt(acc);

            int SS = (int) ((int) (UU * TT) + (0.5 * AA * (TT * TT)));

            JOptionPane.showMessageDialog(null,
                    "The displacement claulcated is " + SS);
        } else if (ACCELERATION.getText().equals("")) {
            String timme = TIME.getText();
            int TT = Integer.parseInt(timme);
            String finalv = FINALVELOCITY.getText();
            int VV = Integer.parseInt(finalv);
            String initv = INITALVELOCITY.getText();
            int UU = Integer.parseInt(initv);

            int SS = (int) 0.5 * (UU + VV) * TT;

            JOptionPane.showMessageDialog(null,
                    "The displacement claulcated is " + SS);
        } else if (INITALVELOCITY.getText().equals("")) {
            String timme = TIME.getText();
            int TT = Integer.parseInt(timme);
            String acc = ACCELERATION.getText();
            int AA = Integer.parseInt(acc);
            String finalv = FINALVELOCITY.getText();
            int VV = Integer.parseInt(finalv);

            int SS = (int) ((VV * TT) - (0.5 * AA * (TT * TT)));

            JOptionPane.showMessageDialog(null,
                    "The displacement claulcated is " + SS);

        } else if (TIME.getText().equals("")) {

            String finalv = FINALVELOCITY.getText();
            int VV = Integer.parseInt(finalv);
            String initv = INITALVELOCITY.getText();
            int UU = Integer.parseInt(initv);
            String acc = ACCELERATION.getText();
            int AA = Integer.parseInt(acc);

            int SS = (((VV * VV) - (UU * UU)) / (2 * AA));

            JOptionPane.showMessageDialog(null,
                    "The displacement claulcated is " + SS);
        }

    }
    // CALCULATING THE INITAL VELOCITY
    if (e.getSource() == CALCULATE && U.isSelected()) {
        if (FINALVELOCITY.getText().equals("")) {

            String dis = DISPLACEMENT.getText();
            int SS = Integer.parseInt(dis);
            String acc = ACCELERATION.getText();
            int AA = Integer.parseInt(acc);
            String timme = TIME.getText();
            int TT = Integer.parseInt(timme);


            int UU = (int) ((SS - (0.5 * AA * (TT * TT))) / (TT));
            JOptionPane.showMessageDialog(null,
                    "The intial velocity claulcated is " + UU);


        } else if (ACCELERATION.getText().equals("")) {
            String timme = TIME.getText();
            int TT = Integer.parseInt(timme);
            String dis = DISPLACEMENT.getText();
            int SS = Integer.parseInt(dis);
            String finalv = FINALVELOCITY.getText();
            int VV = Integer.parseInt(finalv);


            int UU = ((2 * SS) / TT) - VV;
            JOptionPane.showMessageDialog(null,
                    "The intial velocity claulcated is " + UU);
        } 


        else if (TIME.getText().equals("")) {

            String finalv = FINALVELOCITY.getText();
            int VV = Integer.parseInt(finalv);
            String acc = ACCELERATION.getText();
            int AA = Integer.parseInt(acc);
            String dis = DISPLACEMENT.getText();
            int SS = Integer.parseInt(dis);

            int UU = (int) Math.sqrt(((VV * VV) - (2 * AA * SS)));
            JOptionPane.showMessageDialog(null,
                    "The intial velocity claulcated is " + UU);
        } else if (DISPLACEMENT.getText().equals("")) {
            String acc = ACCELERATION.getText();
            int AA = Integer.parseInt(acc);
            String finalv = FINALVELOCITY.getText();
            int VV = Integer.parseInt(finalv);
            String timme = TIME.getText();
            int TT = Integer.parseInt(timme);

            int UU = (AA * TT) - VV;
            JOptionPane.showMessageDialog(null,
                    "The intial velocity claulcated is " + UU);
        }
    }

    // CALCULATING THE FINAL VELOCITY
    if (e.getSource() == CALCULATE && V.isSelected()) {



        if (INITALVELOCITY.getText().equals("")) {
            String dis = DISPLACEMENT.getText();
            int SS = Integer.parseInt(dis);
            String acc = ACCELERATION.getText();
            int AA = Integer.parseInt(acc);
            String timme = TIME.getText();
            int TT = Integer.parseInt(timme);


            int VV = (int) ((SS + (0.5 * AA * (TT * TT))) / TT);
            JOptionPane.showMessageDialog(null,
                    "The final velocity claulcated is " + VV);
        } else if (TIME.getText().equals("")) {
            String acc = ACCELERATION.getText();
            int AA = Integer.parseInt(acc);String dis = DISPLACEMENT.getText();
            int SS = Integer.parseInt(dis);
            String initv = INITALVELOCITY.getText();
            int UU = Integer.parseInt(initv);

            int VV = (int) Math.sqrt(((UU * UU) + (2 * AA * SS)));
            JOptionPane.showMessageDialog(null,
                    "The final velocity claulcated is " + VV);
        } else if (DISPLACEMENT.getText().equals("")) {
            String initv = INITALVELOCITY.getText();
            int UU = Integer.parseInt(initv);
            String timme = TIME.getText();
            int TT = Integer.parseInt(timme);
            String acc = ACCELERATION.getText();
            int AA = Integer.parseInt(acc);

            int VV = (AA * TT) + UU;
            JOptionPane.showMessageDialog(null,
                    "The final velocity claulcated is " + VV);
        } else if (ACCELERATION.getText().equals("")) {
            String timme = TIME.getText();
            int TT = Integer.parseInt(timme);
            String dis = DISPLACEMENT.getText();
            int SS = Integer.parseInt(dis);
            String initv = INITALVELOCITY.getText();
            int UU = Integer.parseInt(initv);

            int VV = ((2 * SS) / TT) - UU;
            JOptionPane.showMessageDialog(null,
                    "The final velocity claulcated is " + VV);
        }
    }

    // CALCULATING THE ACCELERATION
    if (e.getSource() == CALCULATE && A.isSelected()) {
        if (INITALVELOCITY.getText().equals("")) {
            String dis = DISPLACEMENT.getText();
            int SS = Integer.parseInt(dis);
            String finalv = FINALVELOCITY.getText();
            int VV = Integer.parseInt(finalv);
            String timme = TIME.getText();
            int TT = Integer.parseInt(timme);

            int AA = (int) ((SS - (VV * TT)) / (-0.5 * (TT * TT)));
            JOptionPane.showMessageDialog(null,
                    "The acceleration claulcated is " + AA);
        } else if (TIME.getText().equals("")) {
            String dis = DISPLACEMENT.getText();
            int SS = Integer.parseInt(dis);
            String initv = INITALVELOCITY.getText();
            int UU = Integer.parseInt(initv);
            String finalv = FINALVELOCITY.getText();
            int VV = Integer.parseInt(finalv);

            int AA = (((VV * VV) - (UU * UU)) / (2 * SS));
            JOptionPane.showMessageDialog(null,
                    "The acceleration claulcated is " + AA);
        } else if (DISPLACEMENT.getText().equals("")) {

            String finalv = FINALVELOCITY.getText();
            int VV = Integer.parseInt(finalv);
            String initv = INITALVELOCITY.getText();
            int UU = Integer.parseInt(initv);
            String timme = TIME.getText();
            int TT = Integer.parseInt(timme);

            int AA = (VV - UU) / TT;
            JOptionPane.showMessageDialog(null,
                    "The acceleration claulcated is " + AA);
        } else if (FINALVELOCITY.getText().equals("")) {
            String initv = INITALVELOCITY.getText();
            int UU = Integer.parseInt(initv);
            String dis = DISPLACEMENT.getText();
            int SS = Integer.parseInt(dis);
            String timme = TIME.getText();
            int TT = Integer.parseInt(timme);
            int AA = (int) ((SS - (UU * TT)) / (0.5 * (TT * TT)));
            JOptionPane.showMessageDialog(null,
                    "The acceleration claulcated is " + AA);
        }
    }

我很确定我能够将字符串转换为整数作为不同类的对象的方法,所以当我想将一个字符串从位移文本字段转换为int时,我可以调用该对象吗? / p>

1 个答案:

答案 0 :(得分:1)

Boris the Spider 在其中一条评论中解决了主要问题。

关于编辑部分中的隐含问题:

  

我很确定我能够将字符串转换为整数作为不同类的对象的方法,所以当我想将一个字符串从位移文本字段转换为int时,我可以调用该对象吗? / p>

实际上,目前形式的代码并不是很优雅。主要是因为它非常重复。有几种方法可以改善这种情况。一步可以是像

这样的方法
private static int parseIntFrom(JTextField textField, String name)
{
    String string = textField.getText().trim();
    try 
    {
        return Integer.parseInt(string);
    }
    catch (NumberFormatException e)
    {
        JOptionPane.showMessageDialog(null,
            "The "+name+" is not a valid integer: "+string);
    }
    return 0;
}

然后,此方法可以替换您的高度重复的解析代码,顺便说一下,通过在输入无效时显示消息来引入“错误处理”的第一步:

// Remove this:
// String timme = TIME.getText();
// int TT = Integer.parseInt(timme);

// Becomes
int TT = parseIntFrom(TIME, "time");

或者,可以使用 Boris the Spider 提到的JFormattedTextField来确保首先只输入有效的整数值。

我假设复杂的if / else结构可以进一步简化,但是必须知道预期的工作流程(以及可能在那里使用的公式)来提供更详细的提示。到目前为止唯一的建议是显而易见的,至少要介绍一些带有“说话”名称的方法,比如

private void calculateDisplacement() { ... }    
private void calculateAcceleration() { ... }
...

actionPerformed方法调用。