对于我正在制作的命运之轮游戏,游戏板由 56 s 的数组组成JTextField[]
,并带有将当前拼图打印到 this 上的方法JTextField[]
。我试图做的是在谜题被读到时掩盖这些字母,JTextField[]
这样玩家当然看不到解决方案。我已经尝试Color.BLACK
在程序确定它将要放置什么谜题之后将背景设置为,这在某种程度上起作用了,但是实际的文本变灰了,以至于没有被新背景覆盖,当我尝试将背景设置为灰色,文本当然保持黑色。理想情况下,我希望拼图中的空间不被黑色覆盖。这是我正在寻找的一些视觉效果:
[P] [A] [S] [T] [R] [A] [M] [I] [ ] [S] [A] [N] [D] [W] [I] [C] [H]
如果这就是谜题,那么任何有字母的地方都应该完全涂黑,但空格保持原样。
这是所有这一切发生的类的代码。
package wheelOfFortune;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.List;
import java.util.ArrayList;
import javax.swing.text.html.HTMLEditorKit;
public class letterBoard extends JPanel
implements ActionListener
{
private static int TEXT_FIELD_COUNT = 56;
private static int SUIT_COUNT = 1;
private static JTextField[] fields, p;
private Box[] boxes = new Box[SUIT_COUNT];
private static final String wheelFoods[] = {//bunch 'o' puzzles};
private static final String wheelHouse[] = {//bunch 'o' puzzles};
private static final String wheelMovies[] = {"//bunch 'o' puzzles};
private static final String wheelPeople[] = {//bunch 'o' puzzles};
private static final String wheelPlaces[] = {//bunch 'o' puzzles};
private static final String wheelRandom[] = {//bunch 'o' puzzles};
private Color yungMoney = new Color(0, 180, 100);
private static String puzzle;
private static List<JTextField> field;
private static String[][] puzzles = {wheelFoods, wheelHouse, wheelMovies, wheelPeople, wheelPlaces, wheelRandom};
private static String[] puzzled;
public static String cat;
public letterBoard()
{
fields = new JTextField[TEXT_FIELD_COUNT];
setPreferredSize(new Dimension(1,299));
setBackground(yungMoney);
JPanel panel = new JPanel(new GridLayout(4,14));
panel.setBackground(yungMoney);
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));
fields[index].setFont(new Font("Impact", Font.PLAIN, 24));
fields[index].setHorizontalAlignment(JTextField.CENTER);
panel.add(fields[index]);
}
}
panel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLACK,2),"WHEEL OF FORTUNE"));
add(panel);
}
public static String findPuzzle()
{
for(int i=0; i<puzzles.length;i++)
{
puzzled = puzzles[(int) Math.round(Math.random() * (puzzles.length-1))];
for(int j=0; j<puzzled.length; j++)
{
puzzle = puzzled[(int) Math.round(Math.random() * (puzzled.length-1))];
}
}
return puzzle;
}
public static void reset()
{
findPuzzle();
field = new ArrayList<>(puzzle.length());
for(int index=0; index<fields.length;index++)
{
fields[index].setText(null);
}
for(int k=0;k<puzzle.length();k++)
{
fields[k].setFont(new Font("Impact", Font.PLAIN, 32));
String character = Character.toString(puzzle.charAt(k));
fields[k].setText(character);
field.add(fields[k]);
}
}
public static String getCat()
{
if (puzzled.equals(wheelFoods))
{
cat = "FOOD & DRINK";
}
if(puzzled.equals(wheelHouse))
{
cat = "AROUND THE HOUSE";
}
if(puzzled.equals(wheelMovies))
{
cat = "MOVIE TITLES";
}
if(puzzled.equals(wheelPeople))
{
cat = "PEOPLE";
}
if(puzzled.equals(wheelPlaces))
{
cat = "PLACES AROUND THE WORLD";
}
if(puzzled.equals(wheelRandom))
{
cat = "RANDOM STUFF";
}
return cat;
}
public void actionPerformed(ActionEvent e)
{
//game logic
}
}
在这段代码中:构造函数制作实际的游戏板,findPuzzle()
从实例变量中的谜题数组中随机确定游戏要使用的谜题,reset()
清除当前的棋盘,并在棋盘上打印出一个新的随机选择的谜题,并getCat()
显示该类别放在一个单独的、不相关的JTExtField
. 我需要的是其中带有字母的空格(String character
是一个letterBoard
用于填充JTextField[] fields
拼图的单字符长字符串)完全涂黑,但拼图中的空格保持白色。谢谢!注意:程序通过一个主类运行wheelGUI
,并且该reset()
方法连接到一个JButton
内部wheelGUI
。不要认为发布代码wheelGUI
会有帮助。