首页 > 解决方案 > 如何向 jframe 添加多个矩形(尝试以简单的方式编码 2048)

问题描述

我已经制作了游戏,并希望使用矩形而不是 jlabels 使我的 GUI 看起来更好,现在我意识到只有最后一个绘制的矩形显示在 GUI 上

我已经尝试过不同的布局

我的 GUI 类:

public class GUI_N    
{
    private Spiel spiel;
    private KeyEvent e;
    private String beste;
    private int beste1;
    private DrawingComponent[][] feld;
    GUI_N(){
        feld = new DrawingComponent[4][4];
        spiel = new Spiel();
        beste1 = 0;
        beste = "Highscore: "+beste1;


        JFrame g=new JFrame("2048 - Main");
        g.setSize(500,500);
        g.setFocusable(true); //wichtig für KeyListener
        g.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        int i = 0;
        int j = 0;
        int h = 0;
        int l = 0;
        while(i<4)
        {
            while(j<4)
            {
                if(i==0){ 
                    h = 50; 
                }else if(i==1){
                    h = 100; 
                }else if(i==2){
                    h = 150; 
                }else if(i==3){
                    h = 200;
                }
                if(j==0){ 
                    l = 50; 
                }else if(j==1){
                    l = 100; 
                }else if(j==2){
                    l = 150; 
                }else if(j==3){
                    l = 200;
                }
                feld[i][j] = new DrawingComponent(l,h,50,50);
                feld[i][j].setBounds(l,h,50,50);
                j++;
            }
            j=0;
            i++;
        }
        i = 0;
        j = 0;
        while(i<4)
        {
            while(j<4)
            {
                g.add(feld[i][j]);
                j++;
            }
            j=0;
            i++;
        }
        //g.getContentPane().setBackground(new Color(20, 40, 50));
        g.setVisible(true);

    }

    public static void main(String[] args) {
        new GUI_N();
    }  
    } 

我的矩形类:

public class DrawingComponent extends JComponent
{
    private Graphics2D g2;
    private int wert;

    private int x;
    private int y;
    private int w;
    private int h;

    public DrawingComponent(int px,int py,int pw,int ph)
    {
        x=px;
        y=py;
        w=pw;
        h=ph;
    }
    public void paintComponent(Graphics g)
    {
        g2 = (Graphics2D) g;

        Rectangle rect1 = new Rectangle(x,y,w,h);
        g2.setColor(Color.RED);
        g2.fill(rect1);
    }

    public void setWert(int x)
    {
        wert = x;
    }

    public int getWert()
    {
        return wert;
    }
}

正如我所说,只显示最后绘制的矩形。

我如何实现这一目标?

标签: javaswingjframejpanel

解决方案


现在您正在将矩形直接添加到您的框架中。你应该有一个 JPanel 层,你可以给它一个 LayoutManager(GridLayout 是一个很好的查看)来排列你的所有矩形。

所以你会有这样的事情:

JFrame g = new JFrame("2048 - Main");
// GridLayout (on next line) takes number of rows and columns
JPanel panel = new JPanel(new GridLayout(4, 4));
// ... add all the rectangles to the panel here
g.add(panel);

然后您将矩形添加到面板,而不是框架。当您添加它们时,它们将自动在网格中到位。

panel.add(feld[i][j]);

此外,如果您使用 GridLayout,它会动态调整组件的大小并使其适合网格,因此它也可以为您节省一些代码,因为您不需要在 GUI 类中硬编码它们的大小。


推荐阅读