Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Simple swing GUI code....Plz help

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Simple swing GUI code....Plz help

    I am learning java swing programming and have a problem.
    I want to create simple Bouncing ball application using threading and swing..
    So, I created a JFrame and placed 2 buttons, start and stop, and placed a simple ball using fillOval().
    Here is the code...




    package bouncingball;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    class Ball extends JPanel
    {
    @Override
    public void paintComponent(Graphics g)
    {
    g.setColor(Color.red);
    g.fillOval(20, 20, 100, 100);
    }
    }

    public class BouncingBall {

    JFrame f;
    JButton start, stop;

    BouncingBall()
    {
    f = new JFrame("BouncingBall");
    f.setLayout(null);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(700, 700);
    f.getContentPane().add(new Ball());
    start = new JButton("START");
    start.setBounds(100, 450, 150, 20);
    f.getContentPane().add(start);
    stop = new JButton("STOP");
    stop.setBounds(100, 500, 150, 20);
    f.getContentPane().add(stop);
    f.setVisible(true);

    }

    public static void main(String[] args) {
    // TODO code application logic here
    BouncingBall b = new BouncingBall();
    }
    }


    I set the layout to null. On running this code, buttons are visible, but ball is not.
    I know that in layout, we can add only one thing to each region. But, since setLayout(null), what is wrong with it?


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Simple swing GUI code....Plz help

    The natural size of a JPanel is like 0,0 or something. So you just cannot see the Ball JPanel.

    The easiest way to do this would be to make the Ball JPanel your content pane. To do that, change this:
    f.getContentPane().add(new Ball());
    to this:
    f.setContentPane(new Ball());

    I think that should do it.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. The Following User Says Thank You to aussiemcgr For This Useful Post:

    Arati2512 (April 13th, 2012)

  4. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Simple swing GUI code....Plz help

    You are using a null layout - all components added must be given a defined size, which the Ball panel has not been given. That being said, I recommend against using a null layout - Layouts make your life so much easier in the long run, and make your application much more portable across computers and systems. They may seem daunting at first, but a little time playing with them, nesting them, etc...can help a lot in the long run.

Similar Threads

  1. Help with understanding simple GUI Code
    By jordan123 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 21st, 2012, 03:19 PM
  2. Simple code, cant get it working.
    By Malmen in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 8th, 2011, 03:28 PM
  3. Need help with simple code
    By suxen in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 21st, 2011, 08:10 PM
  4. simple game with swing and awt problem
    By Pulse_Irl in forum AWT / Java Swing
    Replies: 2
    Last Post: October 12th, 2010, 02:04 PM
  5. not so simple, simple swing question box
    By wolfgar in forum AWT / Java Swing
    Replies: 2
    Last Post: November 20th, 2009, 03:47 AM

Tags for this Thread