import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.Timer;
public class CountingSheep extends JApplet
{
private Image sheepImage;
private Image backgroundImage;
private GameBoard gameBoard;
private scoreBoard scoreBoard;
public void init()
{
loadImages();
gameBoard = new GameBoard(sheepImage, backgroundImage);
scoreBoard = new scoreBoard();
getContentPane().add(gameBoard);
getContentPane().add(scoreBoard);
}
public void loadImages()
{
sheepImage = getImage(getDocumentBase(), "sheep.png");
backgroundImage = getImage(getDocumentBase(), "bg.jpg");
}
}
当GameBoard
类添加JApplet
类时,程序正常工作,但是,当我尝试添加ScoreBoard
类时,两个Panel
类都可以不在Applet上显示。我猜这现在归结为定位?有什么想法吗?
答案 0 :(得分:5)
一些建议:
paintComponent(Graphics g)
方法中绘制,然后将该JPanel添加到JApplet的contentPane中。 super.paintComponent(g);
,它将刷新JPanel的图形。您上面发布的代码对我来说有点混乱。你想用它做什么?您已经向JApplet添加了组件,这些组件应该处理它们自己的图形,然后您也在JApplet上绘制。你想要达到什么样的行为?
答案 1 :(得分:1)