JFrame中背景图大小随窗口大小自动改变

package com.ui;
 
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
 
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
 
public class MainTopPanel extends JPanel {
	private static final long serialVersionUID = -2783727170881461229L;
	private int width;
	private int height;
	private Image image = new ImageIcon(getClass().getClassLoader().getResource("com/resource/ZHU.jpg")).getImage();
	private JButton btnLogon = new JButton(" 登录 ");
	private JButton btnCancel = new JButton("  取消  ");
	private JLabel labId = new JLabel("账号:");
	private JLabel labPass = new JLabel("密码:");
	private JTextField fieldId = new JTextField();
	private JPasswordField fieldPass = new JPasswordField();
	private int btnLogon_x = 535, btnLogon_y = 399;
	private int btnCancel_x = 655, btnCancel_y = 399;
	private int labId_x = 495, labId_y = 339;
	private int labPass_x = 495, labPass_y = 369;
	private int fieldId_x = 545, fieldId_y = 339;
	private int fieldPass_x = 545, fieldPass_y = 369;
 
	public MainTopPanel(int width, int height) {
		this.width = width;
		this.height = height;
		this.setLayout(null);
		this.init();
	}
 
	public void setScreenSize(int width, int height) { //设置窗口变化后的大小
		this.width = width;
		this.height = height;
	}
 
	public void setLocationComponent(int width, int height) { //计算当窗口放大或者缩小,组件自动调整位置
		int m = this.width - width;
		int n = this.height - height;
		this.btnLogon_x -= m;
		this.btnLogon_y -= n;
		this.btnCancel_x -= m;
		this.btnCancel_y -= n;
		this.labId_x -= m;
		this.labId_y -= n;
		this.labPass_x -= m;
		this.labPass_y -= n;
		this.fieldId_x -= m;
		this.fieldId_y -= n;
		this.fieldPass_x -= m;
		this.fieldPass_y -= n;
		btnLogon.setBounds(this.btnLogon_x, this.btnLogon_y, 102, 22);
		btnCancel.setBounds(this.btnCancel_x, this.btnCancel_y, 102, 22);
		labId.setBounds(this.labId_x, this.labId_y, 57, 17);
		labPass.setBounds(this.labPass_x, this.labPass_y, 57, 17);
		fieldId.setBounds(this.fieldId_x, this.fieldId_y, 191, 22);
		fieldPass.setBounds(this.fieldPass_x, this.fieldPass_y, 191, 22);
	}
 
	private void init() {
		labId.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		labId.setForeground(new Color(255, 255, 255));
		labId.setBounds(495, 339, 57, 17);
		labPass.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		labPass.setForeground(new Color(255, 255, 255));
		labPass.setBounds(495, 369, 57, 17);
 
		btnLogon.setBounds(535, 399, 102, 22);
		btnLogon.setBackground(new Color(57, 149, 136));
		btnCancel.setBackground(new Color(57, 149, 136));
		btnCancel.setBounds(655, 399, 102, 22);
 
		fieldId.setBounds(545, 339, 191, 22);
		fieldId.setBackground(new Color(57, 149, 136));
		fieldId.setForeground(new Color(255, 255, 255));
		fieldPass.setBounds(545, 369, 191, 22);
		fieldPass.setBackground(new Color(57, 149, 136));
		fieldPass.setForeground(new Color(255, 255, 255));
 
		this.add(labId);
		this.add(labPass);
		this.add(btnLogon);
		this.add(btnCancel);
		this.add(fieldId);
		this.add(fieldPass);
	}
 
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		g.drawImage(image, 0, 0, width, height ,this);
	}
 
} 

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值