Runnable JAR文件无法打开JFrame

时间:2014-05-25 02:05:42

标签: java swing jar jframe

我的JAR文件没有运行,我遇到了一个神秘的问题。我的游戏打开一个JFrame对象,其中显示所有图形。一切都在Eclipse中工作,但是当我尝试将完成的游戏导出为Runnable JAR文件,然后从我的桌面双击它时,没有任何反应。我没有收到错误消息或任何内容,我的JFrame没有弹出,所以没有游戏可玩。

我确保我的课程中没有任何错误或警告,但仍然无法打开JFrame。此外,当我右键单击桌面上所谓的Runnable JAR文件,然后转到属性,然后单击表示安全性的选项卡时,SYSTEM下的允许类别下面都有复选标记。

这是我的主要类,如果你需要它,虽然我怀疑实际编码有问题,因为它在Eclipse中工作正常,它只是不能从我的桌面打开:

package com.wierd0games.Under.main;

import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.util.Random;

import javax.swing.JFrame;

import com.wierd0games.Under.graphics.Draw;
import com.wierd0games.Under.graphics.Sprite;
import com.wierd0games.Under.graphics.SpriteSheet;
import com.wierd0games.Under.input.Keyboard;
import com.wierd0games.Under.mobs.Frog;
import com.wierd0games.Under.mobs.Healthbar;

public class Main extends Canvas implements Runnable {
private static final long serialVersionUID=1L;
public static final int WIDTH=300;
public static final int HEIGHT=WIDTH*9/16;
public static final int SCALE=3;

private JFrame frame;
private boolean running=false;
private Thread thread;
private Draw draw;
private Keyboard keyboard;

private int frames;
private int updates;
private final long TIME_BETWEEN_UPDATES=1000000000/60;

BufferedImage image=new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
int[] pixels=((DataBufferInt)image.getRaster().getDataBuffer()).getData();

Frog frog;
private int[] sideCollisions=new int[0];
private int[] verticalCollisions=new int[0];
private final int WATER_LEVEL=1600;

private Generator generator;
private Healthbar healthbar;
private int score=0;
private int time=60;
private int lastScore=0;
private int lastTimeAlive=0;

private int room=0;
private int counter=0;
private Sprite openingScreenToDraw=new Sprite(1, 1, 300, SpriteSheet.backgrounds);
Random random=new Random();

public static void main(String[] args) {
    Main game=new Main();
    game.frame.setResizable(false);
    game.frame.setTitle("Under           |        FPS: ---  Updates: --");
    game.frame.add(game);
    game.frame.pack();
    game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    game.frame.setLocationRelativeTo(null);
    game.frame.setVisible(true);
    game.start();
}

private Main() {
    frame=new JFrame();
    Dimension size=new Dimension(WIDTH*SCALE, HEIGHT*SCALE);
    frame.setPreferredSize(size);

    keyboard=new Keyboard();
    addKeyListener(keyboard);
    draw=new Draw(WIDTH, HEIGHT, pixels);

    frog=new Frog(0, 1632, 0.01, .98, .94, .94, WATER_LEVEL, keyboard);
    generator=new Generator(WATER_LEVEL);
    healthbar=new Healthbar(frog.MAX_HUNGER, frog.MAX_BREATH, (int)(frog.MAX_JUMP_CHARGE*100)-40);
}

private void update() {
    if (room==0) {
        if (++counter>300) {
            room=1;
            counter=-60;
            openingScreenToDraw=Sprite.titleScreen;
        }
        else {
            if (counter>120&&counter<280) {
            for (int i=0; i<openingScreenToDraw.pixels.length; i++) {
                if (openingScreenToDraw.pixels[i]!=0) {
                    if (random.nextInt((300-counter-20))==0) {
                        openingScreenToDraw.pixels[i]=0;
                    }
                }
            }
        }
        }
    }

    if (room==1) {
        keyboard.update();
        if (++counter>=0) {
            if (counter>40) {
                counter=0;
                if (openingScreenToDraw==Sprite.titleScreen) {
                    openingScreenToDraw=Sprite.titleScreen2;
                }
                else {
                    openingScreenToDraw=Sprite.titleScreen;
                }
            }

            if (keyboard.keysPressed[10]) {
                room=2;
                counter=0;
            }
        }
    }

    if (room==2) {
        if (++counter>=60) {
            counter=0;
            time--;
        }
        keyboard.update();
        for (int i=0; i<keyboard.keys.length; i++) {
            if (keyboard.keysPressed[i]) {
                System.out.println("Key "+i+" Pressed");
            }
        }
        int[] returned=generator.update((int)frog.x, (int)frog.y);
        int fliesToEat=returned[0];
        int chestsToCollect=returned[1];
        score+=fliesToEat*10+chestsToCollect*100;

        frog.update(sideCollisions, verticalCollisions, fliesToEat);
        healthbar.update(frog.hunger, frog.breath, (int)(frog.jumpCharge*100)-40);
        draw.update(((int)frog.x+(frog.WIDTH+frog.BLANK_LEFT)/2)*-1+WIDTH/2, ((int)frog.y+(frog.HEIGHT+frog.BLANK_TOP)/2)*-1+HEIGHT/2);
        sideCollisions=generator.getSideCollisions();
        verticalCollisions=generator.getVerticalCollisions();

        if (frog.hunger<=0) {
            room=3;
            counter=0;
            lastScore=score;
            lastTimeAlive=60-time;
            score=0;
            time=60;
            frog=new Frog(0, 1632, 0.01, .98, .94, .94, WATER_LEVEL, keyboard);
            generator=new Generator(WATER_LEVEL);
            healthbar=new Healthbar(frog.MAX_HUNGER, frog.MAX_BREATH, (int)(frog.MAX_JUMP_CHARGE*100)-40);
        }
        if (frog.breath<=0) {
            room=4;
            counter=0;
            lastScore=score;
            lastTimeAlive=60-time;
            score=0;
            time=60;
            frog=new Frog(0, 1632, 0.01, .98, .94, .94, WATER_LEVEL, keyboard);
            generator=new Generator(WATER_LEVEL);
            healthbar=new Healthbar(frog.MAX_HUNGER, frog.MAX_BREATH, (int)(frog.MAX_JUMP_CHARGE*100)-40);
        }
        if (time<=0) {
            room=5;
            counter=0;
            lastScore=score;
            lastTimeAlive=60-time;
            score=0;
            time=60;
            frog=new Frog(0, 1632, 0.01, .98, .94, .94, WATER_LEVEL, keyboard);
            generator=new Generator(WATER_LEVEL);
            healthbar=new Healthbar(frog.MAX_HUNGER, frog.MAX_BREATH, (int)(frog.MAX_JUMP_CHARGE*100)-40);
        }
    }

    if (room==3) {
        keyboard.update();
        if (keyboard.keysPressed[10]) {
            room=1;
        }
    }
    if (room==4) {
        keyboard.update();
        if (keyboard.keysPressed[10]) {
            room=1;
        }
    }
    if (room==5) {
        keyboard.update();
        if (keyboard.keysPressed[10]) {
            room=1;
        }
    }
    if (room==6) {
        keyboard.update();
        if (keyboard.keysPressed[10]) {
            room=1;
        }
    }

}

private void render() {
    BufferStrategy bs=getBufferStrategy();
    if (bs==null) {
        createBufferStrategy(3);
        return;
    }

    for (int i=0; i<pixels.length; i++) {//sets the background color to white so that the clouds don't have holes in them
        pixels[i]=-1;
    }

    if (room==0) {
        draw.drawSpriteToScreen(0, 0, openingScreenToDraw);
    }

    if (room==1) {
        draw.drawSpriteToScreen(0, 0, openingScreenToDraw);
    }

    if (room==2) {
        draw.drawSpriteToScreen(0, Math.min(0, -1*(int)frog.y+WATER_LEVEL-HEIGHT/2-2), Sprite.sky);
        draw.drawSpriteToScreen(0, Math.max(0, -1*(int)frog.y+HEIGHT+WATER_LEVEL-HEIGHT/2-2), Sprite.water);

        frog.drawSelf(draw);
        generator.drawBlocks(draw);
        healthbar.drawSelf(draw, score, time);
    }
    if (room==3) {
        draw.drawSpriteToScreen(0, 0, Sprite.youStarved);
        draw.drawNumberToScreen(80, 64, lastScore);
        draw.drawNumberToScreen(117, 79, lastTimeAlive);
    }
    if (room==4) {
        draw.drawSpriteToScreen(0, 0, Sprite.youDrowned);
        draw.drawNumberToScreen(80, 64, lastScore);
        draw.drawNumberToScreen(117, 79, lastTimeAlive);
    }
    if (room==5) {
        draw.drawSpriteToScreen(0, 0, Sprite.outOfTime);
        draw.drawNumberToScreen(80, 64, lastScore);
        draw.drawNumberToScreen(117, 79, lastTimeAlive);
    }
    if (room==6) {
        draw.drawSpriteToScreen(0, 0, Sprite.youWereEaten);
        draw.drawNumberToScreen(80, 64, lastScore);
        draw.drawNumberToScreen(117, 79, lastTimeAlive);
    }

    Graphics g=bs.getDrawGraphics();
    g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
    g.dispose();
    bs.show();
}

private synchronized void start() {
    running=true;
    thread=new Thread(this, "Under");
    thread.start();
}

private synchronized void stop() {
    running=false;
    try {
        thread.join();
    }
    catch (InterruptedException e) {
        e.printStackTrace();
    }
}

public void run() {
    long lastNanoTime=System.nanoTime();
    long lastMilliTime=System.currentTimeMillis();
    updates=0;
    frames=0;
    requestFocus();

    while (running) {
        long currentNanoTime=System.nanoTime();
        while (currentNanoTime-lastNanoTime>=TIME_BETWEEN_UPDATES) {
            lastNanoTime+=TIME_BETWEEN_UPDATES;
            update();
            updates++;
        }
        render();
        frames++;
        while (lastMilliTime+1000<System.currentTimeMillis()) {
            lastMilliTime+=1000;
            frame.setTitle("Under           |        FPS: "+frames+"  Updates: "+updates);
            updates=0;
            frames=0;
        }
    }
    stop();
}

}

以下是可以下载JAR的链接: https://sites.google.com/site/wierd0games/ludum-dare-under

2 个答案:

答案 0 :(得分:2)

抛出的异常是:

Exception in thread "main" java.lang.ExceptionInInitializerError
        at com.wierd0games.Under.graphics.Sprite.<clinit>(Sprite.java:13)
        at com.wierd0games.Under.main.Main.<init>(Main.java:53)
        at com.wierd0games.Under.main.Main.main(Main.java:57)
Caused by: java.lang.IllegalArgumentException: input == null!
        at javax.imageio.ImageIO.read(ImageIO.java:1388)
        at com.wierd0games.Under.graphics.SpriteSheet.load(SpriteSheet.java:27)
        at com.wierd0games.Under.graphics.SpriteSheet.<init>(SpriteSheet.java:22)
        at com.wierd0games.Under.graphics.SpriteSheet.<clinit>(SpriteSheet.java:13)
        ... 3 more

如果在命令行上使用java -jar Under-LD.jar运行jar,则可以看到异常。

答案 1 :(得分:1)

您是否在run config中选择了主类。

运行&gt;运行配置&gt;主类&gt;选择你的主要课程。 接下来清理并再次构建。然后在项目的dist文件夹中打开jar文件。