Question

I'm creating a hangman game and want 3 different components on the frame, a picture of the gallows, the word trying to be guessed, and the buttons for the lettters, when I try adding these components onto the JFrame the characters are added in the spot I want them too, but the word is showing up on the left side and the HangmanGallows(extends JPanel) is not even showing, how would I make the HangmanGallows and word be shown side by side in the absolute center?

public void createGUI() {
    frame = new JFrame("Hangman");
    frame.setSize(500, 500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    selectedWord = new WordBank().getWord(); // initializes 'selectedWord' to a random word selected from WordBank class
    System.out.println(selectedWord);
    displayWord();
    displayCharacterChoices();
} // end method

public void displayWord() {
    String word = "";
    for (int i = 0; i < selectedWord.length(); i++) {
        word += "_  ";
    } // end for loop
    HangmanGallows pic = new HangmanGallows();
    JLabel actualWord = new JLabel(word);
    actualWord.setFont(new Font(Font.DIALOG, Font.BOLD, 40));

    frame.getContentPane().add(BorderLayout.EAST, pic);
    frame.getContentPane().add(BorderLayout.CENTER, actualWord);
} // end method

public void displayCharacterChoices() {
    String[] array = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
    charactersPanel = new JPanel();
    CharacterButtonListener but = new CharacterButtonListener();
    JPanel panelOne = new JPanel();
    JPanel panelTwo = new JPanel();
    JButton temp;

    for (int i = 0; i < array.length / 2; i++) {
        temp = new JButton(array[i]);
        temp.addActionListener(but);
        temp.setText(array[i]);
        panelOne.add(temp);

    } // end for loop
    for (int i = array.length / 2; i < array.length; i++) {
        temp = new JButton(array[i]);
        temp.addActionListener(but);
        temp.setText(array[i]);
        panelTwo.add(temp);
    } // end for loop

    charactersPanel.setLayout(new BoxLayout(charactersPanel, BoxLayout.Y_AXIS));
    charactersPanel.add(panelOne);
    charactersPanel.add(panelTwo);
    frame.add(BorderLayout.SOUTH, charactersPanel);
} // end method    

No correct solution

OTHER TIPS

Call setVisible(true) after you've built the UI

For example...

public void createGUI() {
    frame = new JFrame("Hangman");
    frame.setSize(500, 500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //frame.setVisible(true);
    selectedWord = new WordBank().getWord(); // initializes 'selectedWord' to a random word selected from WordBank class
    System.out.println(selectedWord);
    displayWord();
    displayCharacterChoices();
    frame.setVisible(true);
} // end method

You may also find frame.pack and frame.setLocationByPlatform(true) of some use

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top