Java array and layout trouble

Discussion in 'Software' started by Pure-D, Nov 21, 2005.

  1. Pure-D

    Pure-D Private E-2

    Hi, I'm just getting into java at the moment I've had a little experience in C and C++, but not lots. At the moment I'm trying to create a simple calculator, and I hit on the idea of using an array to sort out the number buttons and there values, but I've hit a bit of a wall at the moment. Here's the code so far...

    Code:
    import java.awt.*; 
    import java.applet.Applet;
    public class Calctest4 extends Applet{ 
     Button[] Num;
    public Calctest4(){ 
    }
    public void init(){ 
    setLayout(new GridLayout(4, 3, 4, 5));
     Num = new Button[10]; 
     for (int i = 0; i<10; i++){ 
     Num[i] = new Button(Integer.toString(i)); 
     add(Num[i]);
         } 
    }
    public boolean action (Event a, Object b){ 
     if (a.target instanceof Button) 
     showStatus(b.toString()); 
     return true; 
    } 
    }
    
    
    I used an array to avoid this approach..

    Code:
    
                 Button button1 = new Button("1");
    	Button button2 = new Button("2");
    	Button button3 = new Button("3");
    	Button button4 = new Button("4");
    	Button button5 = new Button("5");
    	Button button6 = new Button("6");
    	Button button7 = new Button("7");
    	Button button8 = new Button("8");
    	Button button9 = new Button("9");
    	Button button0 = new Button("0");
    
    but because I'm using an array I seem to have little to no control over the placement or size of the buttons. I get errors when ever i try things like this...

    Code:
    button1.setBounds(42,65,60,34);
    //or
    add(button1);
    //or
    add(button[]);
    
    So what I guess I'm trying to say is how do i pull out the name or such of a button from the array i created?
     
  2. BrokenArrows

    BrokenArrows Sergeant

    When setting the bounds like that you have to set the layout manager to null

    content.setLayout(null);

    content is the name of your jPanel

    I have never used applets so im not sure if its the same but you can try it anyway.
     
  3. Pure-D

    Pure-D Private E-2

    hi again, thanks for the advice.

    just a few more questions, sorry :p

    with the 0-9 buttons in the array will i have to declare values for them as well? so when the "1" button is pressed its value is stored ready for the next press.

    how would i go about getting the numbers to appear in a textfield. for example when 1 and 2 are pressed, 12 will appear there. i've been racking my brain trying to do this and im failing terribly.

    i've already made a calculator with just a textfield and plus, minus and equals, the user would have to type the numbers into the textfield and click the buttons and the answer would appear in the text area. But from what i have learned from this, I cant apply it to the new calculator.
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds