JFrame 上的所有组件都没有显示 [英] All Components on JFrame arent showing

查看:62
本文介绍了JFrame 上的所有组件都没有显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个刽子手游戏,并希望在框架上有 3 个不同的组件、绞刑架的图片、试图猜测的单词以及字母按钮,当我尝试将这些组件添加到 JFrame 时,字符在我想要的地方添加了它们,但是单词出现在左侧并且 HangmanGallows(扩展 JPanel)甚至没有显示,我如何让 HangmanGallows 和单词并排显示在绝对中心?

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    

推荐答案

在构建 UI 后调用 setVisible(true)

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

例如...

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

你还可以找到一些有用的frame.packframe.setLocationByPlatform(true)

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

这篇关于JFrame 上的所有组件都没有显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆