Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jun 2007
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Why the button 9 is not displaying properly ?

    Code:
    import java.awt.*;
    import java.awt.event.*;
     
    public class Standard_Calculator 
    {
     static Frame W;
     static Button num;
     Button operate;
     Button memory;
     
     public static void main(String args[])
     {
      int i = 1, x = 40, y = 50, x2 = 4;
      W = new Frame("Calculator 1.0");
      W.setSize(300, 300);
      for (; i<=9; i++, x+=10)
      { 
       num = new Button(Integer.toString(i));
       num.setActionCommand(Integer.toString(i));
       if (i==x2)
       {
       	x=40;
       	x2+=3;
       	y+=10;
       }
       num.setLocation(x, y);
       num.setSize(10, 10); 
       W.add(num);	
      }
      W.show();	
     }
    }
    i want the numbers to display properly...why no 9 is so big...why is not attached next to 8 ? what do i do ?

  • #2
    New Coder
    Join Date
    Jun 2005
    Posts
    32
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Not sure why it's doing that, but there's a pretty easy fix. Just create a JPanel with a grid layout of 3 x 3 and add the buttons to that. Then after the loop, add that panel to the frame and your buttons will look like you want them to.


  •  

    Posting Permissions

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