QHYun_practice-7
首先准备一张壁纸放在Resource\Image\BackGround文件夹下吧(这个过程还是挺不好弄的,找一张好看的图可不简单= =)
然后在QHgui中写一个背景版的panel
package QHgui; import QHBasic.SConst; import javax.swing.*; import java.awt.*; import java.io.FileNotFoundException; import java.io.IOException; /** * <p> TODU </P> * * @author yeager * @Version V1.1.1.0 * @date 2023/11/22 17:35 */ public class QHLoginWindow extends JFrame { /*--------------------------fileds ---------------------------------*/ Color color = new Color(27,67,103); Color color2 = new Color(27,67,103); private static final int Width = 1536; private static final int Height = 864; //创建一个容器 Container ct; //创建背景面板。 BackgroundPanel bgp; /*--------------------------functions ---------------------------------*/ public QHLoginWindow(String text) throws FileNotFoundException, IOException { super(text, null); //获取屏幕尺寸 setSize(400,300); getContentPane().setLayout(null); this.setType(JFrame.Type.UTILITY); this.setUndecorated(true); //移到平面正中 moveToCenter(); //添加背景面板 addBackground(); //界面初始化完成后,重新界定界面和组件的大小 nodifyFrameSize(); } private void nodifyFrameSize() { int fraWidth = this.getWidth();//frame的宽 int fraHeight = this.getHeight();//fra // me的高 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); int screenWidth = screenSize.width; int screenHeight = screenSize.height; //重新设定界面尺寸大小 int winWidth = fraWidth * screenWidth / Width; int winHeight = fraHeight *screenHeight / Height; this.setSize(winWidth, winHeight); float proportionW = screenWidth/fraWidth; float proportionH = screenHeight/fraHeight; this.modifyComponentSize(this, proportionW,proportionH); } public static void modifyComponentSize(JFrame frame,float proportionW,float proportionH){ try { Component[] components = frame.getRootPane().getContentPane().getComponents(); for(Component co:components) { // String a = co.getClass().getName();//获取类型名称 // if(a.equals("javax.swing.JLabel")) // { // } float locX = co.getX() * proportionW; float locY = co.getY() * proportionH; float width = co.getWidth() * proportionW; float height = co.getHeight() * proportionH; co.setLocation((int)locX, (int)locY); co.setSize((int)width, (int)height); int size = (int)(co.getFont().getSize() * proportionH); Font font = new Font(co.getFont().getFontName(), co.getFont().getStyle(), size); co.setFont(font); } } catch (Exception e) { // TODO: handle exception } } /** * 移动到中间 */ private void moveToCenter() { Container myParent = getParent(); if (myParent == null) { Dimension parentSize = Toolkit.getDefaultToolkit().getScreenSize(); Point topLeft = new Point(0, 0); Dimension mySize = getSize(); int x, y; if (parentSize.width > mySize.width) x = ((parentSize.width - mySize.width) / 2) + topLeft.x; else x = topLeft.x; if (parentSize.height > mySize.height) y = ((parentSize.height - mySize.height) / 2) + topLeft.y; else y = topLeft.y; setLocation(x, y); } else { Point topLeft = myParent.getLocationOnScreen(); Dimension parentSize = myParent.getSize(); Dimension mySize = getSize(); int x, y; if (parentSize.width > mySize.width) x = ((parentSize.width - mySize.width) / 2) + topLeft.x; else x = topLeft.x; if (parentSize.height > mySize.height) y = ((parentSize.height - mySize.height) / 2) + topLeft.y; else y = topLeft.y; setLocation(x, y); } } /** * 添加背景 * @throws IOException * @throws FileNotFoundException */ private void addBackground() throws FileNotFoundException, IOException { //不采用任何布局方式。 ct = this.getContentPane(); getContentPane().setLayout(null); //在这里随便找一张400*300的照片既可以看到测试结果。 bgp = new BackgroundPanel(); bgp.setBounds(0, 0, 400, 300); bgp.setBackground(null); bgp.setOpaque(false); ct.add(bgp); } }
然后将BackGroundPanel加到登录界面中
/** * 添加背景 * @throws IOException * @throws FileNotFoundException */ private void addBackground() throws FileNotFoundException, IOException { //不采用任何布局方式。 ct = this.getContentPane(); getContentPane().setLayout(null); //在这里随便找一张400*300的照片既可以看到测试结果。 bgp = new BackgroundPanel(); bgp.setBounds(0, 0, 400, 300); bgp.setBackground(null); bgp.setOpaque(false); ct.add(bgp); }
运行结果:

 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号