package com.lu.gui;import javax.swing.*;
import java.awt.*;public class MyJFrame extends JFrame {public MyJFrame() {this.setBackground(Color.BLACK);this.setResizable(false);this.setSize(500,500);this.setTitle("登录页面");}
}
package com.lu.gui;import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;public class MyJpanel extends JPanel {public MyJpanel(){this.setBackground(Color.black);}@Overrideprotected void paintComponent(Graphics g) {super.paintComponent(g);g.setColor(Color.cyan);g.setFont(new Font("微软雅黑",Font.BOLD,20));g.drawString("I like you",250,100);BufferedImage bufferedImage = ImgUtil.readImage("img/l.jpg");g.drawImage(bufferedImage,0,0,500,500,null);//取消居中布局->自定义组件位置this.setLayout(null);//标签组件JLabel jLabel = new JLabel("用户登录:");jLabel.setBounds(100,100,100,30);this.add(jLabel);//单行文本框JTextField jTextField = new JTextField();jTextField.setText("请输入用户名");jTextField.setBounds(160,100,150,30);this.add(jTextField);//标签组件JLabel jLabel1 = new JLabel("密 码:");jLabel1.setBounds(100,150,100,30);this.add(jLabel1);//密码框JPasswordField jPasswordField = new JPasswordField();jPasswordField.setBounds(160,150,150,30);this.add(jPasswordField);JButton jButton = new JButton("登录");jButton.setBounds(180,200,80,20);this.add(jButton);}
}
package com.lu.gui;import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;public class ImgUtil {/*** 读取图片*/public static BufferedImage readImage(String path) {//获取类对象Class<ImgUtil> imgUtilClass = ImgUtil.class;//获取类加载器(系统类加载器)ClassLoader classLoader = imgUtilClass.getClassLoader();//通过类加载器获取内存静态资源InputStream resourceAsStream = classLoader.getResourceAsStream(path);Objects.requireNonNull(resourceAsStream,"请检查文件路径"+path);try {BufferedImage read = ImageIO.read(resourceAsStream);return read;} catch (IOException e) {throw new RuntimeException(e);}}
}
package com.lu.gui;public class Test {public static void main(String[] args) {MyJFrame myJFrame = new MyJFrame();myJFrame.setVisible(true);MyJpanel myJpanel = new MyJpanel();myJFrame.add(myJpanel);}
}