I have a Graphical display which creates a new JFrame in its constructor

28 views Asked by At

I am trying to develop a Java app with more JPanels. For now, I have all JPanels in the main constructor Graphics(), and in there, I am adding them all to a JFrame. I want to break the code, and move each container into its own class, but, when I am running that code, it goes over and over again, never stops generating JFrames. Any ideas on how to break my code?

Here is the code:

    public Graphics() {
                    
        //====== GENERAL=========//
        frame = new JFrame();
        frame.setSize(1300, 700);
        frame.setLocationRelativeTo(null); 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
        frame.setVisible(true);
        //====== GENERAL=========//
    
        //====== MENU BAR=========//        
        JMenuBar menuBar = new JMenuBar();
        menuBar.setBounds(0, 0, 1284, 21);
        JMenu menuFile = new JMenu("File");
        menuBar.add(menuFile);
        JMenuItem itemMain = new JMenuItem(new AbstractAction("Main"){
            public void actionPerformed(ActionEvent e) {
                mainPage.setVisible(true);      
            }           
        });
        menuFile.add(itemMain);
        JMenuItem itemLog = new JMenuItem(new AbstractAction("Log off"){
            private static final long serialVersionUID = 1L;
            public void actionPerformed(ActionEvent e) {
                loginPage.setVisible(true);
                mainPage.setVisible(false); 
                passField.setText("");
                lblWrongPassword.setVisible(false);
            }           
        });
        menuFile.add(itemLog);
        JMenu menuHelp = new JMenu("Help");
        menuBar.add(menuHelp);
        JMenuItem menuHelpInfo = new JMenuItem(new AbstractAction("Directions"){
            public void actionPerformed(ActionEvent e) {
                 JOptionPane.showMessageDialog(frame, helpInfo);
        }           
        });
        menuHelp.add(menuHelpInfo);
        frame.setJMenuBar(menuBar);
        //====== MENU BAR=========//    
        
        
        //====== MAIN PAGE=========//
        mainPage = new JPanel();
        mainPage.setBounds(0, 0, 1284, 662);
        frame.getContentPane().add(mainPage);
        mainPage.setLayout(null);
        
0

There are 0 answers