简体   繁体   English

当我在html代码中运行jar文件时,由于RuntimeException而无法正常工作

[英]When i run my jar-file in my html-code it won't work because of a RuntimeException

I made a jar-file of three classes that together make a game. 我制作了一个包含三个类的jar文件,它们共同构成了一个游戏。 I did it like this: 我这样做是这样的:

Made a manifest.mf file with: "MainClass: Spel (two enters)" in. Compiled my java-files to class-files. 用以下内容制作了manifest.mf文件:“ MainClass:拼写(两次进入)”。将Java文件编译为类文件。

and entered this command in my cmd: 并在我的cmd中输入以下命令:

jar cfm Spel.jar manifest.mf Spel.class Spel$1.class Spel$SpelTimerTask.class jar cfm Spel.jar manifest.mf Spel.class Spel $ 1.class Spel $ SpelTimerTask.class

The jar-file was created and when I dubble click on it, it works perfectly. 该jar文件已创建,当我双击它时,它可以完美运行。 But when I make my html-code like this and try to execute my java applet I get a RuntimeException, does anybody know why? 但是,当我使我的html代码像这样并尝试执行Java小程序时,我收到RuntimeException,有人知道为什么吗?

<html>
<body>
<applet archive="Spel.jar" code="Spel.class" width="600" height="400"></applet>
</body>
</html>

this is de code of my java-file: 这是我的java文件的代码:

import java.util.*;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class Spel extends JPanel implements KeyListener {

    public Rectangle screen, ball, block;
    public Rectangle bounds;
    public JFrame frame;
    public SpelTimerTask spelTask;
    public boolean down, right, starten = false;
    public JButton start;
    public int counter = 0;
    public JLabel score;
    public static int speed = 30;
    public static java.util.Timer spelTimer;
    public static Spel panel;

    public Spel(){
        super();
        screen = new Rectangle(0, 0, 600, 400);
        ball   = new Rectangle(0, 0, 20, 20);
        bounds = new Rectangle(0, 0, 600, 400);
        block = new Rectangle(bounds.width/2, bounds.height-50, 40, 10);
        frame = new JFrame("Super tof spel van Stan \u00a9");
        spelTask = new SpelTimerTask();
        score = new JLabel("0");
        score.hide();
        add(score);
        addKeyListener(this);
        setFocusable(true);
        start = new JButton("Start");
        start.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                starten = true;
                ball   = new Rectangle(0, 0, 20, 20);
                start.setFocusable(false);
                start.hide();
                score.show();
            }
        });
        add(start);
    }

    class SpelTimerTask extends TimerTask{
        public void run(){
            repaint();

            if(starten){
            moveBall();
            frame.repaint();
            }
        }
      }

      public void paintComponent(Graphics g){
        bounds = g.getClipBounds();
        g.clearRect(screen.x, screen.y, screen.width, screen.height);
        g.fillRect(ball.x, ball.y, ball.width, ball.height);
        g.fillRect(block.x, block.y, block.width, block.height);
      }

      public void moveBall(){
        if (right) ball.x+=3;
        else ball.x-=3;

        if (down)  ball.y+=3;
        else ball.y-=3;

        if (ball.x > (bounds.width - ball.width)) {
            right = false; 
            ball.x = bounds.width -  ball.width; 
        }

        if (ball.y >= (bounds.height - ball.height - 10) && ball.y <= (bounds.height - ball.height) && ball.x > block.x-20 && ball.x < block.x+40) { 
            down  = false; 
            ball.y = bounds.height - ball.height - 10;
            counter++;
            if(speed > 10){
                faster();
            }
            if(speed <= 10 && speed > 7 && counter % 5 == 0) {
                faster();
            }
            if(speed <= 7 && speed > 5 && counter % 10 == 0){
                faster();
            }
        }

        if (ball.y > (bounds.height - ball.height)) {
            start.show();
            score.hide();
            counter = 0;
        }

        if (ball.x <= 0) { 
            right = true; 
            ball.x = 0; 
        }

        if (ball.y <= 0){ 
            down  = true; 
            ball.y = 0; 
        }

        block.y = bounds.height-10;
        score.setText(Integer.toString(counter));
      }

      public void keyPressed(KeyEvent evt) {
          if(evt.getKeyCode() == KeyEvent.VK_LEFT && block.x > 0) {
              block.x -= 20;
          }

          if(evt.getKeyCode() == KeyEvent.VK_RIGHT && block.x < (bounds.width - block.width - 20)) {
              block.x += 20;
          }
      }

      public void keyTyped(KeyEvent evt){  }
      public void keyReleased(KeyEvent evt){ }

      public void startActionPerformed(java.awt.event.ActionEvent evt) {
            starten = true;
            speed = 10;
            panel.spelTask.cancel();
            panel.spelTask = new SpelTimerTask();
            spelTimer.schedule(panel.spelTask, 0, speed);
      }

      public void faster() {
        speed -= 1;
        panel.spelTask.cancel();
        panel.spelTask = new SpelTimerTask();
        spelTimer.schedule(panel.spelTask, 0, speed);
      }

      public static void main(String arg[]){
        spelTimer = new java.util.Timer();
        panel = new Spel();

        panel.down = true;
        panel.right = true;

        panel.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        panel.frame.setSize(panel.screen.width, panel.screen.height);
        panel.frame.setContentPane(panel); 
        panel.frame.setVisible(true);

        spelTimer.schedule(panel.spelTask, 0, speed);
      }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM