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);
}
}
JFrame中背景图大小随窗口大小自动改变
最新推荐文章于 2023-04-19 20:10:51 发布