0
public class Tile extends JLabel{

private char _c;
static char randomChar;

public Tile(char c, Color background) {
    super();
    setBackground(background);
    setOpaque(true);
    _c = c;

}

public static char randomLetter() {
    Random r = new Random();
    randomChar = (char) (97 + r.nextInt(25));
    return randomChar;
    }

public static char getChar(){
    return Tile.randomLetter();
}

public static String convert(){
    char ch = Tile.randomLetter();
    return String.valueOf(ch);


}

public static void main(String[] args) {
    Tile tile = new Tile(Tile.convert(), Color.BLUE);
    Tile tile1 = new Tile(Tile.randomLetter(), Color.RED);
    Tile tile2 = new Tile(Tile.randomLetter(), Color.GREEN);
    Tile tile3 = new Tile(Tile.randomLetter(), Color.YELLOW);

    JFrame frame = new JFrame();
    frame.getContentPane().setLayout(new GridLayout(4,1));
    frame.setSize(500, 800);
    frame.setVisible(true);
    frame.add(tile);
    frame.add(tile1);
    frame.add(tile2);
    frame.add(tile3);

    System.out.println(Tile.convert());

So im trying to make a game to have four tiles and im using Jlabels as my tiles. My tiles takes in a character and a color and since jlabels dont take in characters, im trying to make a method to convert my character to a string and then place it in my jlabel so it would accept it. How do I go about that?

Kisuna97
  • 37
  • 7

1 Answers1

4

Assuming a char variable called myChar, something as simple as

String text = "" + myChar;

// or

String text2 = String.valueOf(myChar);

would work.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373