Converting JApplet into JPanel for use in another class

259 Views Asked by At

I'm pretty new to programming here.

I've got a complicated JApplet that runs happily by itself, but for purpose of a project, I'd like it to open in a new window when a button is clicked in another JApplet. One way I've heard of doing this is to make the JApplet into an extension of JPanel, then add the JPanel to the other app as you normally would. (In this case, sticking it inside a JFrame).

However, when I try this, I get this error on compiling:

.\Puzzle.java:241: error: cannot find symbol
    wall = getImage(getCodeBase(),"metalcrate.jpg");
                    ^
  symbol:   method getCodeBase()
  location: class Puzzle
.\Puzzle.java:267: error: cannot find symbol
    goal = getImage(getCodeBase(), "redxicon.png");
                    ^
  symbol:   method getCodeBase()
  location: class Puzzle
2 errors

And this on running:

java.lang.IllegalArgumentException: adding a window to a container
    at java.awt.Container.checkNotAWindow(Container.java:488)
    at java.awt.Container.addImpl(Container.java:1089)
    at java.awt.Container.add(Container.java:1003)
    at javax.swing.JApplet.addImpl(JApplet.java:315)
    at java.awt.Container.add(Container.java:415)
    at Test.init(Test.java:27)
    at sun.applet.AppletPanel.run(AppletPanel.java:434)
    at java.lang.Thread.run(Thread.java:745)

This is the gist of how the other applet would open this one as a JPanel/JFrame. The code for the applet I'm trying to convert is pretty long, so I wasn't sure if I should post it in its entirety here. However, it uses ActionEvents and ItemEvents, drawing Images from files, and a null layout (it's a rough version of a Rushhour game), and I'm wondering if this may be part of the problem.

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Test extends JApplet implements ActionListener
{
JButton play;
JFrame newWindow;
Puzzle testpanel;

public void init()
{
setLayout(new FlowLayout());

play = new JButton("Play!");
play.addActionListener(this);
add(play);

testpanel = new Puzzle();

newWindow = new JFrame ("test frame");
newWindow.setBounds(50, 50, 700,600 );
newWindow.add(testpanel);
newWindow.pack();
newWindow.setVisible(true);

add(newWindow);
}

public void actionPerformed (ActionEvent ae)
    {
    Object src = ae.getSource();
    if (src == play)
        newWindow.setVisible(false);
    }

}

What am I doing wrong here? Is there an easier way to do this for the same effect?

0

There are 0 best solutions below