Java:Roller程序-调试-无论有效信息如何,都不会从JTextField和NumberFormatException更新变量
问题: 我无法获取要更新的JTextFields或rollResultTotal的值。 即使JTextFields中的数据有效,仍然会抛出NumberFormatException 问题:为什么变量不能保持不变?这是由于类本身的声明造成的吗?是否可以更新JLabel面板以显示更新的结果?(尝试过这个,史诗般的失败) 提前感谢您的任何输入和示例Java:Roller程序-调试-无论有效信息如何,都不会从JTextField和NumberFormatException更新变量,java,debugging,variables,jtextfield,actionlistener,Java,Debugging,Variables,Jtextfield,Actionlistener,问题: 我无法获取要更新的JTextFields或rollResultTotal的值。 即使JTextFields中的数据有效,仍然会抛出NumberFormatException 问题:为什么变量不能保持不变?这是由于类本身的声明造成的吗?是否可以更新JLabel面板以显示更新的结果?(尝试过这个,史诗般的失败) 提前感谢您的任何输入和示例 /* ITP-120: Final Project Programmer: S. Schnoor Date: N
/*
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) + "!");
}
;}
;}
}
/*
ITP-120:最终项目
程序员:S.Schnoor
日期:2009年11月7日
文件名:SchnoorProject.java
目的:生成“骰子卷”-用户定义的随机值计数,
包括最终计算修改器。
*/
导入java.awt.*;
导入java.awt.event.*;
导入java.util.*;
导入javax.swing.*;
导入java.lang.*;
导入java.math.*;
公共类SchnoorProject扩展JFrame实现ActionListener、EventListener
{
//声明类变量
int sidesToRoll,diceToRoll,diceLeft,修饰符,rollResult,rollResultTotal;
字符串getSides、getDice、getModifier;
JTextField inputSides=新的JTextField();
JTextField inputDice=新的JTextField();
JTextField inputModifier=新的JTextField();
随机滚动;
//面板和按钮
JPanel rollPanel;
JButton newroll按钮、helpButton、exitButton;
//创造色彩
颜色暗色调=新颜色(80,0,80);
颜色暗红色=新颜色(255,215,0);
颜色暗色=新颜色(24,24,24);
深红色=新颜色(127,12,12);
颜色ltSilver=新颜色(140、140、180);
公共静态void main(字符串参数[])
{
JFrame=new SchnoorProject();
WindowListener l=新的WindowAdapter()
{
公共无效窗口关闭(WindowEvent e)
{
系统出口(0);
}
};
frame.addWindowListener(l);
frame.pack();
frame.setVisible(true);
}
公共SchnoorProject()
{
//构件的构造
rollPanel=新的JPanel();
getContentPane().add(rollPanel);
JLabel sidesLabel=新JLabel(“在下面的每个模具上输入侧数(1到99)”;
sidesLabel.setForeground(暗金色);
sidesLabel.setHorizontalAlignment(sidesLabel.CENTER);
JTextField inputSides=新的JTextField(“6”);
输入侧立根背景(暗色);
设置前景(暗金色);
inputSides.setHorizontalAlignment(inputSides.CENTER);
JLabel diceLabel=新的JLabel(“在下方输入要滚动的骰子数量(1到99)”;
diceLabel.setForeground(暗金色);
diceLabel.setHorizontalAlignment(diceLabel.CENTER);
JTextField inputDice=新的JTextField(“3”);
输入设置背景(暗色背景);
inputDice.setForeground(暗金色);
inputDice.setHorizontalAlignment(inputDice.CENTER);
JLabel modifierLabel=新的JLabel(“在下面输入最后一个修饰符。(-99到99)”;
修饰符标签设置前景(暗金色);
modifierLabel.setHorizontalAlignment(modifierLabel.CENTER);
JTextField inputModifier=新的JTextField(“2”);
inputModifier.setBackground(暗色背景);
inputModifier.setForeground(暗金色);
inputModifier.setHorizontalAlignment(inputModifier.CENTER);
JButton newRollButton=新JButton(“ROLL”);
newRollButton.setBackground(暗色背景);
newRollButton.setForeground(暗框);
newRollButton.addActionListener(此);
JButton helpButton=新JButton(“帮助/关于”);
helpButton.setBackground(暗色coal);
帮助按钮。设置前景(银色);
helpButton.addActionListener(此);
JButton exitButton=新JButton(“退出”);
出口按钮。退刀台(暗色);
exitButton.设置前景(深红色);
addActionListener(这个);
//转换
getSides=inputSides.getText();
getDice=inputDice.getText();
getModifier=inputModifier.getText();
sidesToRoll=Integer.parseInt(getSides);
diceToRoll=Integer.parseInt(getDice);
修饰符=Integer.parseInt(getModifier);
diceLeft=diceToRoll;
//rollResultTotal=rollResultTotal;
//镶板
rollPanel.setLayout(新的GridLayout(3,3));
辊道板.立根台(暗坡);
rollPanel.setForeground(暗金色);
添加(侧标签);
添加(标签);
添加(修改标签);
rollPanel.add(输入侧);
rollPanel.add(inputDice);
添加(inputModifier);
rollPanel.add(newRollButton);
rollPanel.add(帮助按钮);
添加(退出按钮);
}
//为多个JButton实现ActionListener
已执行的公共无效操作(操作事件e)
{
字符串arg=e.getActionCommand();
if(“Help/About”.equals(arg))
{
JOptionPane.showMessageDialog(null),“此应用程序根据用户输入的参数生成随机值。\n请选择nu。”
JTextField inputSides = new JTextField(" ");
getSides = inputSides.getText();
sidesToRoll = Integer.parseInt(getSides);
JTextField inputSides = new JTextField("6");
arg == "ROLL"
"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();
}
}
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();
}
}