使用框架和 Java AWT

发布于 2024-07-15 18:55:04 字数 264 浏览 7 评论 0原文

我目前正在使用 AWT GUI 编写一个程序,但遇到了问题。 我基本上想要屏幕左上角有一个图像,图像右侧有一列按钮。 但这并不是正在发生的事情。 当我运行小程序时,我单击一个弹出窗口,显示“启动程序”,然后我想要的图片位于小程序窗口本身中,而按钮列本身位于另一个窗口中。 它看起来像这样:

screenshot

是否有办法解决此问题,以便图像和按钮位于同一窗口中?

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:

screenshot

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

祁梦 2024-07-22 18:55:04

是的。 您正在创建一个框架,但您的图形不在框架内。 没有代码就无法透露太多信息,但是 AWT 教程 java.sun.com 在这方面还不错。


好的,再讲一点(我已经很长时间没有使用 AWT 了。)

这是您遇到的几个问题。 框架是一种窗口——它希望成为一个具有自己的关闭按钮等的单独窗口。

当您创建图形时,您必须告诉它是其父级的组件; 你以某种方式将其作为 Applet 的父级。 因此,您有一段代码

add(myComponent);

在 Applet 的上下文中看起来像 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);
}

您有一个框架,并且正在向其中添加按钮。

public class MyFrame extends Frame {

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

}

您需要通过某种方法将添加 Canvas 的代码移动到 Frame 类中。

(警告:这不是完整的 Java 代码,我不记得正确方法的名称。至少可能是 Applet 中的 init() 方法。

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文