【问题标题】:I'm really having trouble figuring out how to make my GUI window display the values in the array我真的很难弄清楚如何让我的 GUI 窗口显示数组中的值
【发布时间】:2011-06-21 17:15:42
【问题描述】:

如果有人可以将我推向正确的方向,那就太好了。

或者告诉我是否需要重做一个块或其他东西。

这是一个掷骰子的程序。我正在尝试让每个单独的“滚动”显示在 eDieField 中。

这里是类:

import java.util.Random;

public class dice
{
  private int times;
  private int roll;
  private int side;
  public int[] each;
  Random roller = new Random();

  public void setTimes(int rolls)
  {
    times = rolls;
  }

  public void setSides(int die)
  {
    side = die;
  }

  public int getRoll()
  { 
    int[] each = new int[times];
    int total = 0;
    int c = 0;
    int i = 0;
    while (c < times)
    {
      c = c + 1;
      roll = roller.nextInt(side);
      roll = roll + 1;
      each[i] = roll;
      total = total + roll;
      System.out.print(each[i] + " ");
      i = i + 1;
    }
    return total;
  }

  public int getEach()
  {
    return each[/*each number in the array*/];
  }
}

这里是 GUIWindow:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class GUIWindow extends JFrame
{
   private dice dices = new dice();

   private JLabel dice = new JLabel("# of Dice");
   private JLabel sides = new JLabel("# of Sides");
   private JLabel total = new JLabel("Total:");
   private JTextField diceField = new JTextField("");
   private JTextField sideField = new JTextField("");
   private JTextField totField = new JTextField("");
   private JButton button = new JButton ("Roll!");
   private JTextField eDieField = new JTextField("");
   private JLabel empt = new JLabel("Here is each roll");

   // Constructor
   public GUIWindow()
   {
      JPanel dataPanel = new JPanel(new GridLayout(2, 2, 12, 6));
      dataPanel.add(dice);
      dataPanel.add(sides);
      dataPanel.add(diceField);
      dataPanel.add(sideField);
      JPanel rPanel = new JPanel(new GridLayout(1, 3, 12, 6));
      rPanel.add(button);
      rPanel.add(total);
      rPanel.add(totField);      
      JPanel eDiePan = new JPanel(new GridLayout(2, 1, 12, 6));
      eDiePan.add(empt);
      eDiePan.add(eDieField);
      Container container = getContentPane();
      container.add(dataPanel, BorderLayout.WEST);
      container.add(rPanel, BorderLayout.EAST);
      container.add(eDiePan, BorderLayout.SOUTH);
      button.addActionListener(new dieListener());
   }

   // >>>>>>> The controller <<<<<<<<




   private class dieListener implements ActionListener
   {
      public void actionPerformed(ActionEvent e)
      {
        try
        {
          String input = diceField.getText();
          int die = Integer.parseInt(input);
          dices.setTimes(die);
          String inputa = sideField.getText();
          int side = Integer.parseInt(inputa);
          dices.setSides(side);
          int tot = dices.getRoll();
          totField.setText("" + tot);
        }
        catch(Exception ex)
         {
          JOptionPane.showMessageDialog(GUIWindow.this,
                                         "Sorry,\nyou can do that with dice.",
                                         "Dice Fail",
                                         JOptionPane.ERROR_MESSAGE);
         }

        int eachd = dices.getEach();
        eDieField.setText("" + eachd);  
      }
   }
}

还有主要的:

import javax.swing.*;

public class diceRoller
{
   public static void main(String[] args)
   {
      GUIWindow theGUI = new GUIWindow();
      theGUI.setTitle("Dice Roller");
      theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      theGUI.pack();
      theGUI.setVisible(true);
   }
}

我希望这不是不良行为或任何事情,我只是不知道该怎么办。 (而且我愚蠢的教科书没用)假设您输入了 4 个有 4 个面的骰子,如果您用手滚动它们会出现的数字是 2、4、3、4。加上那将是 13。13 进入“totField”和 2 4 3 4 进入“eDieField”。我不能让它工作。我不断收到 nullPointerException 并且我不知道如何保存数组 each[] 以便我可以获取 eDieField 的数字(或其他类似列表之类的东西)。

【问题讨论】:

    标签: java arrays class return


    【解决方案1】:

    您的方法存在一些问题,但导致您的主要问题的是:

    public int getRoll()
      { 
        int[] each = new int[times];
    

    您创建了一个名为“each”的局部变量,但实际上从未在 dice 类中实例化该变量。一旦函数离开其范围,您的本地“每个”就会丢失,从而导致您的空指针错误。你认为你设置了类变量,而实际上你没有。

    去掉每个变量的局部定义(int[]部分)。

    下一部分是您的 getEach 函数。我知道您要做什么,但这不是代码要做的。尝试这样的事情(尽管您可能想将名称更改为 getRollList() 之类的名称):

      public String getEach()
       {
         StringBuffer sb = new StringBuffer();
    
         for (int i : each) {
           sb.append(i).append(",");
         }
    
         return sb.toString();
       }
    

    并将 GUIWindow 类 dieListener actionEvent 更改为:

    String eachd = dices.getEach();
    

    (是的,我会让你找出最后一个逗号,呵呵,你可以用空格代替逗号)。我能够让您的代码处理这些更改。

    您应该更改的其他内容。命名 java 类的一般约定是将类名的第一个字符设为大写字母。 “骰子”类应改为“骰子”。

    【讨论】:

    • 是的!它完全按照我想要的方式工作!非常感谢!
    【解决方案2】:

    您应该将 each 的初始化从

    int[] each = new int[times];
    

    this.each = new int[times];
    

    最好为您的dice 类编写一个构造函数。在构造函数中初始化你的变量以避免NullPointerExceptions 和其他意外使用未初始化的变量:

    public Dice(int times)
    {
        this.times = times;
        this.each = new int[times];
        // ...
    }
    

    还可以尝试更改您的 getEach 方法以返回整数数组。现在它的返回类型只是int

    public int[] getEach()
    {
        return each;
    }
    

    要将整数数组转换为要在eDieField 中显示的字符串,您可以这样做:

    int[] eachDice = dice.getEach();
    
    String eachStr = "";
    for(int d : eachDice)    // for each member of eachDice as d
    {
        eachStr += d + " ";
    }
    eDieField.setText(eachStr);
    

    【讨论】:

      【解决方案3】:

      您可以发布异常文本吗?这样可以更轻松地为您提供帮助。

      让我感到困惑的一件事是,在您的 dice 类中,您有一个全局的 each[],并且您在 getRoll() 中重新声明了它。如果您尝试填充 each[],则应删除内部声明...并且您的返回方法应如下所示:

      public int[] getEach()
        {
          return each;
        }
      

      public int getEach(int pos)
      {
            return each[pos];
      } 
      

      编辑:

      如果您尝试填充每个 [],您应该这样开始 getRoll():

      public int getRoll()
        { 
          each = new int[times];
      

      【讨论】:

        猜你喜欢
        • 2020-11-07
        • 2021-12-30
        • 2020-04-30
        • 1970-01-01
        • 2016-05-01
        • 2011-05-27
        • 1970-01-01
        • 1970-01-01
        • 2014-02-23
        相关资源
        最近更新 更多