为了账号安全,请及时绑定邮箱和手机立即绑定

repaint() - 组件不会出现在简单的展示中

repaint() - 组件不会出现在简单的展示中

噜噜哒 2021-12-22 16:01:58
我正在尝试编写黑白棋的代码,并且......我已经被一个基本的观点困住了。我的主要课程:public class Othello extends JFrame {    private static final long serialVersionUID = 1L;    public static final int WIDTH = 800;    public static final int HEIGHT = 600;    private Grid grid;    public Othello() {        this.setSize(WIDTH, HEIGHT);        this.setTitle("Othello");        this.grid = new Grid();        this.setContentPane(this.grid);        this.grid.revalidate();        this.grid.repaint();    }    public void run() {        this.setLocationRelativeTo(null);        this.setDefaultCloseOperation(EXIT_ON_CLOSE);        this.setResizable(false);        this.setVisible(true);    }    public static void main(String[] args) {        new Othello().run();    }}还有我的JPanel班级:public class Grid extends JPanel {    private static final long serialVersionUID = 1L;    public Grid() {}    @Override    public void paintComponent(Graphics g) {        super.paintComponent(g);        g.setColor(new Color(0,128,0));        g.fillRect(0, 0, WIDTH, HEIGHT);    }}我不明白为什么它不显示任何内容。将paintComponent被调用,但没有任何反应,我想打电话revalidate()和repaint()几乎无处不在,没有什么作品。我一直在寻找不同主题的解决方案将近 1 个小时,但我发现的解决方案都没有奏效。
查看完整描述

1 回答

?
慕村9548890

TA贡献1884条经验 获得超4个赞

这是你的问题:


g.fillRect(0, 0, WIDTH, HEIGHT);

WIDTH 和 HEIGHT 值不是您期望的值,实际上它们很可能都是 0。为了最安全的编程,您需要通过getWidth()和获得实际的宽度和高度getHeight()


不需要那些revalidate()s 和repaint()s。例如:


import java.awt.Color;

import java.awt.Dimension;

import java.awt.Graphics;


import javax.swing.*;


public class GridTest {


    private static final int WIDTH = 800;

    private static final int HEIGHT = 600;


    private static void createAndShowGui() {


        Grid mainPanel = new Grid(WIDTH, HEIGHT);


        JFrame frame = new JFrame("Grid Test");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.getContentPane().add(mainPanel);

        frame.pack();

        frame.setResizable(false);

        frame.setLocationRelativeTo(null);

        frame.setVisible(true);

    }


    public static void main(String[] args) {

        SwingUtilities.invokeLater(() -> createAndShowGui());

    }

}

class Grid extends JPanel {

    private static final long serialVersionUID = 1L;

    private int prefW;

    private int prefH;



    public Grid(int prefW, int prefH) {

        this.prefW = prefW;

        this.prefH = prefH;

    }


    @Override

    public void paintComponent(Graphics g) {

        super.paintComponent(g);


        g.setColor(new Color(0,128,0));

        g.fillRect(0, 0, getWidth(), getHeight());

    }


    @Override

    public Dimension getPreferredSize() {

        if (isPreferredSizeSet()) {

            return super.getPreferredSize();

        }

        return new Dimension(prefW, prefH);

    }


}

此外,如果您所做的只是填充背景,则确实没有必要覆盖paintComponent。setBackground(new Color(0, 128, 0));在 Grid 构造函数中调用将设置它。当然,如果您要绘制其他东西,您可能需要paintComponent——但是如果它是一个网格,请考虑使用JLabels 的网格并设置它们的图标。


查看完整回答
反对 回复 2021-12-22
  • 1 回答
  • 0 关注
  • 212 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号