简体   繁体   中英

Working with frames and Java AWT

I am currently making a program with the AWT GUI and I'm running into a problem. I basically want an image in 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:

屏幕截图

Is there anyway to fix this so that the image and the buttons are in the same window?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM