Why is my JFrame just a black window?

余生颓废 提交于 2020-01-06 14:39:03

问题


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

回答1:


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

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...




回答2:


I'd strongly recommend against coding within the static main method while using Java's swing framework.

A better layout (but wouldn't resolve your problem) would be:

public class GoLDriver
{    
    private GoLModel model;
    private JFrame frame;
    private JPanel panel;
    private GoLComponent component;

    public GoLDriver() {
        model= new GoLModel();
        frame = new JFrame();
        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);
        component = new GoLComponent(model,15,20,20);
        panel.add(component);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

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

    public static void main(String[] args)
    {
        GoLDriver gld = new GoLDriver();

        gld.startDriver();
    }
}

Given this looks like a coding assignment for a class, instead of answering further, I'd recommend looking at the links MadProgrammer posted. This refactor will help make applying the lessons in those links easier to practice.

Basic rule of thumb is, don't put your processing code in the same thread as your display code. Otherwise, the processing code will prevent the display code (event dispatching thread) from working as it normally would. Better to either leverage the SpringTimer/events model, or create a new thread to handle your processing code. See the concurrency lesson for a good introduction on the basics of using threads in Java.



来源:https://stackoverflow.com/questions/22057731/why-is-my-jframe-just-a-black-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!