1

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?

Steven
  • 1,709
  • 3
  • 17
  • 27
  • 1
    Why don't you use Swing? – tckmn Jan 01 '13 at 21:10
  • 1
    Call `this.setLayout` with a non-null argument before adding your button (or anything else). Remove the comp.setLocation and comp.setSize lines; a layout's job is to figure out the proper sizing based on the button's size needs. Exactly which layout class you should use is a separate (and long) discussion; you will need to read the javadoc for each of them, and you'll probably find it tiresome since you dislike GUIs, but there's not all that many and it's an absolute necessity for GUI development. – VGR Jan 01 '13 at 21:12
  • 1
    Here is all of the question that readers of http://stackoverflow.com/questions can see: “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 ...”. Wouldn't you prefer they saw something that started “I have this bit of GUI code” followed by a meaningful extract of your question? – Pascal Cuoq Jan 01 '13 at 21:15

3 Answers3

4

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.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • I can't stand the way Swing looks.. It looks informal, blobby, and just gross.. In my opinion. I'd rather use something that at least resembles what people are used to using. Not Swing.. Unless there is a way to make swing use different, better looking buttons and text boxes, and dropdown lists, and all the other form elements.. – Steven Jan 01 '13 at 21:30
  • 1
    Then use the system look and feel, which will make your Swing app look like a native application. http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/ – JB Nizet Jan 01 '13 at 21:31
  • See [this answer](http://stackoverflow.com/a/5630271/418556) for screenshots of the OS X 'Aqua' PLAF, compared to Ubuntu GTK, Windows & Nimbus. – Andrew Thompson Jan 02 '13 at 02:42
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

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

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:

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

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
paulsm4
  • 114,292
  • 17
  • 138
  • 190