swing - Creating a Wheel of Fortune-esque puzzle board using JPanel/JFrame Java -
i assigned free-form java coding assignment in computer science class in group , decided make wheel of fortune type game. i've been working on gui using jpanel
and jframe
, our online academy goes detail on, know of absolute basics. managed non-aesthetic functioning box displays individual player money , name in class named playerplate
, button panel buttons need @ point during game in class called buttonpanel
.
when came making letter/puzzle board puzzle displayed on 12-14-14-12 grid, experienced slew of problems. here code letterboard
class i'm using try make puzzle board.
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 added later } }
sorry sloppy code. first time posting , having bit pf trouble getting format correctly. when above code run through wheelgui
class, creates 4 separate horizontal segments smaller like, editable, , not stacking correctly. played around different boxes, struts, , border layouts never work properly. , while code compile , run (i'm aware of consistency errors between separate boxes, forgot re-do segment) extremely inefficient, changes must done ~52 different lines of code. experimented iterate
method made:
public void iterate(jtextfield[] foo) { board = foo; for(int i=0; i<foo.length; i++) { foo[i] = new jtextfield(" "); } }
however, resulted in nullpointerexception
error , did not instantiate a1-52 variables used in boxes. great. apologize if post , code mess.
you should have array of jtextfields , array of box or jpanel containers if need. combine them in loop.
private jtextfield[] fields = new jtextfield[text_field_count]; // const = 52 private box[] boxes = new box[suit_count]; // suit count = 4 (int = 0; < boxes.length; i++) { boxes[i] = box.createhorizontalbox(); (int j = 0; j < text_field_count/suit_count; j++) { int index = * (text_field_count / suit_count) + j; fields[index] = new jtextfield(cols); // cols = const, possibly 10 boxes[i].add(fields[index]); // add strut if desired } }
Comments
Post a Comment