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
    评论
以下是示例代码,实现了JFrame窗口背景图片的自适应,并添加了一个文本框和密码框,并使其上下排列并居显示。 ```java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class MyFrame extends JFrame implements ComponentListener { private JLabel background; private JTextField textField; private JPasswordField passwordField; public MyFrame() { super("My Frame"); setSize(600, 400); setLocationRelativeTo(null); // 设置背景图片 ImageIcon imageIcon = new ImageIcon("background.jpg"); Image image = imageIcon.getImage().getScaledInstance(getWidth(), getHeight(), Image.SCALE_SMOOTH); background = new JLabel(new ImageIcon(image)); setContentPane(background); // 添加文本框和密码框 textField = new JTextField(20); passwordField = new JPasswordField(20); JPanel panel = new JPanel(); panel.add(textField); panel.add(passwordField); panel.setOpaque(false); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); add(panel, BorderLayout.CENTER); addComponentListener(this); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } // 窗口大小改变时,重新设置背景图大小 public void componentResized(ComponentEvent e) { ImageIcon imageIcon = new ImageIcon("background.jpg"); Image image = imageIcon.getImage().getScaledInstance(getWidth(), getHeight(), Image.SCALE_SMOOTH); background.setIcon(new ImageIcon(image)); } public void componentMoved(ComponentEvent e) {} public void componentShown(ComponentEvent e) {} public void componentHidden(ComponentEvent e) {} public static void main(String[] args) { new MyFrame(); } } ``` 需要注意的是,为了在窗口大小改变时重新设置背景图大小,我们需要实现 `ComponentListener` 接口,并注册监听器。同时,为了让文本框和密码框上下排列并居显示,我们使用了 `BoxLayout` 布局管理器,并将面板设置为透明。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值