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

时间:2013-05-29 05:03:20

标签: java swing text jtextfield bufferedreader

我已经为一个学校项目工作了一段时间的财富游戏,并且遇到了一个问题,从作为谜题的文件中读取文本到作为董事会的JTextField array拼图显示的地方。

到目前为止我所拥有的:运行游戏的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);
}

这个棋盘是拼图显示的地方。我在JButton类中有一个reset wheelGUI,它作为所有其他类运行的程序的主类。

我想要发生的事情:当用户点击JButton reset时,程序应该从文本文件中读取一行,作为本轮的谜题。它应该在板的每个框中放置一个字符,并留空格。它应该把任何一个字母用黑色字母打开,以表示尚未被猜到的字母放在那里。

什么行不通:FileInputStreamBufferedReader似乎与swing组件不兼容,例如JTextField[]。我不确定将文件读入JTextField[]的其他方法,或者甚至可以通过创建板的方式来实现。谢谢!

注意:这是wheelGUI类的代码。其中的其他代码与单独的类有关。

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);
}
}

另外,对我来说,制作拼图的方法是什么类并不重要。我假设把它放在letterBoard类中是最简单的。再次感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

不要试图读取文件的一行,而是将整个文件读成类似java.util.List的内容。

当用户点击“重置”时,只需从列表中选择下一个元素,然后使用循环从String中获取每个字符。

查看this,其中演示了使用String构建文本字段的基本概念,该{{1}}分为单个字符