JavaSwing中如何让弹出的窗口显示居中

2025-03-20 21:22:10
推荐回答(3个)
回答(1):

private SplashPanel SPLASHPANEL=new SplashPanel();
private int splash_x;
private int splash_y;
private int splash_w;
private int splash_h;
private Container con;

public SplashWindow()
{
con=getContentPane();
con.setLayout(new BorderLayout());

Dimension screensize=Toolkit.getDefaultToolkit().getScreenSize();
Dimension splashsize=SPLASHPANEL.getPreferredSize();
splash_w=splashsize.width;
splash_h=splashsize.height;
splash_x=(int)((screensize.width-splash_w)/2);
splash_y=(int)((screensize.height-splash_h)/2);
con.add(SPLASHPANEL,BorderLayout.CENTER);
setBounds(splash_x,splash_y,splash_w,splash_h);
show();
}

回答(2):

frame = new MyWindow();
frame.setLocationRelativeTo(null);

回答(3):

setLocation()方法