JAVA SWING窗体程序--使用上次关闭时的大小和位置,设置背景图片

在窗体创建时,从首选项中读取上次关闭时保存的信息,就可以了。

frame.setFont(new Font("幼圆", Font.PLAIN, 19));
		frame.setTitle("微服务启动辅助工具");
		Preferences rootPreferences=Preferences.userRoot();//使用上次关闭时的位置和大小
		int x=rootPreferences.getInt(LocationX, 200);
		int y=rootPreferences.getInt(LocationY, 150);
		int with=rootPreferences.getInt(With, 995);
		int height=rootPreferences.getInt(Height, 792);
		frame.setBounds(x, y, with, height);

Preferences对不同系统,使用不同的记录方式,windows系统是使用注册表记录的。

在窗体关闭时,使用下面的方法,保存位置和大小信息,就可以了。

Preferences preferences=Preferences.userRoot();
		Point location= frame.getLocation();
		preferences.putInt(LocationX, location.x);
		preferences.putInt(LocationY, location.y);
		preferences.putInt(With, frame.getWidth());
		preferences.putInt(Height, frame.getHeight());

给控件设置背景图片,只需要重写指定pannel的paintComponent方法就可以
JAVA SWING窗体程序--使用上次关闭时的大小和位置,设置背景图片

new JPanel(){
			@Override
			protected void paintComponent(Graphics g) {
				super.paintComponent(g);
		
				g.drawImage(Toolkit.getDefaultToolkit().getImage(UrlHelper.Image_Base+File.separator+"center.jpg"), 0,0,getWidth(),getHeight(),this);
			}
		};