我制作了一个包含三个类的jar文件,它们组成了一个游戏。我是这样做的:
制作了一个manifest.mf文件:" MainClass:Spel(两个进入)"在。 将我的java文件编译为类文件。
并在我的cmd中输入此命令:
jar cfm Spel.jar manifest.mf Spel.class Spel $ 1.class Spel $ SpelTimerTask.class
创建了jar文件,当我点着它点击它时,它完美地运行。但是,当我像这样制作我的html代码并尝试执行我的java applet时,我得到一个RuntimeException,有人知道为什么吗?
<html>
<body>
<applet archive="Spel.jar" code="Spel.class" width="600" height="400"></applet>
</body>
</html>
答案 0 :(得分:0)
这是我的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);
}
}