簡體   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