首页 > 解决方案 > JFrame 组件未显示

问题描述

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

这是我的两门课:

游戏菜单.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);

    }




}

八关.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);

        }


    }

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

标签: javaswingjframejpaneljmenubar

解决方案


推荐阅读