繁体   English   中英

调用setBackground()和setBorder()时,JLabel不会更改

[英]JLabel doesn't change when setBackground() and setBorder() are called

我一直在为我的一个课程编写蛇程序。 除了一个小问题,它运行得很好:在View (扩展了JLabel ),在构造函数中,将背景设置为Color.WHITE ,将opaquetrue ,将边框设置为Color.GREEN 这些行似乎都没有影响GUI。

这是代码:

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

import javax.swing.BorderFactory;
import javax.swing.JLabel;

public class View extends JLabel {
    private Snake snake;
    private Fruit fruit;
    private Game game;

    public View(int size, Game g) {
        int W = Game.SIZE * Snake.SIZE;
        int H = Game.SIZE * Snake.SIZE;
        this.setOpaque(true);
        this.setBounds((Game.WIDTH-W)/2, (Game.HEIGHT-H)/2, W, H);
        this.setBackground(Color.WHITE);
        this.setBorder(BorderFactory.createLineBorder(Color.GREEN));
        snake = new Snake(Snake.SIZE, Snake.SIZE);
        fruit = new Fruit();
        game = g;

    }

    public void start() {
        boolean flag = true;
        snake.start();

        while(flag) {
            snake.move();
            repaint();
            try {
                Thread.sleep(100);
            }catch(Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void move(int dir) {
        snake.move(dir);
    }

    public void doDrawing(Graphics2D g) {
        int s = snake.SIZE;
        int fx = fruit.getX();
        int fy = fruit.getY();
        g.setColor(Game.SNAKE_COLOR);
        Snake sn = snake;
        boolean flag = true;
        while(flag) {
            g.fillRect(sn.getX(), sn.getY(), s, s);
            if(sn.hasTail()) {
                sn = sn.getTail();
            }
            else flag = false;
        }

        g.setColor(Game.FRUIT_COLOR);
        g.fillRect(fx, fy, s, s);

        if(snake.getX() == fx && snake.getY() == fy) {
            snake.ate();
            fruit.create();
        }
    }

    public void paint(Graphics g) {
        game.repaint();
        Graphics2D d = (Graphics2D) g;
        doDrawing(d);
    }
}

您正在重写paint方法,但永远不要调用super.paint()来让父类执行其绘画操作。 此处提供更多信息。

其他一些改进建议:

  1. 而不是使用paint() ,您应该重写paintComponent()并在方法的前super.paintComponent(g)行中调用父绘制例程super.paintComponent(g)

  2. 您可能考虑扩展JPanelJComponent而不是JLabel ,因为您似乎没有使用此类的任何功能。

  3. 不要在您的绘画方法中调用重绘

package snake;

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

import javax.swing.BorderFactory;
import javax.swing.JComponent;

@SuppressWarnings("serial")
public class View extends JComponent {
    private Snake snake;
    private Fruit fruit;
    private Game game;

    public View(int size, Game g) {
        int W = Control.SIZE * Snake.SIZE;
        int H = Control.SIZE * Snake.SIZE;
        this.setOpaque(true);
        this.setVisible(true);
        this.setBounds((Game.WIDTH-W)/2, (Game.HEIGHT-H)/2, W, H);
        this.setBackground(Color.BLACK);
        this.setBorder(BorderFactory.createLineBorder(Color.GREEN));
        snake = new Snake(Snake.SIZE, Snake.SIZE);
        fruit = new Fruit();
        game = g;

    }

    public void start() {
        boolean flag = true;
        snake.start();

        while(flag) {
            snake.move();
            repaint();
            try {
                Thread.sleep(Control.TIME);
            }catch(Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void move(int dir) {
        snake.move(dir);
    }

    public void doDrawing(Graphics2D g) {
        int s = Snake.SIZE;
        int fx = fruit.getX();
        int fy = fruit.getY();
        g.setColor(Control.SNAKE_COLOR);

        snake.display(g);

        g.setColor(Control.FRUIT_COLOR);
        g.fillRect(fx, fy, s, s);

        if(snake.getX() == fx && snake.getY() == fy) {
            snake.ate();
            fruit.create();
        }
    }

    public void paintComponent(Graphics g) {
        game.repaint();

        super.paintComponent(g);
        Graphics2D d = (Graphics2D) g;
        doDrawing(d);
    }
}

游戏扩展了JFrame

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM