导出的游戏无法正常工作

时间:2015-07-02 19:50:34

标签: java jar jframe

我制作了一个简单的Java游戏,由2个类组成(使用JFrame)。我试图将它导出到Runnable jar,但由于某种原因,当我尝试运行它时它永远不会工作。谢谢!

这是我的代码:

主要类

    RussianRoulette game = new RussianRoulette();
    game.setVisible(true);
    game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    game.setSize(220, 200);
    game.setResizable(false);

}
}

其他类

int lives = 5;
JTextField livesDisplay;
JButton gunButton;
ImageIcon gunCold;
ImageIcon gunActive;
JOptionPane endPane = new JOptionPane();
int endProgram;
int clicked = 0;
JTextField display;


public RussianRoulette() {
    super("Russian Roulette");
    Font font = new Font("Western", 20, 20);

    gunCold = new ImageIcon(getClass().getResource("guncold.gif"));
    gunActive = new ImageIcon(getClass().getResource("gunactive.gif"));

    JPanel buttonPanel = new JPanel();
    JPanel livesPanel = new JPanel();
    JPanel displayPanel = new JPanel();

    display = new JTextField("  Click Gun To Play!", 15);

    livesDisplay = new JTextField("Lives: " + lives, 8);
    livesDisplay.setEditable(false);
    livesDisplay.setFont(font);
    livesDisplay.setBackground(Color.BLACK);
    livesDisplay.setForeground(Color.WHITE);

    livesPanel.add(livesDisplay);
    livesPanel.setBackground(Color.BLACK);

    displayPanel.add(display);
    displayPanel.setBackground(Color.BLACK);
    display.setFont(font);
    display.setEditable(false);
    display.setBackground(Color.BLACK);
    display.setForeground(Color.WHITE);

    LayoutManager overlay = new OverlayLayout(buttonPanel);
    buttonPanel.setLayout(overlay);
    buttonPanel.setBackground(Color.BLACK);
    gunButton = new JButton(gunCold);
    gunButton.setToolTipText("Click To Pull Trigger");
    gunButton.setAlignmentX(0.4f);
    gunButton.setAlignmentY(0.0f);
    buttonPanel.add(gunButton);

    add(buttonPanel, BorderLayout.CENTER);
    add(livesPanel, BorderLayout.NORTH);
    add(displayPanel, BorderLayout.SOUTH);

    gunButton.addActionListener(new ActionListener() {
        @SuppressWarnings("static-access")
        public void actionPerformed(ActionEvent event) {
            Random r = new Random();
            int rand = r.nextInt(6);
            clicked++;
            switch (rand) {
            case 0:
                display.setText(" Bang! You lost a life.");
                lives--;
                livesDisplay.setText("Lives: " + lives);
                break;
            case 1:
                display.setText("      You're alive!");
                break;
            case 2:
                display.setText("      You're alive!");
                break;
            case 3:
                display.setText("      You're alive!");
                break;
            case 4:
                display.setText("      You're alive!");
                break;
            case 5:
                display.setText("      You're alive!");
                break;
            }

            Timer imageSwitch = new Timer(984, new TimerListener());
            imageSwitch.setRepeats(false);
            if (rand == 0) {
                imageSwitch.start();
                gunButton.setIcon(gunActive);
            } else {
                gunButton.setIcon(gunCold);
            }

            if (lives == 0) {
                JFrame end = new JFrame();
                endPane = new JOptionPane();
                end.add(endPane);
                endProgram = endPane.showOptionDialog(null, "You survived "
                        + clicked + " trigger pulls!", "Game Over",
                        endPane.OK_CANCEL_OPTION,
                        endPane.INFORMATION_MESSAGE, null, null, null);

                if (endProgram == endPane.OK_OPTION) {
                    System.exit(0);
                } else if (endProgram == endPane.CANCEL_OPTION) {
                    System.exit(0);
                } else if (endProgram == endPane.CLOSED_OPTION) {
                    System.exit(0);
                }
            }

        }

    });
}

private class TimerListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        gunButton.setIcon(gunCold);

    }

    {
        if (lives == 0) {
            display.setText("  Bang! You're dead.");
        }
    }

}

}

1 个答案:

答案 0 :(得分:1)

我明白了。我必须将图像的路径设置为我的项目中的包,其中包含我的图片。

gunCold = new ImageIcon(getClass().getClassLoader().getResource("images/guncold.gif"));
gunActive = new ImageIcon(getClass().getClassLoader().getResource("images/gunactive.gif"));