经过大量的研究和反复试验,我无法找出如何String
从 aString Array
中的Array
a中准确地随机选择一个并将其打印出来,在按下 a后String Array
逐个字符地打印到 a上。我知道这听起来……很尴尬,我不知道该如何表达才能让它听起来不那么尴尬。所以,这是我制作的课程,它使on a .JTextField Array
JButton
JTextField Array
JPanel
package wheelOfFortune;
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.swing.*;
import java.util.List;
import java.util.ArrayList;
public class letterBoard extends JPanel
implements ActionListener
{
private static JTextField[] fields;
private static final String wheelFoods[] = {"PASTRAMI SANDWICH","PEANUT BUTTER","JAR OF PICKLES","NUTELLA SPREAD","SPAGHETTI AND MEATBALLS","BUTTERED LOBSTER","KING CRAB","PIRATES BOOTY","ORANGE JUICE","GRILLED STEAK","MOLTEN LAVA CAKE"};
private static final String wheelHouse[] = {"BURNT OUT LIGHTBULB", "VACUUM CLEANER","DESKTOP COMPUTER", "ANTIQUE FURNITURE","FLATSCREEN TELEVISION","COFFEE TABLE","MAHOGANY DESK","GARDENING SUPPLIES","REMOTE CONTROLLER"};
private static final String wheelMovies[] = {"SAVING PRIVATE RYAN","THE GREAT GATSBY", "ZOOLANDER","ANCHORMAN","STEP BROTHERS","THE DARK NIGHT RISES",
"PULP FICTION","FIGHT CLUB","THE SIXTH SENSE","DJANGO UNCHAINED","SHUTTER ISLAND","THE EVIL DEAD","THE NOTEBOOK","FORREST GUMP","HAPPY GILMORE","THE CHRONICLES OF NARNIA",
"BILL AND TEDS MOST EXCELLENT ADVENTURE","HAROLD AND KUMAR","HARRY POTTER","THE SHAWSHANK REDEMPTION", "THE TERMINATOR", "THE BLUES BROTHERS", "V FOR VENDETTA"};
private static final String wheelPeople[] = {"OPRAH WINFREY","SPONGEBOB SQUAREPANTS","PATRICK STAR", "LEONARDO DICAPRIO","WILL FERRELL", "NEIL ARMSTRONG", "BARACK OBAMA", "JON TRAVOLTA", "ARNOLD SCHWARZENEGGER", "JAMES FRANCIS RYAN", "MS VOLDSTAD",
"BILLY MAYS", "EDWARD SCISSORHANDS","BILL GATES", "STEVE JOBS", "GANDOLF THE GRAY","TWOCHAINZ","RICK ASTLEY"};
private static final String wheelPlaces[] = {"THE EIFFEL TOWER","MOUNT RUSHMORE","THE MOON", "THE GREAT WALL OF CHINA", "FISHERMANS WARF", "DISNEYLAND", "UNDERNEATH A DESK IN A CLASSROOM", "THE GREAT PLAINS", "THE GREAT BARRIER REEF", "AMERISTRALIA", "THE EMPIRE STATE BUILDING",
"THE NORTH POLE", "THE SPACE NEEDLE", "THE KRUSTY KRAB","WEENIE HUT JUNIOR","THE FORBIDDEN PALACE","MORDOR", "NARNIA","THE LAST STALL ON THE LEFT", "INSIDE OF A BOUNCY HOUSE"};
private static final String wheelRandom[] = {"A BOWL OF OATMEAL","LEMON PLEDGE","GREASY HAMBURGERS", "STAINED PANTS", "A CUP OF CHOCOLATE MILK", "ROLLY CHAIRS", "BENDY STRAWS", "WIENER DOGS", "AN ANGRY OSTRICH", "BART SIMPSON","A CONFUSED MOUNTAIN COW",
"BIGFOOT", "THE ILLUMINATI", "THE C HALL BATHROOMS", "A SHAVED BABOON", "THE SPICE GIRLS", "I AM STUPID"};
private Box[] boxes = new Box[SUIT_COUNT];
private static int TEXT_FIELD_COUNT = 14;
private static int SUIT_COUNT = 1;
Color yungMoney = new Color(0, 180, 100);
private static String puzzle;
private static List<JTextField> field;
private static String[][] puzzles = {wheelFoods, wheelHouse, wheelMovies, wheelPeople, wheelPlaces, wheelRandom};
private static String[] puzzless;
private static JPanel panel;
public letterBoard()
{
fields = new JTextField[TEXT_FIELD_COUNT];
setPreferredSize(new Dimension(1,299));
setBackground(yungMoney);
JPanel panel = new JPanel(new GridLayout(0,14));
panel.setBackground(yungMoney);
for(int t=0; t<4; t++)
{
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(" ");
fields[index].setEditable(false);
fields[index].setPreferredSize(new Dimension(50, 50));
fields[index].setBorder(BorderFactory.createLineBorder(Color.BLACK,1));
panel.add(fields[index]);
}
}
}
panel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLACK,2),"WHEEL OF FORTUNE"));
add(panel);
}
public static String findPuzzle()
{
for(int i=0; i<puzzles.length;i++)
{
puzzless = puzzles[(int) Math.round(Math.random() * (puzzles.length-1))];
for(int j=0; j<puzzless.length; j++)
{
puzzle = puzzless[(int) Math.round(Math.random() * (puzzless.length-1))];
}
}
return puzzle;
}
public static void reset()
{
findPuzzle();
field = new ArrayList<>(puzzle.length());
for(int k=0;k<puzzle.length();k++)
{
field.add(fields[k]);
}
}
public void actionPerformed(ActionEvent e)
{
//game logic
}
}
因此,JTextField Array
是在类的构造函数中形成的letterBoard
。构造函数正在运行并按预期工作。但是,String puzzles[][]
它包含了将拼图放入的各种数组,(这是一个命运之轮游戏。每个人array
都是一个里面有拼图的类别,等等)并没有像我希望的那样与我一起工作。我本质上想要发生的是:该方法reset()
采用在构造函数中形成的 letterBoard 并用String
方法中选择的内容填充它findPuzzle()
。从那里,ActionListener
主类中的anwheelGUI
将连接到JButton reset
,当按下时,应该reset()
从letterBoard
上课并从板上擦掉当前的谜题,并用一个新的、随机选择的字符串填充它puzzles[][]
,允许新一轮的游戏玩法。对不起,如果这有点......令人困惑。我自己对此感到困惑。我查看了一些写到Strings
上的教程JTextFields
,但我的设置略有不同,并且不允许这种方法起作用。感谢您花时间看这个。如果需要,我会发布wheelGUI
课程代码,但我认为它没有包含与此问题有关的任何相关信息。再次感谢。