我将创建一个JPanel Howto,它将按顺序渲染6张图像,然后添加“后退”按钮以返回标题。

我正在尝试检查它是否在类howto中调用paintComponent方法。而且不是这样,我的值仍然为0并最终陷入循环

这是我的方法课

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;

import javax.swing.JButton;
import javax.swing.JPanel;

import Audio.HitSound;
import Graphic.DrawingUtility;

public class Howto extends JPanel implements Runnable{
    private JButton back;
    private static int i;
    public Howto(){
    this.setPreferredSize(new Dimension(800, 600));
    this.setLayout(null);
    repaint();
    HitSound h = new HitSound();
    i=0;
    this.setVisible(true);
    }

    public void run() {
        // TODO Auto-generated method stub
        try {
            while(i<6)
            {
                GameManager.frame.repaint();
                Thread.sleep(10);
                System.out.println("i: " + i);
            }
        } catch (Exception e) {
            // TODO: handle exception
            Thread.interrupted();
        }
    }
    public void paintComponent(Graphics g){
        if(GameManager.thread.isAlive()){
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            g2d.drawImage(DrawingUtility.getHowto(i), 0, 0, 800, 600,null);
            i++;
        }
    }
}


这在我的GameManager类中

public static void startThread() {
        thread = new Thread(howto);
        thread.start();
    }

public static void runHowto() {
    howto = new Howto();
    howto.setVisible(true);
    frame.switchScene(howto);
    howto.repaint();
    startThread();
    while (thread.isAlive()) {
        //frame.repaint();
    }
    if (!thread.isAlive()) {
        JButton back = new JButton();
        back.setBorderPainted(false);
        back.setContentAreaFilled(false);
        back.setFocusPainted(false);
        back.setOpaque(false);
        back.setBounds(640, 446, 132, 132);
        back.addMouseListener(new MouseListener() {

            @Override
            public void mouseReleased(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mousePressed(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseExited(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseEntered(MouseEvent e) {
                // TODO Auto-generated method stub
                HitSound h = new HitSound();
                h.play(3);
            }

            @Override
            public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub
                GameManager.goToTitle();
            }
        });
    }


但是当我调用runHowto();它仍然在循环并且我== 0而不增加我
所以我认为paintComponent和repaint方法可能有问题

所以,请帮助我修复它。

最佳答案

while (thread.isAlive())循环阻止AWT事件调度线程(EDT)。

您需要从runHowto返回事件分发循环。让您的Howto任务使用java.awt.EventQueue.invokeLater发布回EDT来构造后退按钮,并大概将其添加到面板中。

10-07 16:21