Jump to content
  • Advertisement
Sign in to follow this  
namingway

java gui help

This topic is 4008 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
Advertisement
I tried your exact code, and I get a button. It fills the entire frame, but it's definitely there.

What development environment (if any) and Java runtime are you using?

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!