微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

未显示 JFrame 组件

如何解决未显示 JFrame 组件

我正在尝试向 JFrame 添加组件,但是当我运行 GameMenu.java 时,唯一显示的是我的 ImageIcon。我已经实例化了setVisible();,特别是之后我已经设置了我的框架或向面板或菜单添加了组件。所以我不确定为什么没有组件出现。我认为这可能与我的格式或主要方法有关。

这是我的两个类:

GameMenu.java:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.ImageIcon;
import javax.swing.JLabel;

public class GameMenu{

    public static void main(String[] args) {
        FrameCaller obj = new FrameCaller();
    }

}

class FrameCaller extends JFrame {

    public FrameCaller(){
        setLayout(new FlowLayout());
        setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
        add(new JLabel(new ImageIcon("logo.png")));
        pack();
        setLocationRelativeto(null);

        JMenuBar mb = new JMenuBar();
            JMenu m1 = new JMenu("Game List");
            JMenu m2 = new JMenu("Help");
            JMenu m3 = new JMenu("Stats");
                mb.add(m1);
                mb.add(m2);
                mb.add(m3);
        JMenuItem showRulesButton = new JMenuItem("View game rules");
        m2.add(showRulesButton);
        JMenuItem m77 = new JMenuItem("View past game stats");
        m3.add(m77);
        mb.setVisible(true);

        JPanel panel = new JPanel();
            JButton newGameButton = new JButton("New Game");
            newGameButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    new inGameFrame();
                    dispose();
                }
            });
        panel.add(newGameButton);

        panel.setVisible(true);
        setVisible(true);

    }




}

EightOff.java:

import javax.swing.*;



public class EightOff {

    public static void main(String[] args)
    {
        inGameFrame obj = new inGameFrame();
    }
}

    class inGameFrame extends JFrame
    {
        public inGameFrame() {
            setLayout(new FlowLayout());
            setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
            pack();
            setLocationRelativeto(null);
            setVisible(true);

        }


    }

任何提示都会很棒。谢谢。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。