首页 > 解决方案 > 在全屏模式下运行应用程序时,Java Swing 无法找出 JPanel 的问题

问题描述

当我以全屏模式运行应用程序时,未访问MainPanel类的构造函数。只有当我单击菜单栏中的任何项目时才能访问它。

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

应用程序在窗口模式下运行或当我设置宽度和高度时(工作正常)

应用程序以全屏模式运行,但未调用 MainPanel() 构造函数

单击任何菜单项后应用程序以全屏模式运行(调用的 MainPanel() 构造函数)

主.java

    public class Main {
        public static void main(String[] args) {
            AppFrame mainFrame = new AppFrame("Algorithm Visualizer");
            mainFrame.add(new 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);
            }
        }

标签: javaswinguser-interfaceawt

解决方案


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


推荐阅读