How do I fix this bug in Java’s JFrame?

I have been making a “Cookie Clicker” sort of game, where you click cake and it gets you cakes. However, this program has been having some bugs, like when I make the game class, the text variable doesn’t show up. Can anyone help?

public class Game extends JPanel {
        private double numClicks = 0;
        public JLabel text;
        public JLabel hoverText;
        ImageIcon cakeImage = new ImageIcon("cake image location");
        public JButton cakeButton;
        public JPanel counterPanel;

        public Game(Window frame //my jframe (look below)) {
                
                counterPanel = new JPanel();
                counterPanel.setBounds(700, 100, 400, 200);
                counterPanel.setLayout(new BorderLayout());
                counterPanel.setBorder(BorderFactory.createLineBorder(Color.blue, 10));
            
                //make it easier to move the parts around
                setLayout(new BorderLayout());
                
                this.setPreferredSize(new Dimension(1000, 750));
                
                cakeButton = new JButton(cakeImage);
                
                cakeButton.setBounds(100, 100,300,300);
                cakeButton.setBackground(Color.decode("#FFFFFF"));
                cakeButton.setBorder(BorderFactory.createLineBorder(Color.black, 5));
                cakeButton.setFocusable(false);
                cakeButton.setOpaque(false);
                cakeButton.setContentAreaFilled(false);
                //cakeButton.setBorderPainted(false);
                cakeButton.setVisible(true);
                add(cakeButton);
                      
                //make the cake counter
                text = new JLabel("Cakes: 0     "); 
                text.setVerticalAlignment(SwingConstants.TOP);
                text.setHorizontalAlignment(SwingConstants.CENTER);
                text.setFont(new Font("Courier", Font.BOLD,75));
                text.setText("Cakes: 0     ");
                text.setSize(text.getPreferredSize());
                //text.setLocation(500/*50 + (frameLength * 1/2)*/, 100);
                counterPanel.add(text);
                text.setVisible(true);
                
                
                //hover text for the cake button
                hoverText = new JLabel("Cakes: 0     "); 
                hoverText.setVerticalAlignment(SwingConstants.BOTTOM);
                hoverText.setHorizontalAlignment(SwingConstants.CENTER);
                hoverText.setFont(new Font("Courier", Font.BOLD,75));
                hoverText.setText("Cakes: 0     ");
                hoverText.setSize(hoverText.getPreferredSize());
                counterPanel.add(hoverText);
                hoverText.setVisible(false);
                
                frame.add(counterPanel);
                
                //add the action listener to the cake button
                cakeButton.addActionListener( new ActionListener()
                {  
                    public void actionPerformed(ActionEvent e)
                    {  
                        numClicks++;
                        int convClicks = (int) Math.round(numClicks);
                        String texty = "Cakes: " + convClicks + "     ";
                        text.setText(texty);
                    }  
                }
                );
               
                text.addMouseListener( new MouseListener()
                {  
                    
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        // TODO Auto-generated method stub
                        hoverText.setVisible(true);
                        hoverText.setText("Text");
                        System.out.println("hi");
                        
                    }

                    @Override
                    public void mousePressed(MouseEvent e) {
                        // TODO Auto-generated method stub
                        
                    }

                    @Override
                    public void mouseReleased(MouseEvent e) {
                        // TODO Auto-generated method stub
                        
                    }

                    @Override
                    public void mouseEntered(MouseEvent e) {
                        // TODO Auto-generated method stub
                    }

                    @Override
                    public void mouseExited(MouseEvent e) {
                        // TODO Auto-generated method stub
                        
                    }  
                }
                );
        }

}

Here is my Jframe:

public class Window extends JFrame implements KeyListener, WindowListener, MouseListener {   

    JFrame frame;
    JLabel textLabel;
    Game game;
    MainMenu mainMenu;
    
    public Window(String title) {    
        
        setTitle(title);
        
        //Create and set up the window.            
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        
        setLayout(new BorderLayout());
        
        
        addKeyListener(this);
        addMouseListener(this);
        
        
        //Display the window.
        setSize(1000, 750);
        setLocationRelativeTo(null);       
        pack();
        //frame.setLayout(null);
        
        setVisible(true); 
        
        mainMenu = new MainMenu(this);
        add(mainMenu);
        
        setSize(1000, 750); 
        
        //game = new Game(this);
        
    }
    
    public Game mainMessage(int type) {
        if (type == 0) {
            mainMenu.removeMenu();
            
            game = new Game(this);
            add(game);
            game.setVisible(true);
            game.text.setVisible(true);
            SwingUtilities.updateComponentTreeUI(this);
            return game;
            
        }
        return null;
    }
 

    @Override
    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void keyPressed(KeyEvent e) {
        // TODO Auto-generated method stub
        SwingUtilities.updateComponentTreeUI(this);
        Main.receiveKey(e);
        
    }

    @Override
    public void keyReleased(KeyEvent e) {
        // TODO Auto-generated method stub
        
    }
    
    public void windowOpened(WindowEvent e) {}
    public void windowActivated(WindowEvent e) {}
    public void windowIconified(WindowEvent e) {}
    public void windowDeiconified(WindowEvent e) {}
    public void windowDeactivated(WindowEvent e) {}
    public void windowClosed(WindowEvent e) {}

    public void windowClosing(WindowEvent e) {
        dispose();
        System.exit(0);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub
        main.Main.receiveMouse(e);
    }

    @Override
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub
        
    }
    
  
}

I tried making a new panel and using that, but it didn’t work and just glitched out

New contributor

thing is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật