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方法就可以
在这里插入图片描述

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);
			}
		};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值