Results 1 to 13 of 13
  1. #1
    Join Date
    Sep 2010
    Posts
    6
    Rep Power
    0

    Unhappy New to Java - Need Help

    Hi Experts,

    I am new to java,and I am trying to display the button on frame. Please find the code below


    import java.awt.Button;
    import java.awt.Frame;

    public class ButtonDiaplay extends Frame{

    public ButtonDiaplay()
    {
    setBounds(50,90,250,350);
    setTitle("Bouncing Rect");
    setResizable(true);
    // setLayout(null);

    Button create = new Button("Create");
    setBounds(70, 100, 50, 10);
    this.add(create);
    setVisible(true);
    } //ButtonDiaplay
    public static void main(String[] args){
    ButtonDiaplay display = new ButtonDiaplay();
    }
    }// end ButtonDisplay

    On executing the code, the button is overlapping on the frame size.Can anyone let me know why this is happening?

    I dono want to use Swing and Applet. Through normal AWT only I want to display my button with any given size

    Also, if I set the layout as setLayout(null) then I am not able to see my button. Could anyone please explain me why on setting the layout I am not able see the button.

    If I make setResizable(false),then I am seeing only the pane, I have attached screen shot in the attachment.

    Waiting for the quick reply.
    Thanks in advance.
    Attached Thumbnails Attached Thumbnails New to Java - Need Help-button1.jpg  

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,323
    Rep Power
    25

    Default

    When you use setBounds, you are responsible for where components are positioned.
    Did you check that the bounds don't overlap?

  3. #3
    Join Date
    Sep 2010
    Posts
    6
    Rep Power
    0

    Default

    Hi Norms,
    thanks for yur reply. How do we check that its overlapping or not.
    Even I change the value of setBounds(50,90,250,350) as setBounds(200,200,250,350) , my button is getting over lapped.

    Could you please help me out? I had other query also which I had posted in my previous thread.
    Response will be appreciable.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,323
    Rep Power
    25

    Default

    I just noticed that you call setBounds two times for the same component.

    Have you posted your current version?

    Please put your code in code tags. Info here: Java Forums - BB Code List

  5. #5
    Join Date
    Sep 2010
    Posts
    6
    Rep Power
    0

    Default

    Thanks once again. Please find the code. Also I added mouselistner for closing the form but its not working

    Java Code:
    public class ButtonDiaplay extends Frame{
    	Frame frame = new Frame("Zahid");
    	Label lab = new Label("safia");
    	Button but = new Button("Hi");
    	
    	public ButtonDiaplay()
    		{
    		frame.setVisible(true);
    		frame.setSize(300, 300);
    		
    		frame.add(lab);
    		but.setBounds(20,20,100,30); 
    		frame.add(but);
    		addWindowListener(new WindowAdapter(){
    			public void windowClosing(WindowEvent e){
    			e.getWindow().dispose();
    			System.exit(0);
    			}
    		      });
                             }
    public static void main(String[] args){
    		ButtonDiaplay display = new ButtonDiaplay();
    	}
    [QUOTE][/QUOTE]}
    Reply will be appreciable

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,323
    Rep Power
    25

    Default

    Now you have two Frames?
    ButtonDiaplay extends Frame
    Frame frame = new Frame("Zahid");

    And the window listener is connected to the hidden one.

    Can you describe what your problem is now?

  7. #7
    Join Date
    Sep 2010
    Posts
    6
    Rep Power
    0

    Default

    same button is overlapping the frame. I can see the button size eqaul to the frame size. Also you said that window listner is connected to the hidden one, that means if I inherit my base class and also I instantiate the object of the same parent class then in that case my inherit class will lose its property. Is my understanding is correct? Please correct me if I am wrong.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,323
    Rep Power
    25

    Default

    same button is overlapping the frame
    You set the position of the button. If it is too high, move it down.

    window listner is connected to the hidden one
    There are two separate, unrelated windows. There relationship has nothing to do with inherit or parent class.
    The window closing box [X] for the hidden window will never be visible to be pressed.
    Does the code work for you?

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,323
    Rep Power
    25

    Default

    @cliffh Please start your own thread. Don't tag onto someone else's. Your stuff will be ignored and lost here.

  10. #10
    Join Date
    Sep 2010
    Posts
    6
    Rep Power
    0

    Default

    Nope its not working. Could you please provide me the working code with small size button on the frame.
    That means that if I am adding the components to the frame then that will be visible irrespective of local variable or inherit. Please correct me if I am wrong

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,323
    Rep Power
    25

    Default

    the frame then that will be visible irrespective of local variable or inherit
    The frame object that you call setVisible(true) for will be visible.

    working code with small size button
    frame.setLayout(null); // You are doing the layout with setBounds
    but.setBounds(20,50,100,30); // y has been changed to move the button down


    and move the call to setVisible(true) to the last thing done in the constructor

  12. #12
    Join Date
    Sep 2010
    Posts
    6
    Rep Power
    0

    Default

    thanks a lot Norm for your help. Now its working.That means that for each and every compone I need to do setBound explictly.

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,323
    Rep Power
    25

    Default

    for each and every component I need to do setBound explictly
    Yes. You should learn how to use layout managers. Setting bounds explicitly will make maintenance of your code more difficult and inflexible.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •