我真的有麻烦搞清楚如何使我的GUI窗口中的阵列
˚F有人在显示值可以轻移我这将是伟大正确的方向。我真的有麻烦搞清楚如何使我的GUI窗口中的阵列
或者告诉我,如果我需要重做块什么的。
这是掷骰子的程序。我正在尝试在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,3,4。补充说13是'totField'和2 4 3 4进入'eDieField'。我无法得到那个工作。我不断收到nullPointerException,我不知道如何保持数组每个[],所以我可以得到eDieField(或其他类似于列表或其他)的数字。
有几个问题MS你的方法,但引起你的主要问题之一是:
public int getRoll()
{
int[] each = new int[times];
你创建了一个名为“每一个”局部变量,但你从来没有真正实例化一个在骰子类。一旦函数离开它的范围,你的本地“each”就会丢失,导致你的空指针错误。你认为你没有设置类变量。
摆脱每个变量(INT []部分)的本地定义的。
接下来的部分是你的getEach功能。我明白你在做什么,但这不是代码的功能。尝试这样的事情(尽管你可能需要将名称更改为类似getRollList()):
public String getEach()
{
StringBuffer sb = new StringBuffer();
for (int i : each) {
sb.append(i).append(",");
}
return sb.toString();
}
,改变GUIWindow类dieListener动作事件到:
String eachd = dices.getEach();
(是的,我会留给你找出最后一个逗号,嘿,你可以用空格而不是逗号)。我能够让你的代码适应这些变化。
你应该改变的东西。命名java类的一般惯例是使类名的第一个字符成为大写字母。 “骰子”课应改为“骰子”。
你能发布例外文本吗?这会让你更容易帮助你。
有一件事情显得扑朔迷离对我来说,在你的骰子类,你有一个全球的每一个[]和你再次声明这里面getRoll()。如果你试图填补每一个[]应取下内声明...和你的回报方法应该是这样的:
public int[] getEach()
{
return each;
}
或
public int getEach(int pos)
{
return each[pos];
}
编辑:
这是你如何开始getRoll()如果你试图填充每个[]:
public int getRoll()
{
each = new int[times];
您应该
int[] each = new int[times];
改变你的each
初始化
this.each = new int[times];
这将是写一个构造您dice
类是一个好主意。在构造函数中未初始化的变量,以避免NullPointerException
S和其他意外使用初始化变量:
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);
谢谢谢谢谢谢谢谢谢谢你谢谢你!!!!!!! – Mac 2011-02-08 20:05:17