Hi,
What's the method/code for centering a Japplet or JFrame on a monitor screen when loaded?
Thanks... :) :) :)
Printable View
Hi,
What's the method/code for centering a Japplet or JFrame on a monitor screen when loaded?
Thanks... :) :) :)
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)) );
Applets are centered by HTML not by java codeQuote:
Originally Posted by KGComputers
I was answering that part, for the JFrameQuote:
Originally Posted by KGComputers
Hi, when experimenting your code I got an error message...Am I missing something? Is it the package to import?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);
}
}
Here's the error message..
:) :) :)Quote:
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)) );
^
Oh yeah, sorry about that. The main.width and main.height should be main.getWidth() and main.getHeight().
Hi,
Thanks for the help...
Greg :thumb: :) :thumb:
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.
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 :) :) :)
HTML Code:<applet width="300" height="300" code="applet1.class">
<param name="param1" value="value" />
Please install java
</applet>