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 2 of 2

Thread: getBounds problem

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default getBounds problem

    Hello,

    I am doing exercises in my java book and right now I am stuck on an example. This example is supposed to show me how to use the setBounds method of the JFrame class to move a frame window to a given rectangle. I tried playing around with the code and can't seem to figure out what I am doing wrong. I have looked at the API as well which made me try to set parameters in setBounds, but I am still not seeing anything appear on the frame when I run.

    Here are the two different codes I have tried. Can anyone direct me what to look at or give me a hint without giving me the answer. I would like to figure this out on my own, but need direction. Thank you.

    import java.awt.Rectangle;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
     
    public class TranslateDemo {
    	public static void main(String[] args)
    	{
    		// Constructs a frame and show it
    		JFrame frame = new JFrame();
    		frame.setSize(300, 300);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
     
    		Rectangle box = new Rectangle (5, 5, 5, 5);
    		box.setBounds(box);
    		box.getBounds();
     
    		JOptionPane.showMessageDialog(frame, "Click OK to continue");
     
    		box.translate(10, 10);
    		box.setBounds(box);
    		box.getBounds();
    	}
    }

    import java.awt.Rectangle;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
     
    public class TranslateDemo {
    	public static void main(String[] args)
    	{
    		// Constructs a frame and show it
    		JFrame frame = new JFrame();
    		frame.setSize(300, 300);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
     
    		Rectangle box = new Rectangle (5, 5, 5, 5);
    		box.setBounds(10, 10, 10, 10);
    		box.getBounds();
     
    		JOptionPane.showMessageDialog(frame, "Click OK to continue");
     
    		box.translate(10, 10);
    		box.setBounds(20, 20, 20, 20);
    		box.getBounds();
    	}
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,781
    My Mood
    Mellow
    Thanks
    236
    Thanked 717 Times in 703 Posts

    Default Re: getBounds problem

    A change of 10 pixels is very small and may not be perceptible, but I don't think what you're expecting to happen is what is supposed to happen. Try this main() method to get an idea of what setBounds() does:
    	public static void main(String[] args)
    	{
    		// Constructs a frame and show it
    		JFrame frame = new JFrame();
    		frame.setSize(300, 300);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setBounds( new Rectangle( 400, 400, 200, 200 ) );
    		frame.setVisible(true);
     
    		JOptionPane.showMessageDialog(frame, "Click OK to continue");
     
    		frame.setBounds( new Rectangle( 500, 500, 400, 400 ) );
    	}

Similar Threads

  1. Problem with Project Euler problem 18
    By sara_magdy in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 19th, 2013, 12:46 PM
  2. getBounds() method
    By hwoarang69 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 28th, 2012, 05:06 PM
  3. Replies: 3
    Last Post: January 5th, 2012, 12:44 AM
  4. [SOLVED] [Problem] imports javax.swing problem
    By Brollie in forum AWT / Java Swing
    Replies: 8
    Last Post: July 5th, 2009, 07:59 AM
  5. Java program for 2-D Array Maze
    By Peetah05 in forum Collections and Generics
    Replies: 11
    Last Post: May 8th, 2009, 04:30 AM