使用JPanel / JFrame Java创建一个Fortune-esque拼图板轮

时间:2013-05-14 02:14:48

标签: java swing jframe jpanel jtextfield

在我的计算机科学课中,我被分配了一个自由形式的Java编码任务,我和我的小组决定制作一个财富之轮类型的游戏。我一直在使用JPanelJFrame进行GUI工作,我们的在线学院对此进行了详细介绍,因此我了解了大部分绝对基础知识。我设法得到一个非审美但功能齐全的盒子,在一个名为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变量。我能得到的任何帮助都会很棒。如果我的帖子和我的代码混乱,我道歉。

1 个答案:

答案 0 :(得分:2)

如果您需要,您应该拥有一个JTextField数组和一个Box或JPanel容器数组。将它们组合在一个for循环中。

private JTextField[] fields = new JTextField[TEXT_FIELD_COUNT]; // const = 52
private Box[] boxes = new Box[SUIT_COUNT];  // suit count = 4

for (int i = 0; i < boxes.length; i++) {
  boxes[i] = Box.createHorizontalBox();
  for (int j = 0; j < TEXT_FIELD_COUNT/SUIT_COUNT; j++) {
    int index = i * (TEXT_FIELD_COUNT / SUIT_COUNT) + j;
    fields[index] = new JTextField(COLS); // COLS = some const, possibly 10
    boxes[i].add(fields[index]); 
    // add your strut if desired
  }
}