当标准未满足时,Java抛出异常

时间:2014-04-29 20:01:29

标签: java exception

我在上周左右一直在研究这个程序,并且让这个特定的代码块在不久前运行顺利。我根本没有改变它,突然它开始为按下按钮时选择的每个选项抛出异常。我通过放置一个“System.out.println(activityMultiplier);”为每个选项测试了它。每个块中的语句,它打印正确的值,因此项侦听器似乎正在工作。当我选择“选择一个活动级别”(应该抛出异常的optin)时,异常被正确抛出:一旦我点击好了,JOptionPane就会消失,而JComboBox仍然存在,所以我可以进行另一个选择。但是,我现在收到了每个选项的JOptionPane错误消息,我在底部包含了noActivity()方法,这样你就可以看到我正在调用的内容。

public void setActivity() //JComboBox
{
    final JFrame activityWindow = new JFrame();
    activityWindow.setTitle("Question No 6 of 6");
    activityWindow.setLocationRelativeTo(null);

    JPanel activityPanel = new JPanel();
    activityPanel.setLayout(new BorderLayout(10, 10));
    activityPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

    JLabel activityLabel = new JLabel("Please select your activity level:");
    final JComboBox activityList = new JComboBox(activityString);

    activityList.setSelectedIndex(5);
    activityList.addItemListener(new ItemListener()
    {
        public void itemStateChanged(ItemEvent e)
        {
            activityLevel = (String) activityList.getSelectedItem();
        }
    });

    JButton submitBtn = new JButton("Submit");
    submitBtn.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            try
            {
                if (activityLevel.equals("Choose an activity level"))
                    throw new Exception();

                else if (activityLevel.equals("Sedentary (little or no exercise)"))
                {
                    activityMultiplier = 1.2;
                    activityWindow.dispose();
                    setBMR();
                }
                else if (activityLevel.equals("Lightly active (light exercise/sports 1-3 days/week)"))
                {
                    activityMultiplier = 1.375;
                    activityWindow.dispose();
                    System.out.println(activityMultiplier);
                    setBMR();
                }
                else if (activityLevel.equals("Moderatetely active (moderate exercise/sports 3-5 days/week)"))
                {
                    activityMultiplier = 1.55;
                    activityWindow.dispose();
                    System.out.println(activityMultiplier);
                    setBMR();
                }
                else if (activityLevel.equals("Very active (hard exercise/sports 6-7 days a week"))
                {
                    activityMultiplier = 1.725;
                    activityWindow.dispose();
                    System.out.println(activityMultiplier);
                    setBMR();
                }
                else if (activityLevel.equals("Extra active (very hard exercise/sports & physical job or 2x/day training)"))
                {   activityMultiplier = 1.9;
                    activityWindow.dispose();
                    System.out.println(activityMultiplier);
                    setBMR();
                }

            }
            catch(Exception e1)
            {
                noActivity();
            }
        }
    });


    private void noActivity()
    {
        JOptionPane.showMessageDialog(null, "Please select an activity level.", "No Selection Made", JOptionPane.ERROR_MESSAGE);
        JOptionPane.getRootFrame().dispose();
    }

1 个答案:

答案 0 :(得分:0)

为什么不简单地将其更改为:

 if (activityLevel.equals("Choose an activity level"))
          noActivity();

其他选项在其他地方,无论如何阻止 我认为这不适用于例外