JFrame默认的窗体比较土,可以通过一定的美化,让窗体表现的比较漂亮,具体要根据设计的设计图进行美化;

JFrame美化的大致思路:先将JFrame去除默认美化效果,实现JWindow效果,然后再JWindow基础上对窗体的各项内容自定义设置与美化,大多用到插入背景图片、添加各种鼠标事件、弹窗与输入框等圆角等各种美化;

下面是一个登录窗体的美化代码,以后再有对窗体美化的可以参照一下代码进行美化:

1、Login实体类代码:

package com;import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Image;
import java.awt.geom.RoundRectangle2D;
import java.io.File;import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JLabel;import com.alibaba.fastjson.JSON;
import com.model.Message;
import com.model.User;
import com.sun.awt.AWTUtilities;
import com.util.FileUtils;
import com.util.MyButton;
import com.util.MyLineBorder;
import com.util.VerifyCodeUtils;import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JTextField;
import javax.swing.border.MatteBorder;
import javax.swing.JPasswordField;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
/*** 用户登录窗体(按照设计图片效果制作)* @author admin**/
public class Login1{private JFrame frame;//窗体private JTextField userNameField;//用户名输入框private JPasswordField passwordField;//密码输入框private JTextField verifyCodeField;//验证码输入框private String verifyCode;//验证码图片中的验证码值/*** Launch the application.*/public static void main(String[] args) {EventQueue.invokeLater(new Runnable() {public void run() {try {Login1 window = new Login1();window.frame.setVisible(true);} catch (Exception e) {e.printStackTrace();}}});}/*** Create the application.*/public Login1() {initialize();}/*** Initialize the contents of the frame.*/private void initialize() {//将存储验证码文件夹下的所有验证码图片删除FileUtils.deleteAllFiles("./verifyCodeImg");//自定义圆角输入框边界线MyLineBorder myLineBorder = new MyLineBorder(new Color(192, 192, 192), 1 , true);//只显示输入框的下边框MatteBorder bottomBorder = new MatteBorder(0, 0, 1, 0, new Color(192, 192, 192));//设置JFrame禁用本地外观,使用下面自定义设置的外观;JFrame.setDefaultLookAndFeelDecorated(true);frame = new JFrame();frame.setBounds(0, 0, 300, 490);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.getContentPane().setLayout(null);/*** 对窗体进行基本设置*///设置窗体在计算机窗口的中心部位显示frame.setLocationRelativeTo(frame.getOwner());// 去掉窗口的装饰frame.setUndecorated(true);//采用指定的窗口装饰风格frame.getRootPane().setWindowDecorationStyle(JRootPane.NONE);//设置窗体圆角,最后两个参数分别为圆角的宽度、高度数值,一般这两个数值都是一样的AWTUtilities.setWindowShape(frame,new RoundRectangle2D.Double(0.0D, 0.0D, frame.getWidth(), frame.getHeight(), 20.0D, 20.0D));//设置背景颜色,记住一定要修改frame.getContentPane()的颜色,因为我们看到的都是这个的颜色而并不是frame的颜色frame.getContentPane().setBackground(Color.white);/*** 插入顶部非凡汽车背景图片*///创建具有分层的JLayeredPaneJLayeredPane layeredPane = new JLayeredPane();layeredPane.setBounds(0, -5, 300, 200);frame.getContentPane().add(layeredPane);// 创建图片对象ImageIcon img = new ImageIcon(Login1.class.getResource("/images/dingbu.jpg"));//设置图片在窗体中显示的宽度、高度img.setImage(img.getImage().getScaledInstance(300, 200,Image.SCALE_DEFAULT));JPanel panel = new JPanel();panel.setBounds(0, -5, 300, 200);layeredPane.add(panel, JLayeredPane.DEFAULT_LAYER);JLabel lblNewLabel = new JLabel("");panel.add(lblNewLabel);lblNewLabel.setIcon(img);/*** 插入窗体关闭的背景图片及功能*/// 创建图片对象ImageIcon closeImg = new ImageIcon(Login1.class.getResource("/images/close.png"));//设置图片在窗体中显示的宽度、高度closeImg.setImage(closeImg.getImage().getScaledInstance(31, 31,Image.SCALE_DEFAULT));JPanel closePanel = new JPanel();closePanel.setBounds(269, -5, 31, 31);layeredPane.add(closePanel,JLayeredPane.MODAL_LAYER);JLabel closeLabel = new JLabel("");closePanel.add(closeLabel);closeLabel.setIcon(closeImg);closeLabel.addMouseListener(new MouseAdapter() {//鼠标点击关闭图片,实现关闭窗体的功能@Overridepublic void mouseClicked(MouseEvent e) {//dispose(); System.exit(0);//使用dispose();也可以关闭只是不是真正的关闭}//鼠标进入,换关闭的背景图片@Overridepublic void mouseEntered(MouseEvent e) {// 创建图片对象ImageIcon closeImg1 = new ImageIcon(Login1.class.getResource("/images/mouse_close.png"));//设置图片在窗体中显示的宽度、高度closeImg1.setImage(closeImg1.getImage().getScaledInstance(31, 31,Image.SCALE_DEFAULT));closeLabel.setIcon(closeImg1);}//鼠标离开,换关闭的背景图片@Overridepublic void mouseExited(MouseEvent e) {// 创建图片对象ImageIcon closeImg = new ImageIcon(Login1.class.getResource("/images/close.png"));//设置图片在窗体中显示的宽度、高度closeImg.setImage(closeImg.getImage().getScaledInstance(31, 31,Image.SCALE_DEFAULT));closeLabel.setIcon(closeImg);}});/*** 插入窗体最小化的背景图片及功能*/// 创建图片对象ImageIcon minImg = new ImageIcon(Login1.class.getResource("/images/min.png"));//设置图片在窗体中显示的宽度、高度minImg.setImage(minImg.getImage().getScaledInstance(31, 31,Image.SCALE_DEFAULT));JPanel minPanel = new JPanel();minPanel.setBounds(237, -5, 31, 31);layeredPane.add(minPanel,JLayeredPane.MODAL_LAYER);JLabel minLabel = new JLabel("");minLabel.addMouseListener(new MouseAdapter() {//鼠标点击最小化图片,实现最小化窗体的功能@Overridepublic void mouseClicked(MouseEvent e) {frame.setExtendedState(JFrame.ICONIFIED);//最小化窗体}//鼠标进入,换最小化的背景图片@Overridepublic void mouseEntered(MouseEvent e) {// 创建图片对象ImageIcon minImg1 = new ImageIcon(Login1.class.getResource("/images/mouse_min.png"));//设置图片在窗体中显示的宽度、高度minImg1.setImage(minImg1.getImage().getScaledInstance(31, 31,Image.SCALE_DEFAULT));minLabel.setIcon(minImg1);}//鼠标离开,换最小化的背景图片@Overridepublic void mouseExited(MouseEvent e) {// 创建图片对象ImageIcon minImg = new ImageIcon(Login1.class.getResource("/images/min.png"));//设置图片在窗体中显示的宽度、高度minImg.setImage(minImg.getImage().getScaledInstance(31, 31,Image.SCALE_DEFAULT));minLabel.setIcon(minImg);}});minPanel.add(minLabel);minLabel.setIcon(minImg);/*** 插入用户名输入框前面的图片*/// 创建图片对象ImageIcon userNameImg = new ImageIcon(Login1.class.getResource("/images/user_name.png"));//设置图片在窗体中显示的宽度、高度userNameImg.setImage(userNameImg.getImage().getScaledInstance(40, 40,Image.SCALE_DEFAULT));JLabel userNameLabel = new JLabel("");userNameLabel.setBounds(0, 220, 40, 40);userNameLabel.setIcon(userNameImg);//默认获取光标userNameLabel.setFocusable(true);frame.getContentPane().add(userNameLabel);/*** 添加圆角的用户名输入框*/userNameField = new JTextField();userNameField.setBounds(50, 220, 235, 50);userNameField.setBorder(bottomBorder);userNameField.setText(" 用户名");userNameField.setFont(new Font("微软雅黑", 0, 14));userNameField.setForeground(Color.GRAY);//默认设置输入框中的文字颜色为灰色userNameField.addFocusListener(new FocusAdapter() {//获取光标事件@Overridepublic void focusGained(FocusEvent e) {//获取焦点时,输入框中内容是“用户名”,那么去掉输入框中显示的内容;if("用户名".equals((userNameField.getText().trim()))){userNameField.setText("");userNameField.setForeground(Color.black);//设置颜色为黑色}}//失去光标事件@Overridepublic void focusLost(FocusEvent e) {//失去焦点时,如果输入框中去掉空格后的字符串为空串则显示用户名if("".equals((userNameField.getText().trim()))){userNameField.setText(" 用户名");userNameField.setFont(new Font("微软雅黑", 0, 14));userNameField.setForeground(Color.GRAY);//默认设置输入框中的文字颜色为灰色}}});frame.getContentPane().add(userNameField);userNameField.setColumns(10);/*** 插入密码输入框前面的图片*/// 创建图片对象ImageIcon passwordImg = new ImageIcon(Login1.class.getResource("/images/password.png"));//设置图片在窗体中显示的宽度、高度passwordImg.setImage(passwordImg.getImage().getScaledInstance(40, 40,Image.SCALE_DEFAULT));JLabel passwordLabel = new JLabel("");passwordLabel.setBounds(0, 280, 40, 40);passwordLabel.setIcon(passwordImg);frame.getContentPane().add(passwordLabel);/*** 添加圆角的密码输入框*/passwordField = new JPasswordField();passwordField.setBounds(50, 280, 235, 50);passwordField.setBorder(bottomBorder);passwordField.setText(" 密码");passwordField.setFont(new Font("微软雅黑", 0, 14));passwordField.setForeground(Color.GRAY);//默认设置输入框中的文字颜色为灰色passwordField.setEchoChar((char)0);//显示密码输入框中内容passwordField.addFocusListener(new FocusAdapter() {//获取光标事件@Overridepublic void focusGained(FocusEvent e) {//获取焦点时,输入框中内容是“用户名”,那么去掉输入框中显示的内容;if("密码".equals((passwordField.getText().trim()))){passwordField.setText("");passwordField.setEchoChar('*');//显示密码输入框中内容passwordField.setForeground(Color.black);//设置颜色为黑色}}//失去光标事件@Overridepublic void focusLost(FocusEvent e) {//失去焦点时,如果输入框中去掉空格后的字符串为空串则显示用户名if("".equals((passwordField.getText().trim()))){passwordField.setText(" 密码");passwordField.setFont(new Font("微软雅黑", 0, 14));passwordField.setForeground(Color.GRAY);//默认设置输入框中的文字颜色为灰色passwordField.setEchoChar((char)0);//显示密码输入框中内容}}});frame.getContentPane().add(passwordField);/*** 插入验证码输入框前面的图片*/// 创建图片对象ImageIcon verifyCodeImg = new ImageIcon(Login1.class.getResource("/images/verify_code.png"));//设置图片在窗体中显示的宽度、高度verifyCodeImg.setImage(verifyCodeImg.getImage().getScaledInstance(40, 40,Image.SCALE_DEFAULT));JLabel verifyCodeLabel = new JLabel("");verifyCodeLabel.setBounds(0, 340, 40, 40);verifyCodeLabel.setIcon(verifyCodeImg);frame.getContentPane().add(verifyCodeLabel);/*** 添加圆角的验证码输入框*/verifyCodeField = new JTextField();verifyCodeField.setBounds(50, 340, 135, 50);verifyCodeField.setBorder(bottomBorder);verifyCodeField.setText(" 验证码");verifyCodeField.setFont(new Font("微软雅黑", 0, 14));verifyCodeField.setForeground(Color.GRAY);//默认设置输入框中的文字颜色为灰色verifyCodeField.addFocusListener(new FocusAdapter() {//获取光标事件@Overridepublic void focusGained(FocusEvent e) {//获取焦点时,输入框中内容是“用户名”,那么去掉输入框中显示的内容;if("验证码".equals((verifyCodeField.getText().trim()))){verifyCodeField.setText("");verifyCodeField.setForeground(Color.black);//设置颜色为黑色}}//失去光标事件@Overridepublic void focusLost(FocusEvent e) {//失去焦点时,如果输入框中去掉空格后的字符串为空串则显示用户名if("".equals((verifyCodeField.getText().trim()))){verifyCodeField.setText(" 验证码");verifyCodeField.setFont(new Font("微软雅黑", 0, 14));verifyCodeField.setForeground(Color.GRAY);//默认设置输入框中的文字颜色为灰色}}});frame.getContentPane().add(verifyCodeField);verifyCodeField.setColumns(10);/*** 添加验证码图片*/JLabel verifyCodeImgLabel = new JLabel("");verifyCodeImgLabel.setBounds(190, 340, 95, 50);verifyCodeImgLabel.setBorder(myLineBorder);frame.getContentPane().add(verifyCodeImgLabel);//生成一张验证码图片verifyCode = VerifyCodeUtils.createOneCodeImage();//将刚生成的验证码图片显示在窗体中去ImageIcon verifyCodeSourceImg = new ImageIcon("./verifyCodeImg/"+verifyCode+".jpg");// 创建图片对象//设置图片在窗体中显示的宽度、高度verifyCodeSourceImg.setImage(verifyCodeSourceImg.getImage().getScaledInstance(95, 50,Image.SCALE_DEFAULT));verifyCodeImgLabel.setIcon(verifyCodeSourceImg);//点击验证码图片,换一个新的验证码图片verifyCodeImgLabel.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {//删除上一次的验证码图片File file = new File("./src/verifyCodeImg/"+verifyCode+".jpg");if(file.exists()){file.delete();}//生成一张新的验证码图片verifyCode = VerifyCodeUtils.createOneCodeImage();ImageIcon verifyCodeSourceImg1 = new ImageIcon("./verifyCodeImg/"+verifyCode+".jpg");verifyCodeSourceImg1.setImage(verifyCodeSourceImg1.getImage().getScaledInstance(95, 50,Image.SCALE_DEFAULT));verifyCodeImgLabel.setIcon(verifyCodeSourceImg1);}});/*** 添加提示性信息的JLabel*/JLabel reminderMessage = new JLabel("",JLabel.CENTER);reminderMessage.setBounds(15, 395, 270, 20);reminderMessage.setForeground(Color.red);reminderMessage.setFont(new Font("微软雅黑", 0, 12));frame.getContentPane().add(reminderMessage);/*** 添加圆角的提交按钮*/MyButton myButton = new MyButton("安全登录", 0);myButton.setBounds(15, 420, 270, 50);frame.getContentPane().add(myButton);//设置窗体可见frame.setVisible(true);}
}

2、工具类FileUtils代码:

package com.util;
import java.io.File;/*** 对文件操作的工具类* @author admin**/
public class FileUtils {/*** 删除指定文件夹下的所有文件,此文件夹内只有文件,没有任何文件夹*/public static Boolean deleteAllFiles(String filePath){Boolean result = false;File file = new File(filePath);File temp = null;if(file.exists()){String [] tempList = file.list();for (String string : tempList) {temp = new File(filePath+"/"+string);if(temp.isFile()){temp.delete();}}tempList = file.list();if(tempList.length==0){result = true;}}return result;}public static void main(String[] args) {Boolean result = FileUtils.deleteAllFiles("./verifyCodeImg");System.out.println(result);}
}

3、工具类MyButton代码:

package com.util;import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.RoundRectangle2D;
import javax.swing.JButton;
import javax.swing.JFrame;
/*** 自定义带有圆角的按钮工具类* @author admin**/
public class MyButton extends JButton {/* 决定圆角的弧度 */public static int radius = 4;public static Color COLOR1, COLOR2;public static int pink = 3, ashen = 2, sky = 1, stone = 0;/* 粉红 */public static Color pink1 = new Color(39, 121, 181);public static Color pink2 = new Color(39, 121, 181);/* 灰白 */public static Color ashen1 = new Color(39, 121, 181);public static Color ashen2 = new Color(39, 121, 181);/* 深宝石蓝 */public static Color stone1 = new Color(39, 121, 181);public static Color stone2 = new Color(39, 121, 181);/* 淡天蓝色 */public static Color sky1 = new Color(39, 121, 181);public static Color sky2 = new Color(39, 121, 181);/* 光标进入按钮判断 */private boolean hover;public MyButton() {this("", stone);}public MyButton(String name) {this(name, stone);}public MyButton(String name, int style) {super.setText(name);if (style == pink) {COLOR1 = pink1;COLOR2 = pink2;}if (style == ashen) {COLOR1 = ashen1;COLOR2 = ashen2;}if (style == stone) {COLOR1 = stone1;COLOR2 = stone2;}if (style == sky) {COLOR1 = sky1;COLOR2 = sky2;}paintcolor(COLOR1, COLOR2);}private void paintcolor(Color COLOR1, Color COLOR2) {setMargin(new Insets(0, 0, 0, 0));setFont(new Font("微软雅黑", 0, 14));setBorderPainted(false);setForeground(Color.white);setFocusPainted(false);setContentAreaFilled(false);}@Overrideprotected void paintComponent(Graphics g) {Graphics2D g2d = (Graphics2D) g.create();int height = getHeight();int with = getWidth();float tran = 0.9F;/*if (!hover) { 鼠标离开/进入时的透明度改变量tran = 0.6F;}*/g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);/* GradientPaint是颜色渐变类 */GradientPaint p1;GradientPaint p2;if (getModel().isPressed()) {p1 = new GradientPaint(0, 0, new Color(0, 0, 0), 0, height, new Color(100, 100, 100), true);p2 = new GradientPaint(0, 1, new Color(0, 0, 0, 50), 0, height, new Color(255, 255, 255, 100), true);} else {p1 = new GradientPaint(0, 0, new Color(100, 100, 100), 0, height, new Color(0, 0, 0), true);p2 = new GradientPaint(0, 1, new Color(255, 255, 255, 100), 0, height, new Color(0, 0, 0, 50), true);}g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, tran));RoundRectangle2D.Float r2d = new RoundRectangle2D.Float(0, 0, with - 1, height - 1, radius, radius);// 最后两个参数数值越大,按钮越圆,以下同理Shape clip = g2d.getClip();g2d.clip(r2d);GradientPaint gp = new GradientPaint(0.0F, 0.0F, COLOR1, 0.0F, height / 2, COLOR2, true);g2d.setPaint(gp);g2d.fillRect(0, 0, with, height);g2d.setClip(clip);g2d.setPaint(p1);g2d.drawRoundRect(0, 0, with - 3, height - 3, radius, radius);g2d.setPaint(p2);g2d.drawRoundRect(1, 1, with - 3, height - 3, radius, radius);g2d.dispose();super.paintComponent(g);}public static void main(String args[]) {JFrame frm = new JFrame();MyButton but = new MyButton("圆角JButton", 0);frm.setLayout(null);frm.setBounds(800, 400, 500, 200);but.setBounds(30, 30, 200, 50);frm.add(but);frm.setDefaultCloseOperation(3);frm.setVisible(true);}
}

4、工具类MyLineBorder代码:

package com.util; import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.border.LineBorder;/*** 为创建圆角输入框自定义边框线条工具类* @author admin**/
public class MyLineBorder extends LineBorder{  private static final long serialVersionUID = 1L;    public MyLineBorder(Color color, int thickness, boolean roundedCorners) { super(color, thickness, roundedCorners); } public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) { RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); Color oldColor = g.getColor(); Graphics2D g2 = (Graphics2D)g; int i; g2.setRenderingHints(rh); g2.setColor(lineColor); for(i = 0; i < thickness; i++) { if(!roundedCorners) g2.drawRect(x+i, y+i, width-i-i-1, height-i-i-1); elseg2.drawRoundRect(x+i, y+i, width-i-i-1, height-i-i-1, 10, 10); //就是这一句 ,后两个参数控制圆角的大小} g2.setColor(oldColor); }
}

5、工具类VerifyCodeUtils代码:

package com.util;import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.Random;
import javax.imageio.ImageIO;
/*** 生成验证码图片的工具类* @author admin**/
public class VerifyCodeUtils {// 使用到Algerian字体,系统里没有的话需要安装字体,字体只显示大写,去掉了1,0,i,o几个容易混淆的字符public static final String VERIFY_CODES = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ";private static Random random = new Random();/*** 使用系统默认字符源生成验证码** @param verifySize*      验证码长度* @return*/public static String generateVerifyCode(int verifySize) {return generateVerifyCode(verifySize, VERIFY_CODES);}/*** 使用指定源生成验证码** @param verifySize*      验证码长度* @param sources*      验证码字符源* @return*/public static String generateVerifyCode(int verifySize, String sources) {if (sources == null || sources.length() == 0) {sources = VERIFY_CODES;}int codesLen = sources.length();Random rand = new Random(System.currentTimeMillis());StringBuilder verifyCode = new StringBuilder(verifySize);for (int i = 0; i < verifySize; i++) {verifyCode.append(sources.charAt(rand.nextInt(codesLen - 1)));}return verifyCode.toString();}/*** 生成随机验证码文件,并返回验证码值** @param w* @param h* @param outputFile* @param verifySize* @return* @throws IOException*/public static String outputVerifyImage(int w, int h, File outputFile, int verifySize) throws IOException {String verifyCode = generateVerifyCode(verifySize);outputImage(w, h, outputFile, verifyCode);return verifyCode;}/*** 输出随机验证码图片流,并返回验证码值** @param w* @param h* @param os* @param verifySize* @return* @throws IOException*/public static String outputVerifyImage(int w, int h, OutputStream os, int verifySize) throws IOException {String verifyCode = generateVerifyCode(verifySize);outputImage(w, h, os, verifyCode);return verifyCode;}/*** 生成指定验证码图像文件** @param w* @param h* @param outputFile* @param code* @throws IOException*/public static void outputImage(int w, int h, File outputFile, String code) throws IOException {if (outputFile == null) {return;}File dir = outputFile.getParentFile();if (!dir.exists()) {dir.mkdirs();}try {outputFile.createNewFile();FileOutputStream fos = new FileOutputStream(outputFile);outputImage(w, h, fos, code);fos.close();} catch (IOException e) {throw e;}}/*** 输出指定验证码图片流** @param w* @param h* @param os* @param code* @throws IOException*/public static void outputImage(int w, int h, OutputStream os, String code) throws IOException {int verifySize = code.length();BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);Random rand = new Random();Graphics2D g2 = image.createGraphics();g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);Color[] colors = new Color[5];Color[] colorSpaces = new Color[] { Color.WHITE, Color.CYAN, Color.GRAY, Color.LIGHT_GRAY, Color.MAGENTA,Color.ORANGE, Color.PINK, Color.YELLOW };float[] fractions = new float[colors.length];for (int i = 0; i < colors.length; i++) {colors[i] = colorSpaces[rand.nextInt(colorSpaces.length)];fractions[i] = rand.nextFloat();}Arrays.sort(fractions);g2.setColor(Color.GRAY);// 设置边框色g2.fillRect(0, 0, w, h);Color c = getRandColor(200, 250);g2.setColor(c);// 设置背景色g2.fillRect(0, 2, w, h - 4);// 绘制干扰线Random random = new Random();g2.setColor(getRandColor(160, 200));// 设置线条的颜色for (int i = 0; i < 20; i++) {int x = random.nextInt(w - 1);int y = random.nextInt(h - 1);int xl = random.nextInt(6) + 1;int yl = random.nextInt(12) + 1;g2.drawLine(x, y, x + xl + 40, y + yl + 20);}// 添加噪点float yawpRate = 0.05f;// 噪声率int area = (int) (yawpRate * w * h);for (int i = 0; i < area; i++) {int x = random.nextInt(w);int y = random.nextInt(h);int rgb = getRandomIntColor();image.setRGB(x, y, rgb);}shear(g2, w, h, c);// 使图片扭曲g2.setColor(getRandColor(100, 160));int fontSize = h - 4;Font font = new Font("Algerian", Font.ITALIC, fontSize);g2.setFont(font);char[] chars = code.toCharArray();for (int i = 0; i < verifySize; i++) {AffineTransform affine = new AffineTransform();affine.setToRotation(Math.PI / 4 * rand.nextDouble() * (rand.nextBoolean() ? 1 : -1),(w / verifySize) * i + fontSize / 2, h / 2);g2.setTransform(affine);g2.drawChars(chars, i, 1, ((w - 10) / verifySize) * i + 5, h / 2 + fontSize / 2 - 10);}g2.dispose();ImageIO.write(image, "jpg", os);}private static Color getRandColor(int fc, int bc) {if (fc > 255)fc = 255;if (bc > 255)bc = 255;int r = fc + random.nextInt(bc - fc);int g = fc + random.nextInt(bc - fc);int b = fc + random.nextInt(bc - fc);return new Color(r, g, b);}private static int getRandomIntColor() {int[] rgb = getRandomRgb();int color = 0;for (int c : rgb) {color = color << 8;color = color | c;}return color;}private static int[] getRandomRgb() {int[] rgb = new int[3];for (int i = 0; i < 3; i++) {rgb[i] = random.nextInt(255);}return rgb;}private static void shear(Graphics g, int w1, int h1, Color color) {shearX(g, w1, h1, color);shearY(g, w1, h1, color);}private static void shearX(Graphics g, int w1, int h1, Color color) {int period = random.nextInt(2); boolean borderGap = true;int frames = 1;int phase = random.nextInt(2);for (int i = 0; i < h1; i++) {double d = (double) (period >> 1)* Math.sin((double) i / (double) period + (6.2831853071795862D * (double) phase) / (double) frames);g.copyArea(0, i, w1, 1, (int) d, 0);if (borderGap) {g.setColor(color);g.drawLine((int) d, i, 0, i);g.drawLine((int) d + w1, i, w1, i);}} }private static void shearY(Graphics g, int w1, int h1, Color color) {int period = random.nextInt(40) + 10; // 50;boolean borderGap = true;int frames = 20;int phase = 7;for (int i = 0; i < w1; i++) {double d = (double) (period >> 1)* Math.sin((double) i / (double) period + (6.2831853071795862D * (double) phase) / (double) frames);g.copyArea(i, 0, 1, h1, 0, (int) d);if (borderGap) {g.setColor(color);g.drawLine(i, (int) d, i, 0);g.drawLine(i, (int) d + h1, i, h1);} } }//生成一张验证码图片,并保存到项目的verifyCodeImg文件夹下public static String createOneCodeImage(){String imgName = "";try {File dir = new File("./verifyCodeImg");int w = 95, h = 50;String verifyCode = generateVerifyCode(4);File file = new File(dir, verifyCode + ".jpg");outputImage(w, h, file, verifyCode);imgName = verifyCode;} catch (IOException e) {imgName = "";e.printStackTrace();}finally{return imgName;}}public static void main(String[] args) throws IOException {String codeImage = VerifyCodeUtils.createOneCodeImage();System.out.println(codeImage);}
}
 想要更多资讯关注小编或加qq1411943564领取免费Java学习资料

三分钟带你领路Java-JFrame窗体美化相关推荐

  1. 还在动手画棋盘 ?20分钟带你用Java写一个井字棋!

    还在动手画棋盘 ?20分钟带你用Java写一个井字棋! 文章目录 还在动手画棋盘 ?20分钟带你用Java写一个井字棋! 前言 设计过程 1.创建窗体类MyGameWindow 2.创建窗体的构造器 ...

  2. java jframe面板美化,Java-JFrame窗体美化

    Java-JFrame窗体美化 JFrame默认的窗体比较土,可以通过一定的美化,让窗体表现的比较漂亮,具体要根据设计的设计图进行美化: JFrame美化的大致思路:先将JFrame去除默认美化效果, ...

  3. mysql和oracle冲突吗_三分钟带你分清MySQL 和Oracle之间的误区

    原标题:三分钟带你分清MySQL 和Oracle之间的误区 来自:华为云开发者社区 摘要:MySQL和Oracle,别再傻傻分不清. MySQL 和Oracle 在开发中的使用是随处可见的,那就简单去 ...

  4. 三分钟带你看懂prototype原型——ES6进阶

    三分钟带你看懂prototype原型--ES6进阶 1. prototype 定义 2. new 构造函数 3. 存储 4. prototype 作用 1. prototype 定义 在JS中的类的实 ...

  5. 三分钟带你弄懂slot插槽——vue进阶

    文章目录 三分钟带你弄懂slot插槽--vue进阶 一.概述 程序员之死 什么是 slot插槽? 2.6.0 版本中的 slot 二.具名插槽 例子 效果图 代码 三.小惊喜 三分钟带你弄懂slot插 ...

  6. 三分钟带你看懂HDMI接口的PCB设计

    三分钟带你看懂HDMI接口的PCB设计 本文主要讲解的是HDMI的设计,包括作用和运用的总结,希望大家看了以后能轻松的应对各种HDMI方案的PCB设计. 一.什么是HDMI? 高清晰度多媒体接口(英文 ...

  7. 三分钟带你读懂 BERT

    本文为 AI 研习社编译的技术博客,原标题 : BERT Technology introduced in 3-minutes 作者 | Suleiman Khan, Ph.D. 翻译 | 胡瑛皓.s ...

  8. 建网站的最简单方法(三分钟带后台)

    建网站的最简单方法(三分钟带后台) 准备材料 服务器或者本地环境 安装过程 准备材料 织梦二次开发模板或者Ecshop二次开发模板等(我以织梦为例讲解) 如果是本地需要下载ComsenzEXP或者Wa ...

  9. 三分钟带你了解不一样的场效应管

    目前市场上的场效应管主要分为两种,但不论是哪种,场效应管都具有功耗低噪声小,而且不会出现二次击穿的特点,这让其成为替代晶体管的最佳产品.随着场效应管应用面积的扩大,对场效应管的选择和检查也成为了设计者 ...

最新文章

  1. 看动画轻松理解“递归”与“动态规划”
  2. SVN checkout
  3. PHP文件包含漏洞原理分析和利用方法
  4. java中的数据加密1 消息摘要
  5. 使用Xdoclet和Ant构建Hibernate映射和配置文件
  6. 易语言反截图_【易语言】模仿QQ截图
  7. CMD命令之BAT脚本路径信息
  8. 时间加减计算器_小学生苦练加减乘除计算“基本功”,有没有必要?
  9. canvas图形绘制
  10. An Introduction to Unstructured Mesh Generation Methods and Softwares for Scientific Computing
  11. 复杂参数的基本使用方式
  12. mysql数据库原理与应用武洪萍第三张答案_MySQL数据库原理及应用(第2版)(微课版)...
  13. java 调用热敏打印机_java调用芯烨热敏打印机 TSPL2指令 打印没有反应
  14. 怎么把pdf文件转换成word方法分享
  15. 北大核心2020_2020年北大核心论文如何发表
  16. Android中的通知Notification
  17. 【第三期】电子元器件创意作品,附带高清原图,共计60张
  18. SDC公司为美国银行建设的数据中心破土动工
  19. 【目标检测】60、MMYOLO | 如何使用 MMYOLO 训练模型
  20. 登陆失败 禁用当前的账户

热门文章

  1. P1282 多米诺骨牌
  2. matlab 使用fprintf打印度数符号
  3. 服装电商APP软件开发平台作用
  4. python安装 错误 “User installations are disabled via policy on the machine”
  5. 神雕侠侣服务器维修时间,神雕侠侣什么时候更新_神雕侠侣更新维护了什么内容_快吧游戏...
  6. 互联网晚报 | 6月28日 星期二|​ QQ回应大规模账号被盗;iPhone 14系列新机最快8月初量产;微信表情符号写入判决...
  7. frangi黑森矩阵_消灭黑森臭虫
  8. 几款国外主流虚拟主机控制面板介绍
  9. 禁止ios版本上拉回弹效果
  10. VScode代码格式化及语法检测