gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 14:31:58 26 4
gpt4 key购买 nike

我为学校项目开发命运之轮游戏已经有一段时间了,在从充当 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 时,程序应该从文本文件中读取一行,该行将作为本轮的谜题。它应该在棋盘的每个框中放置一个字符,并留空空格。它应该将任何有字母占据的盒子变成黑色,以表示有一个尚未被猜测的字母位于那里。

什么不起作用:A FileInputStreamBufferedReader似乎与 swing 不兼容组件,例如 JTextField[] 。我不确定将文件读入 JTextField[] 的另一种方法,或者如果董事会的创建方式可能的话。谢谢!

注意:这是 wheelGUI 的代码类(class)。其中的其他代码与单独的类相关。

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 中。类将是最简单的。再次感谢您的帮助!

最佳答案

不要尝试读取文件的一行,而是将整个文件读入 java.util.List 之类的内容中。

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

看一下 this,它演示了使用分解为各个字符的 String 构建文本字段的基本概念

关于java - 将文件中的文本读取到 JTextField 数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16805860/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com