2009-12-14 22 views
0

问题: 我无法获取JTextFields或rollResultTotal的值更新。 即使JTextFields中的数据有效,NumberFormatException仍然会被抛出。Java:滚动程序 - 调试 - 不从有效的信息抛出JTextField和NumberFormatException抛出异常

问题:为什么变量不保留?这是由于课堂本身的声明吗?是否可以更新JLabel面板以显示更新的结果? (试过这个,史诗般的失败)

在此先感谢您的任何输入和示例。

/* 
    ITP-120:  Final Project 
    Programmer: S. Schnoor 
    Date:  November 7th, 2009 
    Filename: SchnoorProject.java 
    Purpose:  To generate "dice rolls" - a tally of random values as defined by the user, 
       including a final calculation modifier. 
*/ 

import java.awt.*; 
import java.awt.event.*; 
import java.util.*; 
import javax.swing.*; 
import java.lang.*; 
import java.math.*; 

public class SchnoorProject extends JFrame implements ActionListener, EventListener 
{ 
    // Declare Class Variables 
    int sidesToRoll, diceToRoll, diceLeft, modifier, rollResult, rollResultTotal; 
    String getSides, getDice, getModifier; 
    JTextField inputSides  = new JTextField(); 
    JTextField inputDice  = new JTextField(); 
    JTextField inputModifier = new JTextField(); 
    Random roll; 

    // Panels and Buttons 
    JPanel rollPanel; 
    JButton newRollButton, helpButton, exitButton; 

    // Create Colors 
    Color darkPurple  = new Color (80, 0, 80); 
    Color darkGold   = new Color (255, 215, 0); 
    Color darkCoal   = new Color (24, 24, 24); 
    Color crimson   = new Color (127, 12, 12); 
    Color ltSilver   = new Color (140, 140, 180); 

    public static void main(String args[]) 
    { 
     JFrame frame = new SchnoorProject(); 

     WindowListener l = new WindowAdapter() 
      { 
       public void windowClosing(WindowEvent e) 
       { 
        System.exit(0); 
       } 
      }; 

    frame.addWindowListener(l); 
    frame.pack(); 
    frame.setVisible(true); 

    } 

    public SchnoorProject() 
    { 
     // Construction of Components 
     rollPanel       = new JPanel(); 
     getContentPane().add(rollPanel); 


     JLabel  sidesLabel    = new JLabel ("Enter the number of sides on each die below. (1 to 99)"); 
        sidesLabel.setForeground(darkGold); 
        sidesLabel.setHorizontalAlignment(sidesLabel.CENTER); 

     JTextField inputSides    = new JTextField("6"); 
        inputSides.setBackground(darkCoal); 
        inputSides.setForeground(darkGold); 
        inputSides.setHorizontalAlignment(inputSides.CENTER); 

     JLabel  diceLabel    = new JLabel ("Enter the number of dice to roll below. (1 to 99)"); 
        diceLabel.setForeground(darkGold); 
        diceLabel.setHorizontalAlignment(diceLabel.CENTER); 

     JTextField inputDice    = new JTextField("3"); 
        inputDice.setBackground(darkCoal); 
        inputDice.setForeground(darkGold); 
        inputDice.setHorizontalAlignment(inputDice.CENTER); 

     JLabel  modifierLabel   = new JLabel ("Enter the final modifier below. (-99 to 99)"); 
        modifierLabel.setForeground(darkGold); 
        modifierLabel.setHorizontalAlignment(modifierLabel.CENTER); 

     JTextField inputModifier   = new JTextField("2"); 
        inputModifier.setBackground(darkCoal); 
        inputModifier.setForeground(darkGold); 
        inputModifier.setHorizontalAlignment(inputModifier.CENTER); 

     JButton  newRollButton   = new JButton("ROLL"); 
        newRollButton.setBackground(darkCoal); 
        newRollButton.setForeground(darkGold); 
        newRollButton.addActionListener(this); 

     JButton  helpButton    = new JButton("Help/About"); 
        helpButton.setBackground(darkCoal); 
        helpButton.setForeground(ltSilver); 
        helpButton.addActionListener(this); 

     JButton  exitButton    = new JButton("Exit"); 
        exitButton.setBackground(darkCoal); 
        exitButton.setForeground(crimson); 
        exitButton.addActionListener(this); 

     //Conversions 
        getSides    = inputSides.getText(); 
        getDice     = inputDice.getText(); 
        getModifier    = inputModifier.getText(); 
        sidesToRoll    = Integer.parseInt(getSides); 
        diceToRoll    = Integer.parseInt(getDice); 
        modifier    = Integer.parseInt(getModifier); 
        diceLeft    = diceToRoll; 
        //rollResultTotal  = rollResultTotal; 

     // Paneling 
     rollPanel.setLayout(new GridLayout (3,3)); 
     rollPanel.setBackground(darkPurple); 
     rollPanel.setForeground(darkGold); 
     rollPanel.add(sidesLabel); 
     rollPanel.add(diceLabel); 
     rollPanel.add(modifierLabel); 
     rollPanel.add(inputSides); 
     rollPanel.add(inputDice); 
     rollPanel.add(inputModifier); 
     rollPanel.add(newRollButton); 
     rollPanel.add(helpButton); 
     rollPanel.add(exitButton); 
    } 

    // Implement ActionListener for multiple JButtons 
    public void actionPerformed(ActionEvent e) 
    { 
     String arg = e.getActionCommand(); 
     if ("Help/About".equals(arg)) 
      { 
       JOptionPane.showMessageDialog(null,"This application generates random values, based on the parameters input by the user.\nPlease choose number of dice, sides on each die, and a +/- roll modifier.\nIf no roll modifier is needed, enter 0 (numeric zero) in the modifier field.\n(The starting numbers tell the program to roll 3 six-sided dice and add 2 or '3d6+2'.)","Help/About",JOptionPane.INFORMATION_MESSAGE); 
      } 

     if ("Exit".equals(arg)) 
      { 
       System.exit(0); 
      } 

     if ("ROLL".equals(arg)) 

       { 
        try 
        { 
         while (diceLeft>0) 
         { 
          getSides     = inputSides.getText(); 
          sidesToRoll     = Integer.parseInt(getSides); 
          if(sidesToRoll<1 || sidesToRoll>99)  throw new NumberFormatException(); 
          else 

          getDice      = inputDice.getText(); 
          diceToRoll     = Integer.parseInt(getDice); 
          if(diceToRoll<1 || diceToRoll>99)  throw new NumberFormatException(); 
          else 

          getModifier     = inputModifier.getText(); 
          modifier     = Integer.parseInt(getModifier); 
          if(modifier<-99 || modifier>99)   throw new NumberFormatException(); 
          else 

         rollResult     = roll.nextInt(sidesToRoll)+1; 
         rollResultTotal    = rollResultTotal + rollResult; 
         diceLeft--; 
         } 

        } 


        catch(NumberFormatException ex) 
        { 
         JOptionPane.showMessageDialog(null,"You must enter an integer within the given range of each field.","ROLL",JOptionPane.INFORMATION_MESSAGE); 
         diceLeft = 0; 
        } 


        { //Display the Roll Formula and Result 
        JOptionPane.showMessageDialog(null,"You entered a roll of " + (diceToRoll) + "d" + (sidesToRoll) + "+(" + (modifier) + ").\nYou rolled " + (rollResultTotal) + "!"); 
        } 
     ;} 
    ;} 
} 
+0

我不知道如何格式化上面的代码,好看的观众。我道歉。 – 2009-12-14 08:29:20

+0

选择代码并按ctrl + k(已经为你做过) – Bozho 2009-12-14 08:30:38

+0

对于那些关心学术道德的人,我已经在周末完成了这门课。现在,这全是关于个人知识。 – 2009-12-14 20:25:06

回答

1

您初始化该成员变量,但是你从来没有这个字段添加到UI:

JTextField inputSides = new JTextField(" "); 

这里你仍然包含字符串" ",这不是整数这个文本字段读取。

getSides = inputSides.getText(); 
sidesToRoll = Integer.parseInt(getSides); 

当你建立在构造你的用户界面,你声明与另一同名,局部变量,指向一个新的对象:

JTextField inputSides = new JTextField("6"); 

这是出现在用户界面中的对象。局部变量inputSides隐藏相同名称的成员变量。


顺便说一句,这是不是比较字符串的正确方法:

arg == "ROLL" 

这仅适用于因为arg指向相同,实习对象实例。比较这些值的正确方法应该是:

"ROLL".equals(arg) 

编辑:

你缺少的诀窍是在考虑要创建和哪些参考变量指向的对象。

考虑你的代码的这种简化版本:创建

public class ObjectReferences { 
    private final JLabel label = new JLabel("I am FOO"); // label 1 

    public ObjectReferences() { 
    JLabel label = new JLabel("I am BAR"); // label 2 

    JButton button = new JButton("Click me"); 
    button.addActionListener(new ActionListener() { 
     @Override public void actionPerformed(ActionEvent e) { 
     onclick(); 
     } 
    }); 

    // will display "I am BAR" 
    JFrame frame = new JFrame(); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setSize(600, 400); 
    frame.setLayout(new FlowLayout()); 
    frame.add(label); // label 2 
    frame.add(button); 
    frame.setVisible(true); 
    } 

    public void onclick() { 
    // will display "I am FOO" 
    JOptionPane.showMessageDialog(null, label.getText()); // label 1 
    } 

    public static void main(String[] args) { 
    new ObjectReferences(); 
    } 
} 

两个标签对象,但只有一个被添加到帧。 label 1被成员变量引用,这是onclick()方法可见的那个。在构造函数中,标签2被称为label的局部变量引用,该变量隐藏名为label的成员变量。

你的意图是写这样的事情,而不是:

public class ObjectReferences { 
    private final JLabel label = new JLabel("I am FOO"); 

    public ObjectReferences() { 
    JButton button = new JButton("Click me"); 
    button.addActionListener(new ActionListener() { 
     @Override public void actionPerformed(ActionEvent e) { 
     onclick(); 
     } 
    }); 

    JFrame frame = new JFrame(); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setSize(600, 400); 
    frame.setLayout(new FlowLayout()); 
    frame.add(label); 
    frame.add(button); 
    frame.setVisible(true); 
    } 

    public void onclick() { 
    JOptionPane.showMessageDialog(null, label.getText()); 
    } 

    public static void main(String[] args) { 
    new ObjectReferences(); 
    } 
} 
+0

我很欣赏迅速的回应。我能够使用“ROLL”.equals(arg)以及Exit和Help/About按钮切换arg ==“ROLL”。我还清理了上面的代码以反映这一变化,并删除不必要的代码。尽管如此,还是没有弄清楚。 – 2009-12-14 12:40:53

+0

我编辑了我的答案,希望问题更清楚。 _另外,请注意,您尚未初始化您的'roll'成员,因此当您尝试使用它时,您将收到NullPointerException。_ – McDowell 2009-12-14 16:44:45

+0

这开始有意义。我会继续努力的。 :) – 2009-12-14 20:26:33