有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java我的JPanel类没有在我的JFrame上绘制矩形?

我正试图用java制作一个俄罗斯方块游戏,但我目前只是想在我的游戏网格上显示一个形状。。。我查看了我的代码,没有发现任何错误或错误,这意味着这将无法工作

这是我的主课,叫做Tetris6

package tetris6;


import java.awt.Graphics;
import javax.swing.*;


public class Tetris6 {

    public static final int WIDTH = 400, HEIGHT = 825;


    public static void main(String[] args) {

    GamePanel gamePanel = new GamePanel();

    JFrame frame = new JFrame();
    frame.setLayout(null);
    frame.setResizable(false);
    frame.setSize(WIDTH,HEIGHT);
    frame.setTitle("Tetris");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(MenuBar.MenuBar());
    frame.add(BoardGrid.GameGrid());
    frame.add(gamePanel);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);


    }

}

这是我的类,它使用JPanels创建网格

public class BoardGrid {    


    public static JPanel GameGrid () {  


        JPanel Grid = new JPanel (new GridLayout(20,10));

        for (int i=0; i < 200; i++){

        Grid.add(createSquareJPanel(Color.white,30));


        }

        Grid.setVisible(true);
        Grid.setBorder(BorderFactory.createLineBorder(Color.white));
        Grid.setBounds(0,50,400,625);

        return Grid;

        }

    private static JPanel createSquareJPanel(Color color, int size)
    {
        JPanel tempPanel = new JPanel();
        tempPanel.setBackground(color);
        tempPanel.setMinimumSize(new Dimension(size, size));
        tempPanel.setMaximumSize(new Dimension(size, size));
        tempPanel.setPreferredSize(new Dimension(size, size));
        tempPanel.setBorder(BorderFactory.createLineBorder(Color.blue));
        return tempPanel;
    }

}

这是我在其中创建菜单栏的类

package tetris6;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import static tetris6.Tetris6.WIDTH;

/**
 *
 */
public class MenuBar {

    public static JMenuBar MenuBar () {



    JMenuBar bar = new JMenuBar();
    bar.setBounds(0,0, WIDTH, 25);

    JMenu file = new JMenu("File");
    file.setBounds(0,0,45,24);

    JMenuItem newGame = new JMenuItem("New Game");
    newGame.addActionListener(new ActionListener(){
       @Override
       public void actionPerformed(ActionEvent e){
           //code for new game
           System.out.println("Starting new game...");
       }
    });

    JMenuItem highScore = new JMenuItem("High Score");
    highScore.addActionListener(new ActionListener(){
       @Override
       public void actionPerformed(ActionEvent f){
           int highscore = 0; //replace this with getHighscoreMethod later
           final JFrame alert = new JFrame("HighScore");
           alert.setSize(200,200);
           alert.setLayout(null);
           alert.setLocationRelativeTo(null);

           JLabel score = new JLabel ("The highscore is " + highscore);
           score.setBounds(35,0,200,50);

           JButton okayButton = new JButton("Okay");
           okayButton.setBounds(50,120,100,30);
           okayButton.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){
                alert.dispose();
            }   
           });
           alert.add(score);
           alert.add(okayButton);
           alert.setResizable(false);
           alert.setVisible(true);
       }
    });

    JMenuItem exit = new JMenuItem("Exit");
    exit.addActionListener(new ActionListener(){
       @Override
       public void actionPerformed(ActionEvent g){
           System.out.println("Closing...");
           System.exit(0);
       }
    });


    file.add(newGame);
    file.add(highScore);
    file.add(exit);
    bar.add(file);

    return bar;

}

}

这是我制作俄罗斯方块形状的课程

package tetris6;

import java.awt.Color;
import java.awt.Graphics2D;

public class Shape {

    public static void main () {


    }

    //Creating the tetris shapes and the rotated shapes    

public static Graphics2D O(Graphics2D tetrisShape)
{
  tetrisShape.setColor(Color.yellow);
  tetrisShape.fillRect(0,0,30,30);
  tetrisShape.fillRect(30,0,30,30);
  tetrisShape.fillRect(0,30,30,30);
  tetrisShape.fillRect(30,30,30,30);

  return tetrisShape;
}

public static Graphics2D T_up(Graphics2D tetrisShape)
{
  tetrisShape.setColor(Color.magenta);
  tetrisShape.fillRect(30,0,30,30);
  tetrisShape.fillRect(0,30,30,30);
  tetrisShape.fillRect(30,30,30,30);
  tetrisShape.fillRect(60,30,30,30);

  return tetrisShape;
}

public static Graphics2D T_right(Graphics2D tetrisShape)
{
  tetrisShape.setColor(Color.magenta);
  tetrisShape.fillRect(0,0,30,30);
  tetrisShape.fillRect(0,30,30,30);
  tetrisShape.fillRect(0,60,30,30);
  tetrisShape.fillRect(30,30,30,30);

  return tetrisShape;
}

public static Graphics2D T_left(Graphics2D tetrisShape)
{
  tetrisShape.setColor(Color.magenta);
  tetrisShape.fillRect(0,30,30,30);
  tetrisShape.fillRect(30,0,30,30);
  tetrisShape.fillRect(30,30,30,30);
  tetrisShape.fillRect(30,60,30,30);

  return tetrisShape;
}

public static Graphics2D T_down(Graphics2D tetrisShape)
{
  tetrisShape.setColor(Color.magenta);
  tetrisShape.fillRect(0,0,30,30);
  tetrisShape.fillRect(30,0,30,30);
  tetrisShape.fillRect(60,0,30,30);
  tetrisShape.fillRect(30,30,30,30);

  return tetrisShape;
}

public static Graphics2D I_vertical(Graphics2D tetrisShape)
{
  tetrisShape.setColor(Color.cyan);
  tetrisShape.fillRect(0,0,30,30);
  tetrisShape.fillRect(0,30,30,30);
  tetrisShape.fillRect(0,60,30,30);
  tetrisShape.fillRect(0,90,30,30);

  return tetrisShape;
}

public static Graphics2D I_horizontal(Graphics2D tetrisShape)
{
  tetrisShape.setColor(Color.cyan);
  tetrisShape.fillRect(0,0,30,30);
  tetrisShape.fillRect(30,0,30,30);
  tetrisShape.fillRect(60,0,30,30);
  tetrisShape.fillRect(90,0,30,30);

  return tetrisShape;
}

public static Graphics2D S_horizontal(Graphics2D tetrisShape)
{
  tetrisShape.setColor(Color.red);
  tetrisShape.fillRect(30,0,30,30);
  tetrisShape.fillRect(60,0,30,30);
  tetrisShape.fillRect(0,30,30,30);
  tetrisShape.fillRect(30,30,30,30);

  return tetrisShape;
}

public static Graphics2D S_vertical(Graphics2D tetrisShape)
{
  tetrisShape.setColor(Color.red);
  tetrisShape.fillRect(0,0,30,30);
  tetrisShape.fillRect(0,30,30,30);
  tetrisShape.fillRect(30,30,30,30);
  tetrisShape.fillRect(30,60,30,30);

  return tetrisShape;
}

public static Graphics2D Z_horizontal(Graphics2D tetrisShape)
{
  tetrisShape.setColor(Color.green);
  tetrisShape.fillRect(30,30,30,30);
  tetrisShape.fillRect(60,30,30,30);
  tetrisShape.fillRect(0,0,30,30);
  tetrisShape.fillRect(30,0,30,30);

  return tetrisShape;
}

public static Graphics2D Z_vertical(Graphics2D tetrisShape)
{
  tetrisShape.setColor(Color.green);
  tetrisShape.fillRect(30,0,30,30);
  tetrisShape.fillRect(30,30,30,30);
  tetrisShape.fillRect(0,30,30,30);
  tetrisShape.fillRect(0,60,30,30);

  return tetrisShape;
}

public static Graphics2D J_vertical_left(Graphics2D tetrisShape)
{
  tetrisShape.setColor(Color.blue);
  tetrisShape.fillRect(30,0,30,30);
  tetrisShape.fillRect(30,30,30,30);
  tetrisShape.fillRect(30,60,30,30);
  tetrisShape.fillRect(0,60,30,30);

  return tetrisShape;
}

public static Graphics2D J_vertical_right(Graphics2D tetrisShape)
{
  tetrisShape.setColor(Color.blue);
  tetrisShape.fillRect(0,0,30,30);
  tetrisShape.fillRect(0,30,30,30);
  tetrisShape.fillRect(0,60,30,30);
  tetrisShape.fillRect(30,0,30,30);

  return tetrisShape;
}

public static Graphics2D J_horizontal_up(Graphics2D tetrisShape)
{
  tetrisShape.setColor(Color.blue);
  tetrisShape.fillRect(0,30,30,30);
  tetrisShape.fillRect(30,30,30,30);
  tetrisShape.fillRect(60,30,30,30);
  tetrisShape.fillRect(0,0,30,30);

  return tetrisShape;
}

public static Graphics2D J_horizontal_down(Graphics2D tetrisShape)
{
  tetrisShape.setColor(Color.blue);
  tetrisShape.fillRect(0,0,30,30);
  tetrisShape.fillRect(30,0,30,30);
  tetrisShape.fillRect(60,0,30,30);
  tetrisShape.fillRect(60,30,30,30);

  return tetrisShape;
}

public static Graphics2D L_vertical_right(Graphics2D tetrisShape)
{
  tetrisShape.setColor(Color.orange);
  tetrisShape.fillRect(0,0,30,30);
  tetrisShape.fillRect(0,30,30,30);
  tetrisShape.fillRect(0,60,30,30);
  tetrisShape.fillRect(30,60,30,30);

  return tetrisShape;
}

public static Graphics2D L_vertical_left(Graphics2D tetrisShape)
{
  tetrisShape.setColor(Color.orange);
  tetrisShape.fillRect(0,0,30,30);
  tetrisShape.fillRect(30,0,30,30);
  tetrisShape.fillRect(30,30,30,30);
  tetrisShape.fillRect(30,60,30,30);

  return tetrisShape;
}

public static Graphics2D L_horizontal_up(Graphics2D tetrisShape)
{
  tetrisShape.setColor(Color.orange);
  tetrisShape.fillRect(60,0,30,30);
  tetrisShape.fillRect(0,30,30,30);
  tetrisShape.fillRect(30,30,30,30);
  tetrisShape.fillRect(60,30,30,30);

  return tetrisShape;
}

public static Graphics2D L_horizontal_down(Graphics2D tetrisShape)
{

  tetrisShape.setColor(Color.orange);
  tetrisShape.fillRect(0,0,30,30);
  tetrisShape.fillRect(30,0,30,30);
  tetrisShape.fillRect(60,0,30,30);
  tetrisShape.fillRect(0,30,30,30);

  return tetrisShape;
}

//finished creating the tetrominoes

}

这是我试图在JPanel中绘制矩形的类

package tetris6;

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;

/**
 *
 */
public class GamePanel extends JPanel {

    public void paintComponent (Graphics g) {

        super.paintComponent(g);

        //set the panel to overlap the game grid
        setBounds(0,50,400,625);
        //draw the tetris shape at the top middle of the grid
        g.setColor(Color.yellow);
        g.fillRect(160, 0, 80, 80);

        setVisible(true);



    }

}

共 (1) 个答案

  1. # 1 楼答案

    如果没有合理的理由,请避免设置布局(null)。删除下面的代码。矩形将被显示

    如果仍然必须使用空布局,则必须设置组件的宽度和高度及其x和y位置。在tetris6类中,删除setLayout(null)

       JFrame frame = new JFrame();
       //frame.setLayout(null);   // remove this line.