簡體   English   中英

我真的很難弄清楚如何使我的GUI窗口顯示數組中的值

[英]I'm really having trouble figuring out how to make my GUI window display the values in the array

如果有人可以向正確的方向推動我,那將是很棒的。

或告訴我是否需要重做一些內容。

這是擲骰子的程序。 我正在嘗試在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。在“ totField”中輸入13和2 4 3 4進入“ eDieField”。 我不能讓它工作。 我一直在獲取nullPointerException,並且我不知道如何保持數組each [],因此我可以獲取eDieField的數字(或諸如列表之類的東西)。

您的方法存在一些問題,但是導致您的主要問題的是:

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類的常規約定是將類名稱的第一個字符大寫。 “骰子”類應更改為“骰子”。

您可以張貼例外文字嗎? 這樣可以更輕松地為您提供幫助。

對我來說似乎令人困惑的一件事是,在您的骰子類中,您有一個全局each [],然后在getRoll()中重新聲明了它。 如果您要填充each [],則應刪除內部聲明...,並且return方法應如下所示:

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

要么

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

編輯:

如果您要填充每個[],則應該以這種方式開始getRoll():

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

您應該更改each的初始化

int[] each = new int[times];

this.each = new int[times];

為您的dice類編寫一個構造函數是一個好主意。 在構造函數中初始化變量,以避免NullPointerException以及其他意外使用未初始化變量的情況:

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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM