使用框架和 Java AWT
我目前正在使用 AWT GUI 编写一个程序,但遇到了问题。 我基本上想要屏幕左上角有一个图像,图像右侧有一列按钮。 但这并不是正在发生的事情。 当我运行小程序时,我单击一个弹出窗口,显示“启动程序”,然后我想要的图片位于小程序窗口本身中,而按钮列本身位于另一个窗口中。 它看起来像这样:
是否有办法解决此问题,以便图像和按钮位于同一窗口中?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的。 您正在创建一个框架,但您的图形不在框架内。 没有代码就无法透露太多信息,但是 AWT 教程 java.sun.com 在这方面还不错。
好的,再讲一点(我已经很长时间没有使用 AWT 了。)
这是您遇到的几个问题。 框架是一种窗口——它希望成为一个具有自己的关闭按钮等的单独窗口。
当您创建图形时,您必须告诉它是其父级的组件; 你以某种方式将其作为 Applet 的父级。 因此,您有一段代码
在 Applet 的上下文中看起来像
this
。您有一个框架,并且正在向其中添加按钮。
您需要通过某种方法将添加 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
in the context of the Applet as
this
.You have a Frame, and you're adding your buttons to that.
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