簡體   English   中英

當我在html代碼中運行jar文件時,由於RuntimeException而無法正常工作

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

我制作了一個包含三個類的jar文件,它們共同構成了一個游戲。 我這樣做是這樣的:

用以下內容制作了manifest.mf文件:“ MainClass:拼寫(兩次進入)”。將Java文件編譯為類文件。

並在我的cmd中輸入以下命令:

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

該jar文件已創建,當我雙擊它時,它可以完美運行。 但是,當我使我的html代碼像這樣並嘗試執行Java小程序時,我收到RuntimeException,有人知道為什么嗎?

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

這是我的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