在我的计算机科学课上,我被分配了一个自由格式的 Java 编码作业,我和我的小组决定制作一个命运之轮类型的游戏。我一直在使用JPanel
and开发 GUI JFrame
,我们的在线学院对此进行了一些详细介绍,所以我知道大多数绝对基础知识。我设法得到了一个不美观但功能齐全的盒子,它在一个名为 的类中显示单个玩家的金钱和姓名playerPlate
,以及一个按钮面板,用于在游戏中的某个时间点在一个名为 的类中显示我们需要的所有按钮buttonPanel
。
在制作以 12-14-14-12 网格显示拼图的字母/拼图板时,我遇到了很多问题。这是我letterBoard
用来尝试制作拼图板的课程的代码。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class letterBoard extends JPanel
implements ActionListener
{
private JTextField a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,
a11,a12,a13,a14,a15,a16,a17,a18,a19,a20,a21,a22,a23,
a24,a25,a26,a27,a28,a29,a30,a31,a32,a33,a34,a35,a36,
a37,a38,a39,a40,a41,a42,a43,a44,a45,a46,a47,a48,a49,
a50,a51,a52;
JTextField board[] = new JTextField[52];
public letterBoard()
{
setBackground(Color.GREEN);
JTextField a1 = new JTextField(" ");
JTextField a2 = new JTextField(" ");
JTextField a3 = new JTextField(" ");
JTextField a4 = new JTextField(" ");
JTextField a5 = new JTextField(" ");
JTextField a6 = new JTextField(" ");
JTextField a7 = new JTextField(" ");
JTextField a8 = new JTextField(" ");
JTextField a9 = new JTextField(" ");
JTextField a10 = new JTextField(" ");
JTextField a11 = new JTextField(" ");
JTextField a12 = new JTextField(" ");
JTextField a13 = new JTextField(" ");
JTextField a14 = new JTextField(" ");
JTextField a15 = new JTextField(" ");
JTextField a16 = new JTextField(" ");
JTextField a17 = new JTextField(" ");
JTextField a18 = new JTextField(" ");
JTextField a19 = new JTextField(" ");
JTextField a20 = new JTextField(" ");
JTextField a21 = new JTextField(" ");
JTextField a22 = new JTextField(" ");
JTextField a23 = new JTextField(" ");
JTextField a24 = new JTextField(" ");
JTextField a25 = new JTextField(" ");
JTextField a26 = new JTextField(" ");
JTextField a27 = new JTextField(" ");
JTextField a28 = new JTextField(" ");
JTextField a29 = new JTextField(" ");
JTextField a30 = new JTextField(" ");
JTextField a31 = new JTextField(" ");
JTextField a32 = new JTextField(" ");
JTextField a33 = new JTextField(" ");
JTextField a34 = new JTextField(" ");
JTextField a35 = new JTextField(" ");
JTextField a36 = new JTextField(" ");
JTextField a37 = new JTextField(" ");
JTextField a38 = new JTextField(" ");
JTextField a39 = new JTextField(" ");
JTextField a40 = new JTextField(" ");
JTextField a41 = new JTextField(" ");
JTextField a42 = new JTextField(" ");
JTextField a43 = new JTextField(" ");
JTextField a44 = new JTextField(" ");
JTextField a45 = new JTextField(" ");
JTextField a46 = new JTextField(" ");
JTextField a47 = new JTextField(" ");
JTextField a48 = new JTextField(" ");
JTextField a49 = new JTextField(" ");
JTextField a50 = new JTextField(" ");
JTextField a51 = new JTextField(" ");
JTextField a52 = new JTextField(" ");
Box b1 = Box.createHorizontalBox();
b1.add(Box.createHorizontalGlue());
b1.add(Box.createHorizontalStrut(5));
b1.add(a1);
b1.add(Box.createHorizontalStrut(5));
b1.add(a2);
b1.add(Box.createHorizontalStrut(5));
b1.add(a3);
b1.add(Box.createHorizontalStrut(5));
b1.add(a4);
b1.add(Box.createHorizontalStrut(5));
b1.add(a5);
b1.add(Box.createHorizontalStrut(5));
b1.add(a6);
b1.add(Box.createHorizontalStrut(5));
b1.add(a7);
b1.add(Box.createHorizontalStrut(5));
b1.add(a8);
b1.add(Box.createHorizontalStrut(5));
b1.add(a9);
b1.add(Box.createHorizontalStrut(5));
b1.add(a10);
b1.add(Box.createHorizontalStrut(5));
b1.add(a11);
b1.add(Box.createHorizontalStrut(5));
b1.add(a12);
b1.add(Box.createHorizontalStrut(5));
b1.add(Box.createHorizontalGlue());
Box b2 = Box.createHorizontalBox();
b2.add(Box.createVerticalStrut(5));
b2.add(a13);
b2.add(Box.createVerticalStrut(5));
b2.add(a14);
b2.add(Box.createVerticalStrut(5));
b2.add(a15);
b2.add(Box.createVerticalStrut(5));
b2.add(a16);
b2.add(Box.createVerticalStrut(5));
b2.add(a17);
b2.add(Box.createVerticalStrut(5));
b2.add(a18);
b2.add(Box.createVerticalStrut(5));
b2.add(a19);
b2.add(Box.createVerticalStrut(5));
b2.add(a20);
b2.add(Box.createVerticalStrut(5));
b2.add(a21);
b2.add(Box.createVerticalStrut(5));
b2.add(a22);
b2.add(Box.createVerticalStrut(5));
b2.add(a23);
b2.add(Box.createVerticalStrut(5));
b2.add(a24);
b2.add(Box.createVerticalStrut(5));
b2.add(a25);
b2.add(Box.createVerticalStrut(5));
b2.add(a26);
b2.add(Box.createVerticalStrut(5));
Box b3 = Box.createHorizontalBox();
b3.add(Box.createVerticalStrut(5));
b3.add(a27);
b3.add(Box.createVerticalStrut(5));
b3.add(a28);
b3.add(Box.createVerticalStrut(5));
b3.add(a29);
b3.add(Box.createVerticalStrut(5));
b3.add(a30);
b3.add(Box.createVerticalStrut(5));
b3.add(a31);
b3.add(Box.createVerticalStrut(5));
b3.add(a32);
b3.add(Box.createVerticalStrut(5));
b3.add(a33);
b3.add(Box.createVerticalStrut(5));
b3.add(a34);
b3.add(Box.createVerticalStrut(5));
b3.add(a35);
b3.add(Box.createVerticalStrut(5));
b3.add(a36);
b3.add(Box.createVerticalStrut(5));
b3.add(a37);
b3.add(Box.createVerticalStrut(5));
b3.add(a38);
b3.add(Box.createVerticalStrut(5));
b3.add(a39);
b3.add(Box.createVerticalStrut(5));
b3.add(a40);
b3.add(Box.createVerticalStrut(5));
Box b4 = Box.createHorizontalBox();
b4.add(Box.createVerticalStrut(5));
b4.add(a41);
b4.add(Box.createVerticalStrut(5));
b4.add(a42);
b4.add(Box.createVerticalStrut(5));
b4.add(a43);
b4.add(Box.createVerticalStrut(5));
b4.add(a44);
b4.add(Box.createVerticalStrut(5));
b4.add(a45);
b4.add(Box.createVerticalStrut(5));
b4.add(a46);
b4.add(Box.createVerticalStrut(5));
b4.add(a47);
b4.add(Box.createVerticalStrut(5));
b4.add(a48);
b4.add(Box.createVerticalStrut(5));
b4.add(a49);
b4.add(Box.createVerticalStrut(5));
b4.add(a50);
b4.add(Box.createVerticalStrut(5));
b4.add(a51);
b4.add(Box.createVerticalStrut(5));
b4.add(a52);
b4.add(Box.createVerticalStrut(5));
add(b1);
add(b2);
add(b3);
add(b4);
}
public void actionPerformed(ActionEvent e)
{
//logic to be added later
}
}
对不起,草率的代码。第一次发布并且在正确格式化时遇到了一些麻烦。当上面的代码在wheelGUI
类中运行时,它实际上创建了 4 个单独的水平段,这些段比我想要的要小得多,可编辑,并且不能正确堆叠。我尝试了不同的框、支柱和边框布局,但始终无法使其正常工作。虽然代码确实可以编译和运行(我知道单独的框之间存在一些一致性错误,忘记重新执行该段),但效率极低,因为必须对大约 52 行不同的代码进行任何更改。我尝试了iterate
一种我制作的方法:
public void iterate(JTextField[] foo)
{
board = foo;
for(int i=0; i<foo.length; i++)
{
foo[i] = new JTextField(" ");
}
}
但是,这导致了NullPointerException
错误,并且没有正确实例化要在框中使用的 a1-52 变量。我能得到的任何帮助都会很棒。如果我的帖子和代码一团糟,我深表歉意。