为什么我的JFrame只是一个黑色的窗口? [英] Why is my JFrame just a black window?

查看:287
本文介绍了为什么我的JFrame只是一个黑色的窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码是这样的,当我运行它时,我只得到一个黑色的窗口,不知道为什么.

My code is this, when I run it I just get a black window and I have no idea why.

感谢您的任何反馈. 它应该打印出一张图片,并最终使其移动.

Thanks for any feedback. Its supposed to print out a picture, and eventually make it move.

package assignment04;

import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class GoLDriver
{
    public static void main(String[] args)
    {
        GoLModel model= new GoLModel();
        JFrame frame = new JFrame();
        JPanel panel= new JPanel();
        panel.setLayout(new BorderLayout());
        panel.setPreferredSize(new Dimension(400, 300));
        model.initialize();
        frame.add(panel);
        frame.pack();
        frame.setTitle("Game of Life");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        GoLComponent component = new GoLComponent(model,15,20,20);
        panel.add(component);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        for(int i=0; i <40; i++)
        {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            model.count();
            model.update();
            panel.repaint();
        }
    }
}

推荐答案

最可能的原因是您阻塞了事件调度线程,阻止了它的绘制...

This most likely cause is your block the Event Dispatching Thread, prevent it from painting...

for(int i=0; i <40; i++)
{
    try {
        Thread.sleep(100);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    model.count();
    model.update();
    panel.repaint();
}

看看 Swing中的并发

Take a look at Concurrency in Swing and How to use SwingTimer and then do a search for animation in swing on Google for more suggestions...

这篇关于为什么我的JFrame只是一个黑色的窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆