Sign in to follow this  

java gui help

This topic is 3725 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm having trouble with some simple java gui I'm trying to get a button to appear on my window but its not, here's the code.
import java.awt.*;

public class Main
{
	public static void main(String[] args)
	{
		Frame frame = new Frame("My Window");
		frame.setLocation(100, 100);
		frame.setBackground(Color.CYAN);
		frame.setResizable(false);
		frame.setSize(400, 320);
		frame.setVisible(true);

		Button button = new Button("Click Me");
		button.setLocation(50, 100);
		button.setVisible(true);
		frame.add(button);

	}
}


the window appears but not the button.

Share this post


Link to post
Share on other sites
I'm using netbeans all I get is the cyan background, also is it better to make GUI applications with swing or awt? I notice that awt is for web browser applets, or is it still good for applications as well.

Share this post


Link to post
Share on other sites
I got the CYAN too... but I know that my java is tempermental (1.4.2) ... one of my personal rules is to add all the windowy child stuffs before the Frame.setVisible(true) is called. Different jdks may work differently... i.e. Java 1.5 may have dealt with that.

Moving the Button lines above the setVisible made it work as expected...

There's probobly an invalidate and re-layout method... or something like that to call, but I don't know what it is off hand.




P.S. I like using a JFrame as it's got the setDefaultCloseOperation() method

Share this post


Link to post
Share on other sites
Quote:
Original post by namingway
I'm using netbeans all I get is the cyan background, also is it better to make GUI applications with swing or awt? I notice that awt is for web browser applets, or is it still good for applications as well.


AWT has been obsoleted by Swing, so use the J____ counterparts for UI (JFrame, JButton, ...)

Share this post


Link to post
Share on other sites

This topic is 3725 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this