CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Jan 2009
    Posts
    18

    why do the buttons not show?

    I tried to create a simple GUI,but but my buttons and label dont show.I tried rearranging my lines of code in the launchFrame method but i couldnt get the correct result.I want to create a GUI with three buttons aligned ,a panel on top and a label under the buttons.Here are the lines of code i used



    import java.awt.*;

    public class FlowExample
    {
    private Panel p;
    private Button b1;
    private Button b2;
    private Button b3;
    private Label l;
    private Frame f;


    public FlowExample()
    {
    p = new Panel();
    b1 = new Button(" Blue ");
    b2 = new Button(" Red ");
    b3 = new Button("Yellow");
    l = new Label();
    f = new Frame("Choose Color");
    }

    public void launchFrame()
    {
    p.setSize(500,50);
    p.setBackground(Color.green);
    f.add(p, BorderLayout.NORTH);

    f.add(b1, BorderLayout.WEST);

    f.add(b2, BorderLayout.CENTER);

    f.add(b3, BorderLayout.EAST);

    f.add(l, BorderLayout.SOUTH);

    f.setSize(500,500);
    f.setLayout(null);
    f.setVisible(true);
    }

    public static void main(String args[])
    {
    FlowExample GUI = new FlowExample();
    GUI.launchFrame();
    }
    }

  2. #2
    Join Date
    Feb 2009
    Location
    New York
    Posts
    8

    Re: why do the buttons not show?

    Use javax.swing.JFrame and other JComponets. AWT classes are a bit outdated anyway. Should you use those, top container in JFrame is the contentPane, set it or get it using setContentPane() or getContentPane().

  3. #3
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: why do the buttons not show?

    Quote Originally Posted by VolatileObject View Post
    Use javax.swing.JFrame and other JComponets. AWT classes are a bit outdated anyway. Should you use those, top container in JFrame is the contentPane, set it or get it using setContentPane() or getContentPane().
    If you use JFrame, you don't need to add components to the content pane, you can add them directly to the JFrame.

    As far as the OP goes, it doesn't seem sensible to create a panel, add it to the frame, but then add the other components to the frame instead of the panel. if you're going to do this, why have the panel at all?

    Also, it seems odd to add the components to the frame using BorderLayout constraints, but then to set the frame layout to null - why do that?

    The purpose of computing is insight, not numbers...
    R. Hamming
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  4. #4
    Join Date
    Feb 2009
    Posts
    32

    Re: why do the buttons not show?

    It is because you set the borderlayout of the Frame to null. That effective erases all the components within the layout. Delete that one line and you see the buttons.
    Code:
    f.setLayout(null);
    Delete it. It's magic.

    I agree with the others... use the swing package... JFrame, JButton, JLabel, JPanel, etc... they're prettier .

  5. #5
    Join Date
    Jan 2009
    Posts
    18

    Re: why do the buttons not show?

    Thanks you guys,this is the best java site ever......i'll be back with more questions,but please remember not everyone is as good as you guys,some times its a bit difficult to understand some of the stuff you say,like i didnt even know there was a Swing package.........but thanks,i'll be back.

  6. #6
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: why do the buttons not show?

    i'll be back with more questions,but please remember not everyone is as good as you guys
    If you knew as much as we do then we wouldn't be able to help you

    ,some times its a bit difficult to understand some of the stuff you say
    Don't worry, if we wrote every reply as if you knew nothing then we'd spend hours typing replies and you might get offended. If you don't understand a reply just ask for clarification, we are always happy to give more detail. Unless of course you could have easily found out the info for yourself by a simple google.

  7. #7
    Join Date
    Jan 2009
    Posts
    18

    Re: why do the buttons not show?

    Thats why i think you guys are the best

  8. #8
    Join Date
    Jan 2009
    Posts
    18

    Re: why do the buttons not show?

    please how do i divide the frame into 3 vertical sections(columns),do i still use panels to do that?
    and afterward, can i still add components to the different columns,i also know panels are used to create rows ,its just the columns i'm unsure of

  9. #9
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: why do the buttons not show?

    Yes you can use panels to represent 3 vertical sections. You need to choose an appropriate LayoutManager to get the layout effect you are after ie do all columns need to be the same width or different widths or should they size themselves according to the components in each of the panels

  10. #10
    Join Date
    Jan 2009
    Posts
    18

    Re: why do the buttons not show?

    they need to size them selves according to the components in each panel,in the first vertiacal panel i'll have two vertically arranged buttons ,in the second textfields and a textarea,inthe third two button like components with the symbol">> and <<" on them.the last panel is horizontal,that one i can manage,but how do i set the buttons to be in the middle of the panel?

  11. #11
    Join Date
    Jan 2009
    Posts
    18

    Re: why do the buttons not show?

    wat if i want the panels to be different sizes but the components to be fixed?

  12. #12
    Join Date
    Feb 2009
    Posts
    32

    Re: why do the buttons not show?

    Quote Originally Posted by seken1 View Post
    they need to size them selves according to the components in each panel,in the first vertiacal panel i'll have two vertically arranged buttons ,in the second textfields and a textarea,inthe third two button like components with the symbol">> and <<" on them.the last panel is horizontal,that one i can manage,but how do i set the buttons to be in the middle of the panel?
    Use the borderlayout manager. The first vertical panel would go in the west. The second would go in the center. The third would go in the east. The last horizontal panel would go in the south (or north).
    Last edited by Nim; February 21st, 2009 at 06:33 PM.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured