so im making a simple box from java and this is the code so far:
import java.awt.Canvas;
import javax.swing.JFrame;
public class Display {
public static final int WIDTH = 800;
public static final int LENGTH = 600;
public static void main(String[] args) {
Display game = new Display();
JFrame frame = new JFrame();
frame.setResizable(false);
frame.setVisible(true);
frame.setSize(WIDTH, LENGTH);
frame.add(game);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
im getting an error here “frame.add(game);” its says “The method add(Component) in the type Container is not applicable for the arguments (Display)”
How im not sure what im doing wrong, im using javaSE-1.6
You’re trying to add an instance of
Display, which isn’t a Swing component, to yourframe, hence the error. Looking at your imports, you probably meant to add aJPanelwithin yourDisplayclass (if there is one) to theframe.Alternatively, your
Displayclass needs to inherit from something likeJComponent, if you want to add it directly. You shouldn’t mix AWT and Swing components needlessly.