-1
하향식 사수 게임을 프로그래밍 중이며 게임의 각 레벨 사이에 새 창을 열고 싶습니다. 특정 기준은 다음 충족되는 경우 각 단계 후, 다음 클래스가 구현됩니다새 스레드를 인스턴스화 할 때 주 메소드가 종료됩니다.
는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 서브 클래스가 포함되어 있습니다. 이전 버전의 게임에서는 정상적으로 작동했습니다. 그러나 최근에 Client에 KeyListener 하위 클래스를 추가했는데 이제는 스레드를 인스턴스화 할 때 아무 일도 발생하지 않습니다. 문제가 무엇인지 알기 위해 Shop 클래스 자체에 main 메소드를 만들었고 코드가 도달 할 때 나타나는 것으로 보입니다 Thread s = new Thread (new Shop()); 메인을 실행할 때 "시작됨"만 인쇄되기 때문에 시스템이 완전히 종료됩니다. 이 원인은 무엇입니까?
프로그램이 실제로 종료됩니까? –
메소드 호출을 확인하고 "VM terminated"라고 말합니다. 그러나 내 코드를 면밀히 살펴보면 내 이미지를 인스턴스화하려고 할 때 IOException을 잡는 중임을 알았습니다. IOException은 프로그램에서 발생했을 경우 종료하도록 지시 한 것입니다. 메서드가 잘 실행 중입니다. –
예,이 줄은 도움이되지 않습니다. 'catch (IOException e) {System.exit (0);}'. –