简体   繁体   English

为什么 repaint() 不在 swing 计时器中调用paintcomponent?

[英]Why is repaint() not calling paintcomponent in a swing timer?

When I use repaint() in the swing timer method startTimer, it just simply doesnt call paintcomponent().当我在 swing 计时器方法 startTimer 中使用 repaint() 时,它只是根本不调用paintcomponent()。 Everything works inside of the timer method except repaint().除了 repaint() 之外,一切都在计时器方法内部工作。 Im pretty new at java so if anyone could help me with this problem or point out any other errors I would greatly appreciate it.我是 java 的新手,所以如果有人可以帮助我解决这个问题或指出任何其他错误,我将不胜感激。 thank you谢谢你

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;
    }   
}

Since you didn't provide a runnable example I should suppose that you're doing something like由于您没有提供可运行的示例,我应该假设您正在做类似的事情

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

The problem is in boardImage() that creates a new instance of Board and adds that instance to the JFrame, not the original board , on which you start the timer.问题出在 boardImage() 中,它创建了一个新的 Board 实例,并将实例添加到 JFrame,而不是在您启动计时器的原始board中。

So replace所以更换

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

with

frame.getContentPane().add(this);

Consider also removing the explicit setSize on the frame and add override getPreferredSize on your custom component, then use frame.pack() to automatically adjust the frame size to the board.还可以考虑删除框架上的显式setSize并在您的自定义组件上添加覆盖getPreferredSize ,然后使用frame.pack()自动调整板的框架大小。

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

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