Will "this" work for a JPanel that is extended??

[Solved/Closed]
Report
Posts
10
Registration date
Tuesday December 11, 2012
Status
Member
Last seen
February 4, 2013
-
Posts
2
Registration date
Saturday November 1, 2014
Status
Member
Last seen
November 1, 2014
-
Hello,
I am writing a small program in Java, and it is an interface which I would like to center on the page. I know that to have a GUI open in the middle of the screen it is setLocationRelativeTo(null);, but I am under the impression that this has to be preceded by an object, like obj.setLocationRelativeTo(null);.
Herein lies my question, if I have extended JPanel with my class, can I use this.setLocationRelativeTo();?? I am under the impression that this ia where the ":this" keyword would be used, but am not sure to be honest....
This is my code so far:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;

public class fileTwo extends JPanel {
public void paint.Component(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
Ellipse2D pie = new Ellipse2D.Double(240, 160, 48,48);
g2.setPaint(Color.YELLOW);
g2.fill(pie);

I am tempted to place the line this.setLocationRelativeTo(null);
as the next line, but I am not sure that I will not get yelled at by the compiler for that.
I want the panel to open in the middle of the screen??
My second thought is that I should just write setLocationRelativeTo(); with nothing else, but that just doesn't feel right either.
Please advise!!


3 replies

Posts
4476
Registration date
Thursday January 28, 2010
Status
Contributor
Last seen
August 2, 2020
768
No Guts, No Glory :)

Try it, if compiler complains, you would know. I have always found that in programming one learns more from mistakes then from smooth sailing.

You may see some thing here that would be of intererst
https://stackoverflow.com/questions/3480102/java-jframe-setlocationrelativetonull-not-centering-the-window-on-ubuntu-10-0
Posts
2
Registration date
Saturday November 1, 2014
Status
Member
Last seen
November 1, 2014

Posts
2
Registration date
Saturday November 1, 2014
Status
Member
Last seen
November 1, 2014