0

Program is Compiling and running but it is not displaying anything on the frame just a blank screen. i am making main menu for my game using cardLayout, i have made different classes for every panel and then added them in cardlayout in Frame Kindly some one please pinpoint the mistake i am doing i cant find it.. thankyou

enter code here

import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.io.*;
    import javax.imageio.ImageIO;
    import java.awt.image.BufferedImage;

    public class MainMenu extends JFrame implements ActionListener{
     static CardLayout cl2;
    static JPanel cardPanel;
    Name p1;
    MainM p2;
    Robot p3;
    High p4;
    Inst p5;
    gamepanel gp;

    public static int height;
    public static int width;

    public static void main(String args[]){
                    MainMenu cl = new MainMenu();
                    cl.What();
                    }
                    public void What() {

                    // get the screen size as a java dimension


                   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    setVisible(true);

                    setLayout(new BorderLayout());
                    setTitle("EROBOT");
                    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();



                // get height, and  width
                 height = screenSize.height ;
                 width = screenSize.width ;


                // set the jframe height and width
                setSize(new Dimension(width, height));


                    cardPanel = new JPanel();
                    p1= new Name();
                    p2= new MainM();
                    p3= new Robot();
                    p4= new High();
                    p5= new Inst();
                    gp= new gamepanel();






                    cl2 = new CardLayout();
                    cardPanel.setLayout(cl2);


                    cardPanel.add(p1, "name");
                    cardPanel.add(p2, "menu");
                    cardPanel.add(p3, "robo");
                    cardPanel.add(p4, "inst");
                    cardPanel.add(p5, "high");
                    cardPanel.add(gp, "start");
                     getContentPane().add(cardPanel);

        //name 
        Name.done.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
             cl2.show(cardPanel, "menu" );
        }
        });

    // Main
        MainM.newg.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
             cl2.show(cardPanel, "start" );
        }
        });
        MainM.high.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
             cl2.show(cardPanel, "high" );
        }
        });
        MainM.robo.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
             cl2.show(cardPanel, "robo" );
        }
        });
        MainM.inst.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
             cl2.show(cardPanel, "inst" );
        }
        });
    /////////////////////
    //Robot

    Robot.go.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
             cl2.show(cardPanel, "start" );
        }
    });

    Robot.back1.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
             cl2.show(cardPanel, "menu" );
        }
    });

    //high score
    High.back2.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
             cl2.show(cardPanel, "menu" );
        }
    });
    //how to play
    Inst.back.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
             cl2.show(cardPanel, "menu" );
        }
    });


                    }
                    public void actionPerformed(ActionEvent e){}


    }

     class Name extends JPanel{
    JLabel enter;
    static JButton done;
    JTextField name;
    JPanel side;
    JPanel up;
    JLabel title;


    Name(){
    Color h3= new Color(255,229,204);
    side= new JPanel();
    up= new JPanel();


    setLayout(null);
    MainMenu mm= new MainMenu();


            setBackground(h3);
            title = new JLabel("E-ROBOT");
             title.setFont(new Font("Ravie", Font.PLAIN, 40));  //setting the style and the font of JLabel
             title.setBounds(520, 100, 300, 50);


             enter= new JLabel("ENTER YOUR NAME HERE");
             enter.setFont(new Font("Ravie", Font.PLAIN, 20));
              enter.setForeground(Color.orange);
             enter.setBounds(370, 200, 350, 50);
            name = new JTextField();

            name.setBounds(390, 250, 150, 25);
            done= new JButton("DONE");
             done.setFont(new Font("Ravie", Font.PLAIN, 20));
              done.setForeground(Color.orange);
             done.setBackground(Color.YELLOW);
             done.setOpaque(false);
             done.setBorder(null);
            done.setBounds(400, 280, 150, 30);
            side.setBounds(0,40,mm.width,60);
            up.setBounds(40,0,40,mm.height);
            side.setBackground(Color.ORANGE);
            up.setBackground(Color.ORANGE);
            side.add(title);
            add(enter);
            add(name);
            add(done);
            add(side);
            add(up);
            }
    }
     class MainM extends JPanel{


    JPanel l1;
    JPanel l2;
    JPanel l3;
    JPanel l4;
    JPanel contain;
    JPanel side1;
    JPanel up1;
    static JButton newg;
    static JButton high;
    static JButton robo;
    static JButton inst;
    JLabel title;





    MainM(){
    MainMenu mm= new MainMenu();
    Color h3= new Color(255,229,204);
    setBackground(h3);
    l1= new JPanel();
    l2= new JPanel();
    l3= new JPanel();
    l4= new JPanel();
    contain= new JPanel();
    side1= new JPanel();
    up1= new JPanel();


    title = new JLabel("E-ROBOT");
             title.setFont(new Font("Ravie", Font.PLAIN, 40));
             title.setForeground(Color.WHITE);
                title.setBounds(520, 100, 300, 50);

                //custom colors
             Color b1= new Color(99,230,199);
             Color h= new Color(132,234,23);
            Color h1= new Color(45,99,120);
            Color h2= new Color(0,0,101);

            Color h4= new Color(154,0,0);

            l1.setBounds(480,200,10,525);
                l1.setBackground(h4);
                l2.setBounds(465,210,565,10);
                l2.setBackground(h4);
                l3.setBounds(1000,200,10,525);
                l3.setBackground(h4);
                l4.setBounds(465,690,565,10);
                l4.setBackground(h4);

            // settings style amd font of buttons
             newg = new JButton("New Game");
             newg.setFont(new Font("Ravie", Font.PLAIN, 25));
             newg.setBackground(Color.YELLOW);
             newg.setOpaque(false);
             newg.setBorder(null);
             newg.setForeground(h2);

            high = new JButton("High Scores");
            robo= new JButton("Select your Robot");
            inst = new JButton("How to Play");
             high.setFont(new Font("Ravie", Font.PLAIN, 25));
             high.setBackground(Color.YELLOW);
             high.setOpaque(false);
             high.setBorder(null);
             high.setForeground(h1);


              robo.setFont(new Font("Ravie", Font.PLAIN, 25));
              robo.setBackground(Color.YELLOW);
             robo.setOpaque(false);
             robo.setBorder(null);
             robo.setForeground(h);


              inst.setFont(new Font("Ravie", Font.PLAIN, 25));
              inst.setBackground(Color.YELLOW);
             inst.setOpaque(false);
             inst.setBorder(null);
             inst.setForeground(Color.MAGENTA);





            contain.setBounds(490,220,490,460);
            contain.setLayout(new GridLayout(4,1));

            side1.setBounds(0,40,mm.width,60);
            up1.setBounds(40,0,40,mm.height);
            side1.setBackground(h4);
            up1.setBackground(h4);






            //adding all the JComponents to the screen
            side1.add(title);
            contain.add(newg);
            contain.add(high);
            contain.add(robo);
            contain.add(inst);
            add(l1);
            add(l2);
            add(l3);
            add(l4);
            add(side1);
            add(up1);
            add(contain);


    }
    }
    class Robot extends JPanel{
    ImageIcon a;
    ImageIcon b;
    ImageIcon c;

    JPanel side2;
    JPanel up2;
    static JButton r1;
    static JButton r2;
     static JButton r3;
    static JButton back1;
    static JButton go;
    JLabel title1;
    Robot(){
     Color h= new Color(132,234,23);
    MainMenu mm= new MainMenu();
    Color h3= new Color(255,229,204);
    Color h4= new Color(154,0,0);
    side2= new JPanel();
    up2= new JPanel();
    r1= new JButton();
    r2= new JButton();
    r3= new JButton();
    back1= new JButton("Back");
    go= new JButton("LEt's go");

    a = new ImageIcon(getClass().getResource("a.gif"));
        b = new ImageIcon(getClass().getResource("b.gif"));
        c = new ImageIcon(getClass().getResource("c.gif"));

        //adding the ImageIcons to the JButtons
        r1 = new JButton(a);
        r1.setBounds(120,120,300,300);
        r1.setBackground(Color.YELLOW);
        r1.setOpaque(false);
         r1.setBorder(null);
        r2 = new JButton(b);
        r2.setBounds(460,120,300,300);
        r2.setBackground(Color.YELLOW);
        r2.setOpaque(false);
         r2.setBorder(null);

        r3 = new JButton(c);
        r3.setBounds(890,120,300,300);
        r3.setBackground(Color.YELLOW);
        r3.setOpaque(false);
         r3.setBorder(null);
        back1 = new JButton("Let's Go!");
        back1.setBounds(520, 500, 170, 60);
         back1.setFont(new Font("Ravie", Font.PLAIN, 25));
              back1.setBackground(Color.YELLOW);
             back1.setOpaque(false);
             back1.setBorder(null);
        setLayout(null);
         title1 = new JLabel("E-ROBOT");
             title1.setFont(new Font("Ravie", Font.PLAIN, 40)); //setting the style and the font of JLabel
            // title1.setBounds(520, 100, 300, 50);
        side2.setBounds(0,40,mm.width,60);
            up2.setBounds(40,0,40,mm.height);
            side2.setBackground(h);
            up2.setBackground(h);
            side2.add(title1);

            add(side2);
            add(up2);
        add(r1);
        add(r2);
        add(r3);
        add(back1);
        add(go);

    }

    }
     class High extends JPanel{
    JLabel title3;
    static JButton back2;
    JPanel side4;
    JPanel up4;

    High()
    {
     Color h= new Color(132,234,23);
    MainMenu mm= new MainMenu();
    Color h3= new Color(255,229,204);
    side4 = new JPanel();
    up4= new JPanel();
    Color h1= new Color(45,99,120);
    setLayout(null);
            title3 = new JLabel("E-ROBOT");
             title3.setFont(new Font("Ravie", Font.PLAIN, 40)); //setting the style and the font of JLabel
             title3.setBounds(520, 100, 300, 50);
             back2= new JButton("Back");
             back2.setBounds(500,500,120,30);
             back2.setFont(new Font("Ravie", Font.PLAIN, 25));
              back2.setBackground(Color.YELLOW);
             back2.setOpaque(false);
             back2.setBorder(null);
            side4.setBounds(0,40,mm.width,60);
            up4.setBounds(40,0,40,mm.height);
            side4.setBackground(h1);
            up4.setBackground(h1);
            side4.add(title3);

            add(side4);
            add(up4);

            add(back2);
    }
    }
     class Inst extends JPanel{
    JLabel jl1;
    JLabel jl2;
    JLabel jl3;
    JLabel jl4;
    JLabel jl5;

    JLabel title2;
    JPanel side3;
    JPanel up3;
    static JButton back;
    JPanel contain2;
    Inst(){
    MainMenu mm= new MainMenu();
    contain2= new JPanel();
    side3= new JPanel();
    up3= new JPanel();
     Color h= new Color(132,234,23);
    Color h3= new Color(255,229,204);

            jl1= new JLabel("Welcome aboard\n !");
            jl2 = new JLabel("So the game is simple.\n");
            jl3 = new JLabel("You have to catch even number only\n.");
            jl4 = new JLabel("A life ends if you catch an odd number.\n");
            jl5 = new JLabel("Oh and also the game gets more difficult when your lives end!\n");
            jl1.setFont(new Font("Ravie", Font.PLAIN, 20));
            jl2.setFont(new Font("Ravie", Font.PLAIN, 20));
            jl3.setFont(new Font("Ravie", Font.PLAIN, 20));
            jl4.setFont(new Font("Ravie", Font.PLAIN, 20));
            jl5.setFont(new Font("Ravie", Font.PLAIN, 20));
            jl1.setForeground(Color.magenta);
            jl2.setForeground(Color.magenta);
            jl3.setForeground(Color.magenta);
            jl4.setForeground(Color.magenta);
            jl5.setForeground(Color.magenta);
             title2 = new JLabel("E-ROBOT");
             title2.setFont(new Font("Ravie", Font.PLAIN, 40)); //setting the style and the font of JLabel
             title2.setBounds(520, 100, 300, 50);

            back = new JButton("Back");
            back.setFont(new Font("Ravie", Font.PLAIN, 15));
              back.setBackground(Color.YELLOW);
             back.setOpaque(false);
             back.setBorder(null);

            setLayout(null);
            contain2.setBounds(80,200,800,400);
            contain2.setBackground(h3);
            side3.setBounds(0,40,mm.width,60);
            up3.setBounds(40,0,40,mm.height);
            side3.setBackground(Color.MAGENTA);
            up3.setBackground(Color.MAGENTA);
            side3.add(title2);

            contain2.add(jl1);
            contain2.add(jl2);
            contain2.add(jl3);
            contain2.add(jl4);
            contain2.add(jl5);
            contain2.add(back);
            add(contain2);
            add(side3);
            add(up3);






    }
    }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • OHH... it is working but not the first time but when i minimize it and then mazimize it runs perfectlty... – user3516817 May 23 '14 at 21:29
  • 1) Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). 2) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete and Verifiable Example). – Andrew Thompson May 24 '14 at 01:56

1 Answers1

0

I'm not sure whether it helps (sorry but your code is too large and bad formatted, so I cannot understand it), but you can try to revalidate and repaint the root pane or content pane of JFrame each time when you update your GUI.

Something like this

public static void updateRootPane(JComponent aComponent) {
    Window w = SwingUtilities.windowForComponent(aComponent);
    if (w instanceof JFrame) {
        ((JFrame) w).getRootPane().revalidate();
        ((JFrame) w).getRootPane().repaint();
    } else if (w instanceof JDialog) {
        ((JDialog) w).getRootPane().revalidate();
        ((JDialog) w).getRootPane().repaint();
    }
}
Sergiy Medvynskyy
  • 11,160
  • 1
  • 32
  • 48