Floating button is AWT?

早过忘川 提交于 2019-12-11 05:04:22

问题


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);
    }
}

.. 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?


回答1:


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.

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.




回答2:


Personally, i would use Swing based components over AWT (personally), apart from anything else, there are more components and support.

contained is an invalid reference, you don't need it. You create two windows and only show the one without the button. Drop the reference to the Frame and rely on the window instead

public DefaultWindow()
{
    setBackground(Color.black);
    setLocation(0, 0);
    setSize(1280,720);
    Button comp = new Button("Hello");
    setLocation(0, 0);
    comp.setSize(10, 10);
    add(comp);
    pack();
    setVisible(true);
}

I would avoid setting windows to arbitrary sizes, not all screens are the same.

You will also run foul of the layout manager, meaning that the settings you supply to the button may be overridden.

I would take the time to read through Creating a GUI With JFC/Swing




回答3:


Q: So, I've always avoided using GUIs in java because, personally, I can't stand GUIs.

A: Hey: I thought I was alone on the planet :) My motto has always been "GUIs make simple tasks easier ... and difficult tasks utterly impossible" ;)

As far as your question: the answer is:

1) Swing is good for "thick clients" (i.e. Java desktop applications)

2) JSP is good for "web applications) (i.e. client/server web apps)

3) Don't even think about using AWT for your entire GUI. It was deprecated very early in Java history (Java 1.2, specifically).

Here are some good tutorials:

  • http://www.java2s.com/Code/Java/Swing-JFC/HelloWorldSwing.htm

  • http://www.ibm.com/developerworks/java/tutorials/j-tomcat/

PS: Despite what I said about GUIs ... and IDE can be very helpful.

Personally, I use Eclipse. For several reasons:

  • Eclipse (like most IDEs) can be used for cmd-line, Swing, JSP and Java EE apps.

  • Eclipse (unlike Netbeans or IntelliJ) is equally useful for Android apps

  • Eclipse has an extremely broad ecosystem of 3rd party plugins (from companies as diverse as IBM and Google).

IMHO...



来源:https://stackoverflow.com/questions/14114205/floating-button-is-awt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!