簡體   English   中英

將JApplet嵌入到JFrame中

[英]Embedding a JApplet into a JFrame

好的,我想知道是否有人能告訴我哪里出錯了。

我有一個名為Game的JApplet,如果我在eclipse中使用AppletViewer和一個名為GUI的JFrame來運行它,它運行正常。 以下是兩個代碼:

PS。 我從Game中刪除了大部分代碼,使其變小,現在只是基本的。

Game.java:

package com.ion.brickdodge;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;

import javax.swing.JApplet;


public class Game extends JApplet implements Runnable{

private static final long serialVersionUID = 1L;
Image man1;
Image man2;
Image cman; 
int WIDTH = 250;
int HEIGHT = 350;
int lives = 3;
int spx = WIDTH / 2;
int score = 0;

Image dbImage;
Graphics dbg;

public void init () {
    setSize  (WIDTH, HEIGHT);
    setFocusable(true);
    setBackground(Color.LIGHT_GRAY);

    man1 = getImage(getCodeBase(), "res/man1.png");
    man2 = getImage(getCodeBase(), "res/man2.png");

    cman = man1;

}

public void start() {
    Thread th = new Thread(this);
    th.start();
}

public void run() {
    Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
    while(true){
        repaint();

        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
    }   
}

public void paint(Graphics g) {

    g.drawImage(cman, spx, 320, this);
    g.setColor(Color.BLACK);
    g.drawString("Score: " + score, 5, 10);
    g.drawString("Lives: " + lives, 5, 25);

}

public void update(Graphics g){
    if (dbImage == null)
    {
        dbImage = createImage (this.getSize().width, this.getSize().height);
        dbg = dbImage.getGraphics ();
    }

    dbg.setColor (getBackground ());
    dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);

    dbg.setColor (getForeground());
    paint (dbg);

    g.drawImage (dbImage, 0, 0, this);
}
    }

這是GUI.java:

package com.ion.brickdodge;

import java.awt.BorderLayout;

import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class GUI {

public static void main(String args[]) {
      JFrame frame = new JFrame("test");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setSize(1000, 1000);

      JPanel panel = new JPanel(new BorderLayout());
      frame.add(panel);

      JApplet applet = new Game();
      panel.add(applet, BorderLayout.CENTER);
      applet.init();
      frame.pack();

      frame.setVisible(true);
    }
}

當我運行它時,錯誤GUI.java拋出...

Exception in thread "main" java.lang.NullPointerException
at java.applet.Applet.getCodeBase(Unknown Source)
at com.ion.brickdodge.Game.init(Game.java:30)
at com.ion.brickdodge.GUI.main(GUI.java:22)

任何幫助將非常感激

我將回答這個問題:我認為更好的解決方案是將GUI設置為創建JPanels。 然后,如果您希望將GUI作為applet運行,您只需創建JPanel並將其放在JApplet的contentPane中。 同樣,如果要創建JFrame,請創建JPanel並將其放在JFrame的contentPane中。

其他問題:

  • 考慮將您的圖像作為資源,因為它們可能會保存在jar文件中。
  • 不要直接在頂級窗口(如JApplet)中繪制,也不要使用paint方法。
  • 而是在JPanel的重寫paintComponent(...)方法中繪制。 這將允許您的代碼利用Swing的雙緩沖。
  • 不要在Swing GUI中覆蓋update(...) 這是為AWT完成的,但應該避免Swing。
  • 不要在繪圖方法中讀取任何圖像或執行任何其他cpu密集型操作。
  • 查看Swing繪畫教程,看看應該如何進行這種編碼。 你可以找到入門教程在這里 ,和更先進的文章在這里

注意:如果問題是“為什么會這樣?” 答案是JVM會自動為applet設置applet上下文和存根。 但除非將applet加載到網頁中,否則不會發生這種情況。 如果您自己加載它,則需要實現,實例化和附加這些設施。

一旦獲得applet元素通常提供的詳細信息,這樣做並不是很難。

(OTOH最好的策略是將游戲轉換為面板,將其放入框架中,然后使用JWS啟動它。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM