Results 1 to 10 of 10

Thread: [RESOLVED] JFrame and JApplet Center on screen

  1. #1

    Thread Starter
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,024

    Resolved [RESOLVED] JFrame and JApplet Center on screen

    Hi,

    What's the method/code for centering a Japplet or JFrame on a monitor screen when loaded?

    Thanks...
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  2. #2
    Lively Member
    Join Date
    Jul 2002
    Posts
    86

    Re: JFrame and JApplet Center on screen

    This should do it:
    Code:
    JFrame main = new JFrame();
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    ...(set the size and all that)...
    main.setLocation(((screenSize.width / 2) - (main.width / 2)), 
                      ((screenSize.height / 2) - (main.height / 2)) );

  3. #3
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: JFrame and JApplet Center on screen

    Quote Originally Posted by KGComputers
    Hi,

    What's the method/code for centering a Japplet or JFrame on a monitor screen when loaded?

    Thanks...
    Applets are centered by HTML not by java code
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  4. #4
    Lively Member
    Join Date
    Jul 2002
    Posts
    86

    Re: JFrame and JApplet Center on screen

    Quote Originally Posted by KGComputers
    Hi,

    What's the method/code for centering a Japplet or JFrame on a monitor screen when loaded?

    Thanks...
    I was answering that part, for the JFrame

  5. #5

    Thread Starter
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,024

    Re: JFrame and JApplet Center on screen

    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class CenterJFrame{
           public static void main(String[] a){
    
    		JFrame main = new JFrame();
            
    		main.setSize(new Dimension(400,500));
    		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    		//..(set the size and all that)...
    		main.setLocation(((screenSize.width / 2) - (main.width / 2)),
                          ((screenSize.height / 2) - (main.height / 2)) );
            main.setVisible(true);
    }
    }
    Hi, when experimenting your code I got an error message...Am I missing something? Is it the package to import?

    Here's the error message..

    D:\java programs\swing API\CenterJFrame.java:15: width is not public in java.awt.Component; cannot be accessed from outside package
    main.setLocation(((screenSize.width / 2) - (main.width / 2)),
    ^
    D:\java programs\swing API\CenterJFrame.java:16: height is not public in java.awt.Component; cannot be accessed from outside package
    ((screenSize.height / 2) - (main.height / 2)) );
    ^
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  6. #6
    Lively Member
    Join Date
    Jul 2002
    Posts
    86

    Re: JFrame and JApplet Center on screen

    Oh yeah, sorry about that. The main.width and main.height should be main.getWidth() and main.getHeight().

  7. #7

    Thread Starter
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,024

    Re: JFrame and JApplet Center on screen

    Hi,

    Thanks for the help...

    Greg
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  8. #8
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: [RESOLVED] JFrame and JApplet Center on screen

    There was some othe way of doing this, but I honestly don't remember how. If I ever think of it, then I'll post it here.

  9. #9

    Thread Starter
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,024

    Re: [RESOLVED] JFrame and JApplet Center on screen

    Hi,

    Glad if you can add some code...Thanks in advance...If you can post an html code on how to center a JApplet on screen, that would be fine also...

    Greg
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  10. #10
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: [RESOLVED] JFrame and JApplet Center on screen

    HTML Code:
    <applet width="300" height="300" code="applet1.class">
    			<param name="param1" value="value" />
    			Please install java
    		</applet>
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

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