【问题标题】:Why is repaint() not calling paintcomponent in a swing timer?为什么 repaint() 不在摆动计时器中调用paintcomponent?
【发布时间】:2021-06-03 12:42:58
【问题描述】:

当我在摇摆计时器方法 startTimer 中使用 repaint() 时,它只是根本不调用paintcomponent()。除了 repaint() 之外,一切都在计时器方法内部工作。我在java方面很新,所以如果有人可以帮助我解决这个问题或指出任何其他错误,我将不胜感激。谢谢

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class Board extends JPanel {
    Timer timer;
    int count;
    int clockNumber;
    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.fillRect(30, 30, 640, 640);//makes a black square
        for(int i=30;i<=510; i+=160)//adds white columns
        {
            for(int j=30; j<=510; j+=160)
            {
                g.clearRect(i,  j,  80,  80);
            }
        }
        for(int i=110; i<=590; i+=160)//adds black columns
        {
            for(int j=110; j<=590; j+=160)
            {
                g.clearRect(i, j, 80, 80);
            }
        }
        g.setFont(new Font("Monospace", Font.BOLD, 30));
        g.setColor(Color.WHITE);
        g.drawString("a", 85, 660);
        g.drawString("c", 245, 660);
        g.drawString("e", 405, 660);
        g.drawString("g", 565, 660);
        g.drawString("7", 35, 140);
        g.drawString("5", 35, 300);
        g.drawString("3", 35, 460);
        g.drawString("1", 35, 620);
        
        g.setColor(Color.BLACK);
        g.drawString("b", 165, 660);
        g.drawString("d", 325, 660);
        g.drawString("f", 485, 660);
        g.drawString("h", 645, 660);
        g.drawString("8", 35, 60);
        g.drawString("6", 35, 220);
        g.drawString("4", 35, 380);
        g.drawString("2", 35, 540);
        System.out.println(clockNumber);
        g.drawString(String.valueOf(clockNumber), 300, 300);
    }
    public void showX(Graphics g)
    {
        g.setFont(new Font("wrongFont", Font.BOLD, 200)); 
        g.setColor(Color.RED);
        g.drawString("X", 35, 540);
    }
    public void boardImage()
    {
        JFrame frame=new JFrame();
        frame.setSize(600, 600);
        frame.getContentPane().add(new Board());
        frame.setLocationRelativeTo(null);
        frame.setBackground(Color.LIGHT_GRAY);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        CoordinateGame game=new CoordinateGame();
        frame.addMouseListener(new MouseAdapter() 
        {
            public void mouseClicked(MouseEvent e) 
            {
                int x=e.getX();
                int y=e.getY();
                    
            }
        }); 
    }
    public int clockNumber()
    {
        return clockNumber;
    }
    public void startTimer(int seconds)
    {
        ActionListener action=new ActionListener()
        {
            public void actionPerformed(ActionEvent e) 
            {
                clockNumber=count;
                if(count==0)
                {
                    timer.stop();
                }
                else
                {
                    System.out.println(count);
                    clockNumber--;
                    count--;
                    repaint();
                }
            }
            
        };
        timer=new Timer(1000, action);
        timer.setInitialDelay(0);
        timer.start();
        count=seconds;
    }   
}

【问题讨论】:

  • (1-) 框架的创建不应该是 Board 类的一部分。您的 main() 方法应该创建 JFrame 并将 Board 组件添加到框架中。我在您的最后一个问题中为您提供了 Custom Painting 上的 Swing 教程的链接:stackoverflow.com/q/67807288/131872 并声明您应该使用工作代码作为起点,这样您将拥有更好的结构化代码。您没有遵循本教程,也没有更好的结构化代码。您不应该将 MouseListener 添加到框架中。这不是本教程所做的。阅读教程!!!

标签: java swing graphics paint repaint


【解决方案1】:

由于您没有提供可运行的示例,我应该假设您正在做类似的事情

Board board=new Board();
board.boardImage();
board.startTimer();

问题出在 boardImage() 中,它创建了一个新的 Board 实例并将 那个 实例添加到 JFrame,而不是您启动计时器的原始 board

所以替换

frame.getContentPane().add(new Board());

frame.getContentPane().add(this);

考虑同时删除框架上的显式 setSize 并在您的自定义组件上添加覆盖 getPreferredSize,然后使用 frame.pack() 自动调整框架大小以适应电路板。

【讨论】:

    猜你喜欢
    • 2022-12-17
    • 2017-01-03
    • 1970-01-01
    • 2015-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-28
    相关资源
    最近更新 更多