tags:

views:

222

answers:

1

Hello, I am currently making a program with the AWT gui and I'm running into a problem. I basically want an image the top left hand corner of the screen, and a column of buttons on the right of the image. This isn't what's happening though.. When I run the applet, I click a popup saying "Start Program" and then the picture I want is in the applet window itself and the column of buttons is in another window by itself. This is what it looks like: http://i42.tinypic.com/13zxq8p.jpg.

Any way to fix this so that the image and the buttons are in the same window?

+1  A: 

Yeah. You're creating a frame but your graphic isn't inside the frame. Can't tell much without the code, but the AWT Tutorial at java.sun.com isn't bad on this stuff.


Okay, a little more (I haven't used AWT in a long time.)

Here's the couple of issues you have. A Frame is a kind of Window -- it wants to be a separate window with its own close button and so forth.

When you create your graphic, you have to tell it was component its parent is; you're somehow parenting it to the Applet. So you have some piece of code that looks like

add(myComponent);

in the context of the Applet as this.

public class myApplet extends Applet {
   // lots of stuff here creating your canvas, putting the image in it
   // and so forth.  There's an example, see fn 1.
   // When you're done, you have a component, call it myImage.

   add(myImage);
}

You have a Frame, and you're adding your buttons to that.

public class MyFrame extends Frame {

    add(new Button(...));
    add(new Button(...));

}

You need to move the code that adds your Canvas into the Frame class in some method.

(WARNING: this is not complete Java code, I don't recall the names of the right methods offhand. Probably the init() method in the Applet, at least.

fn1. http://java.sun.com/developer/onlineTraining/awt/contents.html#simpleexample

Charlie Martin
Yeah, I've followed that a little bit. It's my first time using AWT :SAnyway, thank you and if anyone has any more suggestions, please post. Thanks again!
Tyler
I'm not sure if I'm even adding the image correctly..This is what my main class currently looks like: http://friendpaste.com/61E13yV4CSR9JiFZhrDeSeStartFrame is the frame that pops up at the beginning of the game and WindowFrame is the frame with all of the buttons on it.
Tyler
Yup, there you are. In your Applet ctor, you're creating a MediaTracker on this, then adding the graphic there. That means you're adding the graphic to the Applet. Here's a hint: start with a clean directory, and build a little piece on at a time.
Charlie Martin
Like, make your basic applet, put a textbox in it, make sure you an see that. Make your Frames, and write a main method so you can run it standalone. Try those.
Charlie Martin
Alright, I'm probably going to create an empty project and mess around with it until I understand it a little better. Thanks.
Tyler
I moved the paint and update (for paint) methods into the BoardClass, which draws a lot of the other stuff. Now, the main applet is blank, which isn't a bad thing.. But the only things showing up on the WindowFrame are the buttons still. They take up all of the windowFrame.
Tyler
That's probably because of the layout manager. They're a PITA, but at least in this code, you're putting the MainWindow panel in a one-cell GridLayout, and putting your buttons in a BorderLayout. You need to make sure you're inserting your other stuff as needed by the layout manager.
Charlie Martin