[英]Reading text from a file onto a JTextField array
I've been working on a wheel of fortune game for a school project for a while now, and ran into a problem with reading text from files that act as the puzzles onto a JTextField array
which acts as the board where the puzzle is displayed. 我一直在为学校项目开发运气游戏轮,有一段时间,我遇到了一个问题,即从充当谜题的文件中读取文本到
JTextField array
,该JTextField array
充当谜题的显示板。
What I have so far: A GUI for the game to run, as well as all the graphical components that will be included. 到目前为止,我所拥有的:用于运行游戏的GUI,以及将包含的所有图形组件。 This is the code for creating the puzzle board within the class
letterBoard
这是用于在
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);
}
This board is where the puzzle will be displayed. 该板将显示拼图。 I have a
JButton
reset
whithin the wheelGUI
class, which acts as the main class for the program that all other classes run through. 我在
wheelGUI
类中有一个JButton
reset
,它作为所有其他类运行的程序的主类。
What I want to happen: Upon a user clicking the JButton reset
, the program should read a line from a text file which will act as the puzzle for this round. 我要发生的事情:用户单击
JButton reset
,程序应从文本文件中读取一行,这将成为这一轮的难题。 It should put one character in each box of the board, and leave spaces blank. 它应该在面板的每个框中放入一个字符,并留出空白。 It should turn any box with a letter occupying it black, to signify that a letter that has yet to be guessed is positioned there.
它应将带有字母的任何框都变成黑色,以表示尚未猜测的字母位于该框内。
What's not working: A FileInputStream
and BufferedReader
don't seem to be compatible with swing
components such as a JTextField[]
. 什么不起作用:
FileInputStream
和BufferedReader
似乎与swing
组件(例如JTextField[]
不兼容。 I'm not sure of another way to read a file into a JTextField[]
, or if it's even possible with the way the board is created. 我不确定将文件读入
JTextField[]
的另一种方法,或者用创建板的方式甚至不可能。 Thank you! 谢谢!
Note: Here is the code for the wheelGUI
class. 注意:这是
wheelGUI
类的代码。 The other code in there is related to separate classes. 那里的其他代码与单独的类有关。
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);
}
}
Also, it doesn't really matter to me what class the method to make the puzzle is in. I'd assume putting it in the letterBoard
class would be the easiest. 另外,对我来说,制作难题的方法在哪个类中并不重要。我认为将其放在
letterBoard
类中是最简单的。 Thanks again for the help! 再次感谢您的帮助!
Instead of trying to read a line of the file, read the entire file into something like a java.util.List
. 与其尝试读取文件的一行,不如将整个文件读取为
java.util.List
。
When the user clicks "reset", simply select the next element from the list and use a loop to get each character from the String
. 当用户单击“重置”时,只需从列表中选择下一个元素,然后使用循环从
String
获取每个字符。
Take a look at this which demonstrates the basic concept of building the text fields using a String
which is broken down into it's individual characters 看看这个这表明用构建文本字段的基本概念
String
被分解为它的单个字符
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.