当我实例化一个新线程时,main方法终止

时间:2017-05-10 13:50:53

标签: java java-threads

我正在编写一个自上而下的射击游戏,我想在每个游戏关卡之间打开一个新窗口。在每个级别之后,如果满足某个条件,则实现以下类:

import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.Image.*;
import java.awt.image.BufferedImage.*;
import java.text.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.border.*;
import javax.swing.text.*;
import javax.imageio.ImageIO;
import java.io.InputStream.*;
import java.io.*;

public class Shop extends JFrame implements Runnable
{
    private static java.awt.image.BufferedImage shotgun, zombie, armor, aRifle;
    static ArrayList<Image> stock = new ArrayList<Image>();
    private ColorPanel contentPane3;
    private JPanel container = new JPanel();
    private JPanel contentPane4;
    private int item=0;
    private boolean broke = false;
    private boolean newb = true;
    private boolean reg = false;
    private boolean stay = true;

    public static void main(String [] args)
    {
        System.out.println("started");
        Thread s = new Thread( new Shop () );
        System.out.println("running");
        s.start();
        System.out.println("ended");
    }

    public Shop()
    {        
        super("Monster Escape v4");

        try
        {
            shotgun = ImageIO.read(getResource("shotgun.png"));        
            zombie = ImageIO.read(getResource("zombie.png"));        
            armor = ImageIO.read(getResource("armor.png"));
            aRifle = ImageIO.read(getResource("assaultRifle.png"));
        }catch (IOException e){System.exit(0);}

        stock.clear();
        stock.add(armor);
        if(!(Client.inventory.contains("shotgun")))
            stock.add(shotgun);            
        if(!(Client.inventory.contains("assaultRifle")))
            stock.add(aRifle);

        container.removeAll();

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        contentPane4 = new JPanel();
        contentPane4.setPreferredSize(new Dimension( 700,100 ));
        contentPane3 = new ColorPanel(Color.WHITE);
        contentPane3.setPreferredSize(new Dimension( 700,700 ));
        setBounds( 100,100,606,719 );
        setResizable(false);

        GridLayout g1 = new GridLayout(1, 3);
        contentPane4.setLayout(g1);
        container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));

        JButton sleft = new JButton("←");
        JButton sright = new JButton("→");
        JButton buy = new JButton("buy");
        JButton leave = new JButton("leave");

        contentPane4.add(sleft);
        contentPane4.add(buy);
        contentPane4.add(sright);
        contentPane4.add(leave);

        sleft.addActionListener((e) -> {
            broke = false;
            newb = false;
            if(item==0)
                item = stock.size()-1;
            else
                item--;
        });

        sright.addActionListener((e) -> {
            broke = false;
            newb = false;
            if(item==stock.size()-1)
                item=0;
            else
                item++;
        });

        buy.addActionListener((e) -> {
            broke = false;
            newb = false;
            if(stock.get(item) == armor)
            {
                if(Client.cash >= 100)
                {
                    Client.maxHealth += 4;
                    Client.cash -= 100;
                    reg = true;
                }
                else
                    broke = true;
            }
            else
            {
                if(Client.cash >= 250)
                {
                    Client.inventory.add(getName(item));
                    stock.remove(item);
                    Client.cash -= 250;
                    item--;
                    reg = true;
                }
                else
                    broke = true;
            }

        });

        leave.addActionListener((e) -> {
            stay = false;
        });

        container.add(contentPane3, BorderLayout.EAST);
        container.add(contentPane4, BorderLayout.WEST);
        setContentPane(container);



        setVisible(true);
    }

    public static String getName(int i)
    {
        String temp = null;
        if(stock.get(i) == armor)
            temp = "armor";
        else if(stock.get(i) == shotgun)
            temp = "shotgun";
        else if(stock.get(i) == aRifle)
            temp = "assaultRifle";
        return temp;
    }

    private InputStream getResource(String ref) throws IOException
    {
        InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(ref);
        if( in != null )
            return in;
        return new FileInputStream(ref);

    }

    public void run()
    {


        while(stay)
        {
            repaint();

            try
            {
                Thread.sleep(10);
            }catch( InterruptedException e ) { }

        }
        dispose();
    }


}

这包括未显示的ColorPanel子类。在我以前的游戏版本中,这个工作正常。但是,我最近向我的客户端添加了一个KeyListener子类,现在当我尝试实例化该线程时没有任何反应。我在Shop类中创建了一个main方法,以查看问题所在,并且似乎在代码到达时     Thread s = new Thread(new Shop()); 系统完全终止,因为当我运行main时它只打印&#34;启动。&#34;造成这种情况的原因是什么?

1 个答案:

答案 0 :(得分:0)

添加 s.join();

开始后。在这种情况下,主线程将等待直到s线程完成。