Java 初始化JFrame窗口时出现黑屏

Java 初始化JFrame窗口时出现黑屏,java,swing,jframe,Java,Swing,Jframe,到现在为止,我觉得我已经尝试了一切。我正在努力 让这个简单的俄罗斯方块游戏运行,但即使我没有 除此之外,JFrame窗口仍然显示为黑色,带有一个小的 左上角的白色矩形 这是我已经尝试过的: 使用f.setVisible(真)进行游戏;正如许多ppl建议的那样 检查可能的无限循环(将=+1更改为+=1) 还尝试包括://f.add(面板)//f、 包装() 我的代码: package fritol.sevela.java; import java.awt.Color; import java.a

到现在为止,我觉得我已经尝试了一切。我正在努力 让这个简单的俄罗斯方块游戏运行,但即使我没有 除此之外,JFrame窗口仍然显示为黑色,带有一个小的 左上角的白色矩形

这是我已经尝试过的:

  • 使用f.setVisible(真)进行游戏;正如许多ppl建议的那样
  • 检查可能的无限循环(将=+1更改为+=1)
  • 还尝试包括:
    //f.add(面板)//f、 包装()
  • 我的代码:

    package fritol.sevela.java;
    
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Point;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.util.ArrayList;
    import java.util.Collections;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    
    
    
    public class Tetris002 extends JPanel {        
    
    
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
    
    
        /**
         * 
         */
    
    
    
        private final Point[][][] myPoint= {
                {
    
                    {new Point(0,1),new Point(1,1),new Point(2,1),new Point(3,1)}, 
                    {new Point(1,0),new Point(1,1),new Point(1,2),new Point(1,3)}, 
                    {new Point(0,1),new Point(1,1),new Point(2,1),new Point(3,1)}, 
                    {new Point(1,0),new Point(1,1),new Point(1,2),new Point(1,3)}, 
                },  
    
    
    
                {
    
                    {new Point(0,1),new Point(1,1),new Point(2,1),new Point(2,0)}, 
                    {new Point(1,0),new Point(1,1),new Point(1,2),new Point(2,2)}, 
                    {new Point(0,1),new Point(1,1),new Point(2,1),new Point(0,1)}, 
                    {new Point(1,0),new Point(1,1),new Point(1,2),new Point(0,3)}, 
                },  
    
    
                {
    
                    {new Point(0,1),new Point(1,1),new Point(2,1),new Point(2,0)}, 
                    {new Point(1,0),new Point(1,1),new Point(1,2),new Point(2,2)}, 
                    {new Point(0,1),new Point(1,1),new Point(2,1),new Point(0,0)}, 
                    {new Point(1,0),new Point(1,1),new Point(1,2),new Point(2,0)}, 
                },  
    
                {
    
                    {new Point(0,0),new Point(0,1),new Point(1,0),new Point(1,1)}, 
                    {new Point(0,0),new Point(0,1),new Point(1,0),new Point(1,1)}, 
                    {new Point(0,0),new Point(0,1),new Point(1,0),new Point(1,1)}, 
                    {new Point(0,0),new Point(0,1),new Point(1,0),new Point(1,1)}, 
                },  
    
        };  
    
    
        //Defining COLOURS
        private final Color[] myColor= {Color.CYAN, Color.magenta,Color.orange,Color.yellow,Color.black,Color.pink,Color.red};
    
    
        private Point pt;
        private int currentPiece;
        private int rotation;
        private ArrayList<Integer> nextPiece=new ArrayList<Integer>();
        private long score;
        private Color[][] well;
    
    
    
        private void init() {
                well=new Color[12][24];   
                for(int i=0; i<12;i++) {  
                    for(int j=0;j<23;j++) { 
                        if(i==0||i==11||i==22) {
                            well[i][j]=Color.pink;
                        }else{
                            well[i][j]=Color.black; 
                        }                               
    
                    }
                }
    
                newPiece();   
            }   
    
    
        public void newPiece() {
            pt=new Point(5,2);
            rotation=0;
            if(nextPiece.isEmpty()) {   
                Collections.addAll(nextPiece, 0,1,2,3);   
                Collections.shuffle(nextPiece);  
            }
        }
    
        rotation.
        private boolean collidesAt(int x, int y, int rotation) {
    
            for(Point p: myPoint[currentPiece][rotation]) {
                if(well[p.x+x][p.y+y]!=Color.black) {
                    return true;
    
                }
    
            }
            return false;
        }
    
        private void rotate(int i) {    
        int newRotation=(rotation+i)%4;
        if(newRotation<0) {
            newRotation=3;
    
            }
    
        if(!collidesAt(pt.x,pt.y, newRotation)){
            rotation=newRotation;
    
        }
        repaint(); //Some repaint function?
    
        }   
    
                //move and move only??
        public void move(int i) {
            if(!collidesAt(pt.x,pt.y,rotation)) {
            pt.x+=i;    
            }
    
            repaint();
    
        }
        public void drop() {           
            if(!collidesAt(pt.x,pt.y,rotation)) {
            pt.y+=1;
            }else {
                fixToWell();
            }
            repaint();
            }
    
    
        public void  fixToWell() {
            for(Point p: myPoint[currentPiece][rotation]) {
            well[pt.x+p.x]  [pt.y+p.y]=myColor[currentPiece];
    
            }
            clearRows();
            newPiece();
    
        }
    
        public void deleteRow(int row) {         
            for(int j=row-1;j>0;j--) {
                for (int i=1; i<11;i++){
                    well[i][j+1]=well[i][j];
    
                }
            }
        }
    
        //Now we will create Row
    
        public void clearRows() {
            boolean gap;
            int numClear=0;
            for(int j=21; j>0; j--) {
                gap=false;
                for(int i=1;i<11;i++) {
                    if(well[i][j]==Color.black) {
                    gap=true;
                        break;
    
                    }
                }       
            if(!gap) {
                deleteRow(numClear);
                j+=1;
                numClear+=1;    
            }
            }
            switch(numClear) {            //score stuff
                case 1:
                    score+=100;
                    break;
    
                case 2:
                    score+=300;
                    break;
    
                case 3:
                    score+=500;
                    break;  
    
                case 4:
                    score+=800;
                    break;
                }   
    
    
            } //now we provide function to draw the piece //<>[]{}
    
            private void drawPiece(Graphics g) {
                g.setColor(myColor[currentPiece]);
                for(Point p:myPoint[currentPiece][rotation]) {
                g.fillRect((p.x+pt.x)*26,(pt.y+p.y)*26,25,25);
    
                }       
            }
    
            public void paintComponent(Graphics g) {    
                g.fillRect(0, 0, 26*12, 26*23);
                for(int i=0; i<23;i++) {
                for (int j=0;j<23;j++)  {
                    g.setColor(well[i][j]);
                    g.fillRect(26*i,26*j, 25,25);
    
                    }   
                }   
                g.setColor(Color.WHITE);
                g.drawString("Score:"+score, 19*12, 25);        
                drawPiece(g);
    
            }
    
    
        public static void main(String[] args) {
    
    
    
            JFrame f=new JFrame("Tetris002");         
            //JPanel panel=new JPanel(); //Added by me
            //f.setContentPane(panel);  //added by me
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setSize(12*26+10, 26*23+25);
            //f.add(panel);
            //f.pack();
            f.setVisible(true);
    
    
            final Tetris002 game=new Tetris002();  
    
    
            game.init();
            f.add(game);
    
    
    
            //Now Controls and Keys
            f.addKeyListener(new KeyListener() {
    
            @Override
            public void keyTyped(KeyEvent e) { 
    
            }
    
            public void keyReleased(KeyEvent e) { 
    
    
    
            }
    
            public void keyPressed(KeyEvent e) { 
                switch(e.getKeyCode()) {
                case KeyEvent.VK_UP:
                    game.rotate(-1);
                    break;
    
                case KeyEvent.VK_DOWN:
                    game.rotate(+1);
                    break;      
    
    
                case KeyEvent.VK_LEFT:
                    game.move(-1);
                    break;  
    
                //case KeyEvent.VK_RIGHT:        
                    //game.move(+1);
                    //break;
    
                case KeyEvent.VK_SPACE:
                    game.drop();
                    game.score+=1;
    
                    break;
    
                    }
                }
        });
        new Thread() {
            public void run() {
                while(true) {
                    try {
                        Thread.sleep(1000); 
    
                } catch (InterruptedException e) {
    
                    e.printStackTrace();
    
                }
                game.drop();
    
    
                }
            }
    
            }.start();
    
    
            }
    
    }
    
    包fritol.sevela.java;
    导入java.awt.Color;
    导入java.awt.Graphics;
    导入java.awt.Point;
    导入java.awt.event.KeyEvent;
    导入java.awt.event.KeyListener;
    导入java.util.ArrayList;
    导入java.util.Collections;
    导入javax.swing.JFrame;
    导入javax.swing.JPanel;
    公共类Tetris002扩展了JPanel{
    /**
    * 
    */
    私有静态最终长serialVersionUID=1L;
    /**
    * 
    */
    私人终点[][]我的终点={
    {
    {新点(0,1),新点(1,1),新点(2,1),新点(3,1)},
    {新点(1,0),新点(1,1),新点(1,2),新点(1,3)},
    {新点(0,1),新点(1,1),新点(2,1),新点(3,1)},
    {新点(1,0),新点(1,1),新点(1,2),新点(1,3)},
    },  
    {
    {新点(0,1),新点(1,1),新点(2,1),新点(2,0)},
    {新点(1,0),新点(1,1),新点(1,2),新点(2,2)},
    {新点(0,1),新点(1,1),新点(2,1),新点(0,1)},
    {新点(1,0),新点(1,1),新点(1,2),新点(0,3)},
    },  
    {
    {新点(0,1),新点(1,1),新点(2,1),新点(2,0)},
    {新点(1,0),新点(1,1),新点(1,2),新点(2,2)},
    {新点(0,1),新点(1,1),新点(2,1),新点(0,0)},
    {新点(1,0),新点(1,1),新点(1,2),新点(2,0)},
    },  
    {
    {新点(0,0),新点(0,1),新点(1,0),新点(1,1)},
    {新点(0,0),新点(0,1),新点(1,0),新点(1,1)},
    {新点(0,0),新点(0,1),新点(1,0),新点(1,1)},
    {新点(0,0),新点(0,1),新点(1,0),新点(1,1)},
    },  
    };  
    //定义颜色
    私有最终颜色[]myColor={Color.CYAN,Color.品红色,Color.橙色,Color.黄色,Color.黑色,Color.粉色,Color.red};
    私人点;
    私用电流片;
    私有整数旋转;
    private ArrayList nextpice=new ArrayList();
    私人长分数;
    私人色彩[][]良好;
    私有void init(){
    井=新颜色[12][24];
    
    对于(int i=0;i你看到控制台有没有任何可能的错误?因为当我运行你的代码时,我在这行中得到了
    ArrayIndexOutOfBounds
    异常(在
    paintComponent
    方法内部):
    g.setColor(well[i][j]);
    。你的外循环是
    for(int i=0;i<23;i++)
    数组是
    新颜色[12][24]
    。难怪会发生这种情况。将循环更改为
    for(int i=0;i<12;i++)
    将使帧可见

    不过也有一些注意事项

  • 所有swing应用程序都必须在自己的线程上运行,称为EDT(事件调度线程)。在
    main
    中调用
    SwingUtilities\invokeLater
    ,然后让应用程序在那里运行

    SwingUtilities.invokeLater(()->{
        JFrame f = new JFrame("Tetris002");
        ....
    });
    
  • 与其创建一个
    新线程
    ,不如看看如何使用
    javax.swing.Timer
    看起来更好(从代码的角度来看),它是为这种情况创建的,因为任务将在EDT中运行。用很少的话,糟糕的(代码外观)
    线程
    部分变成:

    new Timer(1000, e -> {
        game.drop();
    }).start();
    
  • 当您
    @覆盖
    paintComponent
    时,请始终调用
    super.paintComponent(g)
    。让组件“正常”绘制,然后执行以下操作:



  • 你看到控制台有没有任何可能的错误?因为当我运行你的代码时,我在这行中得到了一个
    ArrayIndexOutOfBounds
    异常(在
    paintComponent
    方法中):
    g.setColor(well[I][j]);
    。你的外循环是
    for(inti=0;I<23;I++
    ,而
    well
    数组是
    新颜色[12][24]
    。难怪会发生这种情况。将循环更改为(int i=0;i<12;i++)的
    将使帧可见

    不过也有一些注意事项

  • 所有swing应用程序都必须在自己的线程上运行,称为EDT(事件调度线程)。在
    main
    中调用
    SwingUtilities\invokeLater
    ,然后让应用程序在那里运行

    SwingUtilities.invokeLater(()->{
        JFrame f = new JFrame("Tetris002");
        ....
    });
    
  • 与其创建一个
    新线程
    ,不如看看如何使用
    javax.swing.Timer
    看起来更好(从代码的角度来看),它是为这种情况创建的,因为任务将在EDT中运行。用很少的话,糟糕的(代码外观)
    线程
    部分变成:

    new Timer(1000, e -> {
        game.drop();
    }).start();
    
  • 当您
    @覆盖
    paintComponent
    时,请始终调用
    super.paintComponent(g)
    。让组件“正常”绘制,然后执行以下操作:



  • 问题是函数
    paintComponent
    。您给数组赋值很好。您超出了边界。在数组上循环时,我建议使用
    array.length
    函数,而不是手动设置其长度。无论如何,如果您这样更改函数,它应该可以工作

    public void paintComponent(Graphics g) {
        g.fillRect(0, 0, 26 * 12, 26 * 23);
        for (int i = 0; i < well.length; i++) {
            for (int j = 0; j < well[i].length; j++) {
                g.setColor(well[i][j]);
                g.fillRect(26 * i, 26 * j, 25, 25);
            }
        }
        g.setColor(Color.WHITE);
        g.drawString("Score:" + score, 19 * 12, 25);
        drawPiece(g);
    }
    
    公共组件(图形g){
    g、 fillRect(0,0,26*12,26*23);
    for(int i=0;i