Results 1 to 7 of 7

Thread: Really really easy Swing help [resolved]

  1. #1

    Thread Starter
    Member filburt1's Avatar
    Join Date
    Aug 1999
    Posts
    6,935

    Really really easy Swing help [resolved]

    I'm learning Swing at my mentorship and am used to VB's way of doing GUI stuff, so naturally Swing is hell to me. How can I center a JFrame in the screen, like VB's vbCenterScreen StartupPosition property? I have the VB code but I have no idea how to do it in Java:

    VB Code:
    1. Me.Move Screen.Width \ 2 - Me.Width \ 2, Screen.Height \ 2 - Me.Height \ 2
    Last edited by filburt1; Sep 27th, 2001 at 03:02 PM.

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    I have that code somwhere in one of my programs. Ill have to look for it. Hold on.

  3. #3
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Ok sorry i had to look through some old projects.
    This Visual Basic code is equivlent to the Java code
    down below.

    Private Sub Form_Load()
    Me.Top = (Screen.Height - Me.Height) / 2
    Me.Left = (Screen.Width - Me.Width) / 2
    End Sub


    // center on display

    Dimension display = getToolkit().getScreenSize();
    Dimension bounds = this.getSize();

    // i used this because my class extended Frame

    int x = (display.width - bounds.width) / 2;
    int y = (display.height - bounds.height) / 2;

    if (x < 0) x = 10; // I forgot why i had these two
    if (y < 0) y = 15; // statements, but give them a try.

    this.setLocation(x,y);

  4. #4

    Thread Starter
    Member filburt1's Avatar
    Join Date
    Aug 1999
    Posts
    6,935
    Cool, it works.

  5. #5
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    the setLocaiton() method is defined in the javax.swing JComponent class along with getLocation(); They are inherited naturally from the java.awt.Component class.

  6. #6
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

  7. #7

    Thread Starter
    Member filburt1's Avatar
    Join Date
    Aug 1999
    Posts
    6,935
    *stares blankly* Oh, yeah, I know. Yeah. Right. *continues blank stare*

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