Java 当应用处于全屏模式时 运行 Swing 无法识别 JPanel 的问题

Java Swing cannot figure out a problem with JPanel when running the app in full-screen mode

MainPanel class 的构造函数在我 运行 全屏模式下未被访问。只有当我点击菜单栏中的任何项目时才会访问它。

应用程序 运行 在窗口模式下或当我手动设置框架的宽度和高度时都很好。

app running in windowed-mode or when I set width and height (working fine)

app running in full-screen mode but the MainPanel() constructor is not being called

app running in full-screen mode after clicking on any of the menu item (the MainPanel() constructor in called)

Main.java

    public class Main {
        public static void main(String[] args) {
            AppFrame mainFrame = new AppFrame("Algorithm Visualizer");
            mainFrame.add(new MainPanel());
        }
    }

MainPanel.java

    public class MainPanel extends JPanel {
        MainPanel() {
            this.setBackground(Color.BLACK);
        }
    }

AppFrame.java

    class AppFrame extends JFrame implements ActionListener { 
            private JMenuBar menuBar;
            private JMenu fileMenu, sortingAlgoMenu, searchingAlgoMenu;
            private JMenuItem exitItem, bubbleSortItem;
        
            // constructor with frame_title and auto app resolution
            AppFrame(String frameTitle) {
                // sets the app theme
                setTheme();
        
                // Frame Properties
                this.setTitle(frameTitle);
                this.setDefaultCloseOperation(EXIT_ON_CLOSE);
                this.setResizable(false);
        
                GraphicsDevice myDevice = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
            
                // this is the code that makes the frame full-screen
                if(myDevice.isFullScreenSupported()) {
                    this.setUndecorated(true);
                    myDevice.setFullScreenWindow(this);
                }
                else {
                    // windowed mode (title bar present)
                    int deviceWidth = myDevice.getDisplayMode().getWidth();
                    int deviceHeight = myDevice.getDisplayMode().getHeight();
                    
                    this.setSize(deviceWidth, deviceHeight);
                    this.setLocationRelativeTo(null);
                }
        
                // adds the MenuBar
                addMenuBar();
        
                // makes the JFrame visible
                this.setVisible(true);
            }
        
            // constructor passed with app title, width and height
            AppFrame(String frameTitle, int frameWidth, int frameHeight) {
                // sets the app theme
                setTheme();
        
                // Frame Properties
                this.setTitle(frameTitle);
                this.setDefaultCloseOperation(EXIT_ON_CLOSE);
                this.setResizable(false);
                this.setSize(frameWidth, frameHeight);
                this.setLocationRelativeTo(null);
        
                // adds the MenuBar
                addMenuBar();
        
                // makes the JFrame visible
                this.setVisible(true);
            }
        
            // sets the theme of the application
            private static void setTheme() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } 
                catch (ClassNotFoundException e) {
                    e.printStackTrace();
                } 
                catch (InstantiationException e) {
                    e.printStackTrace();
                } 
                catch (IllegalAccessException e) {
                    e.printStackTrace();
                } 
                catch (UnsupportedLookAndFeelException e) {
                    e.printStackTrace();
                }
            }
        
            //  setting the JMenuBar
            private void addMenuBar() {
                menuBar = new JMenuBar();
        
                fileMenu = new JMenu("File");
                menuBar.add(fileMenu);
        
                sortingAlgoMenu = new JMenu("Sorting Algorithms");
                searchingAlgoMenu = new JMenu("Searching Algorithms");
        
                menuBar.add(sortingAlgoMenu);
                menuBar.add(searchingAlgoMenu);
        
                exitItem = new JMenuItem("Exit");
                bubbleSortItem = new JMenuItem("Bubble Sort");
        
                fileMenu.add(exitItem);
                sortingAlgoMenu.add(bubbleSortItem);
        
                // on-click of "Exit" 
                exitItem.addActionListener(this);
        
                this.setJMenuBar(menuBar);
            }
        
            // handle action events (on-click listeners)
            @Override
            public void actionPerformed(ActionEvent e) {
                if(e.getSource() == exitItem)
                    System.exit(0);
            }
        }

只需在应用框架中的 addMenuBar() 之前添加 MainPanel。无论如何都会调用构造函数,但 MainPanel 以其他方式不可见。

我发现我做错了什么。在添加 JPanel 之前,我使 JFrame 可见。所以,我刚刚从 AppFrame class 中删除了框架可见性,并在添加 JPanel 后从 Main.class 设置了它的可见性(true)。