Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将文件中的文本读取到JTextField数组中_Java_Swing_Text_Jtextfield_Bufferedreader - Fatal编程技术网

Java 将文件中的文本读取到JTextField数组中

Java 将文件中的文本读取到JTextField数组中,java,swing,text,jtextfield,bufferedreader,Java,Swing,Text,Jtextfield,Bufferedreader,我为一个学校项目做了一段时间的幸运轮游戏,遇到了一个问题,从充当拼图的文件中读取文本到充当拼图显示板的JTextField数组中 到目前为止,我拥有的是:一个用于运行游戏的GUI,以及将包含的所有图形组件。这是在类中创建拼图板的代码letterBoard import java.awt.*; import java.awt.event.*; import java.io.BufferedReader; import java.io.FileInputStream; import java

我为一个学校项目做了一段时间的幸运轮游戏,遇到了一个问题,从充当拼图的文件中读取文本到充当拼图显示板的
JTextField数组中

到目前为止,我拥有的是:一个用于运行游戏的GUI,以及将包含的所有图形组件。这是在类中创建拼图板的代码
letterBoard

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.*;

 public class letterBoard extends JPanel
                            implements ActionListener                   
 {
 private JTextField[] fields = new JTextField[TEXT_FIELD_COUNT];
 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);
 static String[] fieldsArray = new String[52];


public letterBoard()
{
    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);
}

而且,制作拼图的方法在哪一类对我来说并不重要。我想把它放在
黑板上是最容易的。再次感谢你的帮助

不要试图读取文件的一行,而是将整个文件读入类似于
java.util.List
的文件中

当用户单击“重置”时,只需从列表中选择下一个元素,并使用循环从
字符串中获取每个字符

看看哪个演示了使用
字符串构建文本字段的基本概念,该字符串被分解为各个字符

import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;
import javax.swing.*;

public class wheelGUI extends JFrame implements ActionListener {
private playerPlate player1, player2, player3;
Color yungMoney = new Color(0, 180, 100);
private String fileName = "D:/Users/Garrett/Workspace/WheelOfFortune/src/wheelOfFortune/img/wheel1.png";
private String cat;
private static String spinValue;
private String[] wheelStuff = new String[]{"Bankrupt", "Lose a Turn", "$5000", "$600", "$500", "$300", "$800", "$550", "$400", "$900", "$350", "$450", "$700"};
private JTextField results;
private static JButton spin, solve, buyVowel, guess, reset, end, cont;
private int numVowel;
private int numLetter;
private static int currentPlayer;
public wheelGUI() {
    super("Butt Stuff!");

    ImageIcon i = new ImageIcon(fileName);
    JLabel picture = new JLabel(i);

    player1 = new playerPlate("Garrett", Color.RED, 0);
    player2 = new playerPlate("Jonny", Color.YELLOW, 1);
    player3 = new playerPlate("Robert", Color.BLUE, 2);

    letterBoard letters = new letterBoard();
    catBox category = new catBox(cat);
    inputField input = new inputField();

    Box wall = Box.createHorizontalBox();
    wall.add(player1);
    wall.add(Box.createHorizontalStrut(5));
    wall.add(player2);
    wall.add(Box.createHorizontalStrut(5));
    wall.add(player3);

    spin = new JButton("Spin!");
    spin.addActionListener(this);
    solve = new JButton("Solve the Puzzle");
    solve.addActionListener(this);
    buyVowel = new JButton("Buy a Vowel");
    buyVowel.addActionListener(this);
    guess = new JButton("Guess a Letter");
    guess.addActionListener(this);
    reset = new JButton("Reset");
    reset.addActionListener(this);
    cont = new JButton("Continue");
    cont.addActionListener(this);

    JPanel buttonPanel = new JPanel(new GridLayout(3, 1, 5, 5));
    buttonPanel.setPreferredSize(new Dimension(300,380));
    buttonPanel.setBackground(yungMoney);
    buttonPanel.add(spin);
    buttonPanel.add(guess);
    buttonPanel.add(buyVowel);
    buttonPanel.add(solve);
    buttonPanel.add(cont);
    buttonPanel.add(reset);

    JPanel result = new JPanel();
    result.setBackground(yungMoney);
    results = new JTextField(spinValue);
    results.setBackground(Color.CYAN);
    results.setHorizontalAlignment(JTextField.CENTER);
    results.setBorder(BorderFactory.createLineBorder(Color.BLACK,2));
    results.setPreferredSize(new Dimension(150,100));
    results.setFont(new Font("Impact", Font.PLAIN, 28));
    results.setEditable(false);
    result.add(results);

    Box catInput = Box.createVerticalBox();
    catInput.add(category);
    catInput.add(Box.createVerticalStrut(50));
    catInput.add(result);
    catInput.add(Box.createVerticalStrut(100));
    catInput.add(input);

    Container c = getContentPane();
    c.setBackground(yungMoney);
    c.add(buttonPanel, BorderLayout.EAST);
    c.add(wall, BorderLayout.SOUTH);
    c.add(letters, BorderLayout.NORTH);
    c.add(picture, BorderLayout.WEST);
    c.add(catInput, BorderLayout.CENTER);
}
public JTextField spinWheel(String[] wheelStuff)
{
    Random rnd = new Random();
    spinValue = wheelStuff[rnd.nextInt(wheelStuff.length)];
    results.setText(spinValue);
    return results;
}
public void actionPerformed(ActionEvent e) 
{
    JButton b = (JButton)e.getSource();
    if(b==spin)
    {
        spinWheel(wheelStuff);
        repaint();
    }
}
public static void main(String[] args) {
    wheelGUI window = new wheelGUI();
    window.setBounds(50, 50, 1024, 768);
    window.setDefaultCloseOperation(EXIT_ON_CLOSE);
    window.setResizable(false);
    window.setVisible(true);
}
}