空白框架没有明显的原因

时间:2014-05-23 21:26:01

标签: java swing jframe jpanel

程序正在编译并运行,但它不会在框架上显示任何空白屏幕。 我正在使用cardLayout为我的游戏制作主菜单,我为每个面板制作了不同的类,然后将它们添加到了Framelay的Framelayout中 请一些人请指出我正在做的错误我无法找到它.. 三江源

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);






    }
    }

1 个答案:

答案 0 :(得分:0)

我不确定它是否有帮助(抱歉,但您的代码太大且格式错误,所以我无法理解),但您可以尝试每次重新验证并重新绘制JFrame的根窗格或内容窗格当您更新GUI时。

像这样的东西

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();
    }
}