浮动按钮是AWT? [英] Floating button is AWT?
问题描述
所以,我一直在Java中使用图形用户界面,因为就个人而言,我不能忍受的GUI避免。但是,我已经开始要求我使用图形用户界面,以及令人惊讶,我有问题的项目..我有此位的code ..
So, I've always avoided using GUIs in java because, personally, I can't stand GUIs. But, I've started a project which requires me to use GUIs, and, unsurprisingly, I'm having problems.. I have this bit of code..
public class DefaultWindow extends Window
{
private DefaultWindow(Frame owner)
{
super(owner);
contained = owner;
}
public DefaultWindow()
{
this(new Frame(""));
contained.setBackground(Color.black);
contained.setLocation(0, 0);
contained.setSize(1280,720);
Button comp = new Button("Hello");
comp.setLocation(0, 0);
comp.setSize(10, 10);
add(comp);
pack();
contained.setVisible(true);
}
}
..它创建一个1280x720的窗口,黑色背景(这是很好),它还会在我的屏幕顶部左上角的浮动按钮..我该如何使按钮是在窗口?
.. and it creates a 1280x720 window with a black background (which is good) and it also creates a floating button in the top left-hand corner of my screen.. How do I make the button be in the window?
推荐答案
您正在创建窗口,它在其构造函数中,创建一个框架(这本身就是一个窗口)的子类的实例。你这显示空框,并添加按钮,你创建的窗口。那么,到底你有两个窗口。
You're creating an instance of a subclass of Window which, in its constructor, create a Frame (which is itself a Window). You're showing this empty frame, and add the button to the window you're creating. So in the end you have two windows.
我觉得你真正想要的是创造一个且只有一个框架。您的类不应该扩展窗口,而这一切不应该在构造函数来完成。此外,AWT是一种过时。你应该用摇摆。 Oracle有左右摇摆很大的教程,这也BTW介绍了如何使用布局管理器(你应该这样做)。 阅读本教程。
I think that what you really want is to create one and only one Frame. Your class shouldn't extend Window, and all this shouldn't be done in a constructor. Moreover, AWT is kind of obsolete. You should be using Swing. Oracle has a great tutorial about Swing, which BTW also explains how to use layout managers (which you should do). Read this tutorial.
这篇关于浮动按钮是AWT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!