Frame Border

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SammyB
    Recognized Expert Contributor
    • Mar 2007
    • 807

    Frame Border

    I am creating a Frame with a checkerboard. Currently the checkerboard and the two other panels fill the entire form, but I would like to have a 20 pixel border of blank space around the three frames, but I can only change the spacing between frames. How do I do this?

    My class extends Frame and the important constructor lines are below (I've left out the TextArea constructors & the panel.add's). I've attached a sketch of what I would like it to look like.
    [code=java]
    Panel pnlCheckerboard = new Panel();
    TextArea txtSquare[] = new TextArea[16];
    //
    Panel pnlInput = new Panel();
    TextField txtStart = new TextField(8);
    TextField txtStop = new TextField(8);
    TextField txtStep = new TextField(8);
    Label lblStart = new Label("Start");
    Label lblStop = new Label("Stop");
    Label lblStep = new Label("Skip");
    //
    Panel pnlButtons = new Panel();
    Button btnGo = new Button("Go");
    Button btnClear = new Button("Clear") ;
    //
    this.setLayout( new BorderLayout(10 ,10));
    pnlCheckerboard .setLayout(new GridLayout(4,4, 10,10));
    pnlInput.setLay out(new GridLayout(2,3, 10,10));
    pnlButtons.setL ayout(new FlowLayout(Flow Layout.CENTER,5 0,1));
    // Bunches of add's left out
    this.add(pnlChe ckerboard, NORTH);
    this.add(pnlInp ut, CENTER);
    this.add(pnlBut tons, SOUTH);
    [/code]
    Attached Files
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Add all those components to another JPanel. Have a look at the BorderFactory.
    Make it create an EmptyBorder 20 pixels wide on all sides. Set that border
    to the JPanel. Finally add that JPanel to your Frame and voila.

    kind regards,

    Jos

    ps. Better use a JFrame (Swing) instead of a Frame (AWT)

    Comment

    • SammyB
      Recognized Expert Contributor
      • Mar 2007
      • 807

      #3
      Originally posted by JosAH
      Add all those components to another JPanel. Have a look at the BorderFactory.
      Make it create an EmptyBorder 20 pixels wide on all sides. Set that border
      to the JPanel. Finally add that JPanel to your Frame and voila.

      kind regards,

      Jos

      ps. Better use a JFrame (Swing) instead of a Frame (AWT)
      Yep, that worked. Couldn't figured out how to use a JFrame (comes in later chapters), so I just shoved a JPanel into the Frame. It was not pretty, but I tweaked the TextField & TextArea sizes and the form size until it looked great. Thanks!

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by SammyB
        Yep, that worked. Couldn't figured out how to use a JFrame (comes in later chapters), so I just shoved a JPanel into the Frame. It was not pretty, but I tweaked the TextField & TextArea sizes and the form size until it looked great. Thanks!
        There's OGR (tm) (OGR == One Golden Rule) that says: never mix AWT and Swing
        components; the two don't like each other. Instead of a JPanel and a Border
        you could have a look at the Canvas and Insets classes; they can
        do more or less the same and AWT understands them.

        A bit of explanation: AWT uses the system available visual components for the
        actual drawing, so e.g. an AWT Button has a 'ButtonPeer' that calls the native
        windowing system to do the drawing. Such a component is called a 'heavy weight'
        component. Swing takes the other route and does all the drawing itself, entirely
        written in Java. (Swing was slow in the early days). Swing doesn't know about
        those heavy weight components so if you place two or more of them near each
        other or even on top of them two drawing systems are invoked and Swing doesn't
        know that and AWT doesn't know that the native system doesn't draw certain parts
        of the screen; the net result is a mess.

        kind regards,

        Jos

        Comment

        Working...