0

我一直在为一个学校项目开发命运之轮游戏一段时间,并且遇到了从充当拼图的文件中读取文本到充当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 resetwheelGUI,它充当所有其他类运行的程序的主类。

我想要发生的事情:当用户单击 时JButton reset,程序应该从文本文件中读取一行,这将作为这一轮的拼图。它应该在棋盘的每个框中放置一个字符,并将空格留空。它应该将任何一个字母占据它的盒子变成黑色,以表示一个尚未被猜到的字母被放置在那里。

什么不工作: AFileInputStream并且BufferedReader似乎与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类中是最简单的。再次感谢您的帮助!

4

1 回答 1

1

与其尝试读取文件的一行,不如将整个文件读入类似java.util.List.

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

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

于 2013-05-29T05:12:49.953 回答