Set the JFrame to the center of the screen in NetBeans

I am developing a java swing desktop application with NetBeans and I want to set the JFrame to the center of the screen.

from the network I understand that I can use

setLocationRelativeTo(null);

to set the frame to the center But I can’t paste the code into the NetBeans IDE because

    frame.pack()
and frame.setVisible()

NetBeans 7 IDE codes are generated and this will prevent code from being entered between these two methods. to get the following:

frame.pack() 
setLocationRelativeTo(null);
frame.setVisible()

Any suggestions to fix the problem.

+5
source share
9 answers

setVisible() ? . , JFrame Navigator Properties. Code , . initComponents() JFrame.

+1

β†’ β†’ Generate Center

+12

:

public frame() {     
   initComponents();
}

: "this.setLocationRelativeTo(null);" "initComponents();"

:

public frame() {     
   initComponents();
   this.setLocationRelativeTo(null);
}

, =)

+10

:

  • .
  • " ".

.

+3

, , NetBeans. , , , , .

http://forums.netbeans.org/ptopic37419.html

, GUI .

, , :

Properties β†’ Code. 1. , ( !) 2. "" JFrame. 3. " ". 4. " ". 5. , "..." . 6. , " " .

, [0,0] . [450,0]. x 450, Y 0. , Y 0 450.

, 226740 NetBeans, . , , , .

+2

@DerekMannering :

Netbeans , setLocationRelativeTo. "" . " " "". " ". , " " . , , " " . , . JFrame Frame.

0

Designer Netbeans JFrame, .

Inside the code, change the form size policy to Create Resize Code

Then select the Generate Center option.

0
source
  • when writing the constructor giving the code below, make your jframe in the center of the screen

    public ProjectWork_jframe() {
    initComponents();
    
    Dimension screenSize,frameSize;
    int x,y;
    screenSize=Toolkit.getDefaultToolkit().getScreenSize();
    frameSize=getSize();
    x=(screenSize.width-frameSize.width)/2;
    y=(screenSize.height-frameSize.height)/2;
    setLocation(x, y);
    }
    
0
source

to try ....

public class_name{
     initComponents();
        setLocationRelativeTo(this);
}
0
source

All Articles