Java help

Discussion in 'Software' started by Computer Acolyte, Apr 4, 2004.

  1. Computer Acolyte

    Computer Acolyte Private First Class

    Ok this is a very basic problem but I can't figure out what's wrong.
    I'm learning the awt (Abstract Windowing Toolkit) package (I know it's old but that's what the Java book has) :p
    Anyway, I wrote a small program just to see how it all fits together, it's supposed to give me a window titled "Animals" with 3 colored buttons.
    Here's the code:

    import java.awt.*;
    public class TestAwt {
    public static void main(String[] args) {
    Frame frame=new Frame("Animals");
    Panel panel=new Panel();
    Button b1=new Button("Dog");
    Button b2=new Button("Cat");
    Button b3=new Button("Hamster");

    b1.setForeground(Color.white);
    b1.setBackground(Color.blue);
    b2.setForeground(Color.white);
    b2.setBackground(Color.blue);
    b3.setForeground(Color.white);
    b3.setBackground(Color.blue);

    panel.setBackground(Color.blue);
    panel.add(b1);
    panel.add(b2);
    panel.add(b3);

    frame.add(panel);
    frame.setVisible(true);
    frame.setSize(200,200);

    }
    }

    For some reason all it gives me is the frame (overall window) but no components inside (no buttons, no background [panel]), which is very strange, considering that this program (a simpler version of the previous one) works:

    import java.awt.*;
    public class Wtf {
    public static void main(String[] args) {
    Frame frame=new Frame("Alice in wonder land");
    Panel panel=new Panel();
    Button b=new Button("Press");
    panel.add(b);
    frame.add(panel);
    frame.setSize(300,300);
    frame.setVisible(true);
    panel.setBackground(Color.black);

    }
    }

    What am I missing here?
     
  2. Computer Acolyte

    Computer Acolyte Private First Class

    Ok I figured it out, I can't set visible before I set size.
    I've another question though, these are just variebles of the objects, why does the order matter? Doesn't the actual display of the objects occure AFTER this code is executed?
     

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