将选定的字符串从数组数组中的数组打印到JTextField数组

时间:2013-05-30 04:23:01

标签: java arrays swing list jtextfield

经过大量研究和反复试验后,我无法找到如何从String String Array Array中的String Array中随机选择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()是一个内部有谜题的类别,等等)不能使用我,我希望如何。我基本上想要发生的是:方法String获取构造函数中形成的letterBoard,并使用方法findPuzzle()中选择的ActionListener填充它。从那里开始,主要班级wheelGUIJButton reset连接到reset(),当按下时,应该从letterBoard调用puzzles[][]方法然后从棋盘上擦除当前拼图,并用Strings中随机选择的新词填充它,允许新一轮的游戏玩法。对不起,如果这有点......令人困惑。我自己也很困惑。我查看了一些将JTextFields写入wheelGUI的教程,但我的设置略有不同,并且不允许这种方法工作。感谢您抽出宝贵时间来看看这个。如果需要,我会发布{{1}}类代码,但我不认为它包含与此问题相关的任何相关信息。再次感谢。

2 个答案:

答案 0 :(得分:3)

我认为你正在尝试做类似的事情......

public static void reset() {
    findPuzzle();
    field = new ArrayList<>(puzzle.length());
    // Clear the existing values
    for (int index = 0; index < fields.length; index++) {
        fields[index].setText(null);
    }
    // Update the new values
    for (int k = 0; k < puzzle.length(); k++) {
        // Get an individual character from the String and make it a String
        // cause it's easier to deal with...
        String character = Character.toString(puzzle.charAt(k));
        fields[k].setText(character);
        field.add(fields[k]);
    }
}

现在这将显示答案,但我认为你可以解决它......

答案 1 :(得分:0)

使用随机函数随机数并将其用作索引从谜题中获取随机字符串;

 Random r = new Random();
 int ind1 = r.nextInt(puzzles.length);
 int ind2 = r.nextInt(puzzles[0].length);
 //process puzzles[ind1][ind2];