可能重复:
Adding image in JApplet
我将我的程序编译成applet的一个类文件,它运行并通过java工作但是当我将它添加到我的html网页时它不会加载图片,我有所有图片和类和html页面在一个文件中,它加载小程序,我可以点击一级,它只是赢得不画画。这是我的代码。
import javax.swing.JApplet;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class MainApplet extends JApplet {
public void init()
{
LabelFrame frame = new LabelFrame();
}
public class LabelFrame {
public LabelFrame( )
{
int x = 500;
// final LoadingPictures panel=new LoadingPictures();
setSize(x,500);
setLocation(350,200);
setVisible(true);
JMenuBar menubar = new JMenuBar();
setJMenuBar(menubar);
JMenu Change = new JMenu("Pick a Level");
menubar.add(Change);
JMenu Move = new JMenu("Move keys");
menubar.add(Move);
JMenu Options = new JMenu("Options");
menubar.add(Options);
JMenu About = new JMenu("About");
menubar.add(About);
JMenu Select = new JMenu("Select");
menubar.add(Select);
final JMenuItem black = new JMenuItem("Black BackGround");
Options.add(black);
final JMenuItem blue = new JMenuItem("Blue BackGround");
Options.add(blue);
final JMenuItem yellow = new JMenuItem("Yellow BackGround");
Options.add(yellow);
final JMenuItem green = new JMenuItem("Green BackGround");
Options.add(green);
final JMenuItem white = new JMenuItem("White BackGround");
Options.add(white);
JMenuItem info = new JMenuItem("infomation");
About.add(info);
JMenuItem Level2 = new JMenuItem("Level 1");
Change.add(Level2);
final JRadioButton redBlock = new JRadioButton("redblock");
final JButton moveleft = new JButton("Left");
final JButton moveright = new JButton("Right");
Select.add(redBlock);
Move.add(moveleft);
Move.add(moveright);
Level2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event1)
{
final LoadingPictures levelpics1 = new LoadingPictures(1);
add(levelpics1);
//levelpics1.setVisible(false);
//levelpics1.setVisible(true);
revalidate();
//repaint();
//Move the redblock right
redBlock.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent rightselect)
{
moveright.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent right)
{
if (LoadingPictures.topone ==400)
{
JOptionPane.showMessageDialog(null, "You have won the level now move to next level");
}
else
{LoadingPictures.topone+= 50;
levelpics1.repaint();}
}});
}});
//Move the redblock left
redBlock.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent leftselect)
{
moveleft.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent left)
{
if (LoadingPictures.topone == 0)
{
JOptionPane.showMessageDialog(null, "Can not move left anymore");
}
else
{
LoadingPictures.topone-= 50;
levelpics1.repaint();
}
}});
}});
int delay3 = 500; //milliseconds
ActionListener taskPerformer3 = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if(LoadingPictures.level2topleft == 0)
{
LoadingPictures.level2topleft+=325;
levelpics1.repaint();
}
else
{
LoadingPictures.level2topleft-=25;
levelpics1.repaint();
}
}
};
new Timer(delay3, taskPerformer3).start();
int delay2 = 1000; //milliseconds
ActionListener taskPerformer2 = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if(LoadingPictures.topleftone == 0)
{
LoadingPictures.topleftone+=325;
levelpics1.repaint();
}
else
{
LoadingPictures.topleftone-=25;
levelpics1.repaint();
}
}
};
new Timer(delay2, taskPerformer2).start();
int delay11 = 750; //milliseconds
ActionListener taskPerformer11 = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if(LoadingPictures.level2topfor4block == 300)
{
LoadingPictures.level2topfor4block-=300;
levelpics1.repaint();
}
else
{
LoadingPictures.level2topfor4block+=25;
levelpics1.repaint();
}
}
};
new Timer(delay11, taskPerformer11).start();
black.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
levelpics1.setBackground(Color.black);
}});
blue.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
levelpics1.setBackground(Color.blue);
}});
yellow.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
levelpics1.setBackground(Color.yellow);
}});
green.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
levelpics1.setBackground(Color.green);
}});
white.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
levelpics1.setBackground(Color.white);
}});
}
});
info.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event)
{
JOptionPane.showMessageDialog(null,"•This is our Final project for computer programming 3 \n" +
"•\n•Based off of the " +
"game by KIRA GAmes called UnBlock Me");
}});
}
}
public static class LoadingPictures extends JPanel{
ImageIcon block2vertical = new ImageIcon ("2block.jpg");
ImageIcon block4vertical = new ImageIcon("4block.jpg");
ImageIcon redblock = new ImageIcon("redblockhorizontal.jpg");
ImageIcon Level2block4 = new ImageIcon("Level2block4.jpg");
ImageIcon Level2block2 = new ImageIcon("Level2block2.jpg");
static int top = 0;
static int topone = 0;
static int topleft = 200;
static int level2topleft = 200;
static int topleftone = 225;
static int topfor4blockvertical = 150;
static int level2topfor4block = 150;
private int j;
public LoadingPictures(int i)
{
super();
j = i;
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if (j == 1)
{
g.drawImage(redblock.getImage(),topone,200,this);
g.drawImage(Level2block4.getImage(),350,level2topfor4block,this);
g.drawImage(Level2block2.getImage(),100,level2topleft,this);
g.drawImage(Level2block2.getImage(),200,topleftone,this);
}
else if(j == 2)
{
}
}
}
}
这是我在html中加载applet的代码行
<applet code ="MainApplet.class" height = "500" width = "500"></applet>