public class Interface extends JFrame
{
public Interface() throws InterruptedException
{
//windowsetup
pan = new MainPanel();
Thread t = new Thread(pan);
t.start();
add(pan);
addKeyListener(pan);
}
public static void main(String[] Args) throws InterruptedException
{
Interface proj = new Interface();
}
}
////////
public class MainPanel extends JPanel implements KeyListener, Runnable
{
public void paint(Graphics g)
{
//painting
}
public void run()
{
while(true)
{
this.repaint();
//other codes
try
{
Thread.sleep(10);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
I have a code that looks like this. When I press the run button in Eclipse, sometimes it works properly, but sometimes the there would be nothing in the window at all, nothing painted and keys doesn't work.
I heard that using threads in GUI might cause concurrency problems, and I wonder if this is the case, and what I should do to correct it. Thanks.