将JTextField写入JTextField []时将其涂黑

时间:2013-05-31 01:36:54

标签: java arrays string swing jtextfield

对于我正在制作的财富游戏,游戏板由56 JTextField[]个数组组成,其方法可将当前拼图打印到此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的代码会有所帮助。

0 个答案:

没有答案