点击上方“全栈开发者社区”,选择“置顶公众号”

关键时刻,第一时间送达!

全栈资料:全栈开发者社区资源库更新啦! 小编从java开始带大家学习,大家养成好习惯,记得把公众号置顶,知识点由浅入深,相信跟着小编步伐坚持下去的将来一定精通全栈,行业大佬!有问题撩小编微信:15222301178

明天七夕了,为了程序员们的幸福,特此写了一个玫瑰花作为表白神器,祝大家表白成功,哈哈。

1 书写表白语句的frame(渐入功能)

package com.wanju.blessing;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

import shen.Panel;

import com.sun.awt.AWTUtilities;


public class ShowFrame extends JFrame {

 public ShowFrame() {
   this.setSize(600, 500);

   Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
   Dimension frameSize = this.getSize();
   this.setUndecorated(true);
   this.setLocation((screenSize.width - frameSize.width) / 2,
       (screenSize.height - frameSize.height) / 2);
   this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   Container container = this.getContentPane();
//    AWTUtilities.setWindowOpaque(this, true);
   AWTUtilities.setWindowOpacity(this, 0.01f);
   container.add(new MyPanel(this));
   this.setBackground(null);
   this.setVisible(true);
   new FadeOut(this).start();
 }

 public static void main(String[] args) {
   ShowFrame showFrame = new ShowFrame();
 }
}
class MyPanel extends JPanel{
 private Image background;
 JFrame frame;
 public MyPanel(final JFrame frame){
   this.frame = frame;
   try {
     URL url=Panel.class.getResource("a.jpg");  
     ImageIcon icon = new ImageIcon(url);
         background = icon.getImage() ;
//      background = ImageIO.read(new File("D:\workspace\maven\blessing\src\shen\b.jpg"));
     this.addMouseListener(new MouseAdapter() {
       @Override
       public void mouseClicked(MouseEvent e) {
         super.mouseClicked(e);
//          new FadeOut(MyPanel.this.frame).start();
         frame.setVisible(false);
         frame.dispose();
       }
     });
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
 protected void paintComponent(Graphics g) {
   super.paintComponent(g);
   g.drawImage(background,0,0,666,666,null);
   g.setColor(Color.red);
   g.setFont(new Font("", Font.BOLD, 15));
   
   
   
   
   g.drawString("祝愿宝儿永远幸福的像花儿一样", 60, 280);
 }
 
 
}
class FadeOut extends Thread {
   private JFrame wnd;
   public FadeOut(JFrame wnd) {
       this.wnd = wnd;
   }
   public void run() {
       try {
           for (int i = 0; i < 50; i++) {
             Thread.sleep(50);
             AWTUtilities.setWindowOpacity(wnd, i / 50f);
           }
           
       } catch (Exception ex) {
           ex.printStackTrace();
       }
   }
}

2 玫瑰花frame

package shen;

import java.awt.EventQueue;
import javax.swing.JFrame;


import com.sun.awt.AWTUtilities;

public class Frame extends JFrame {

 private static final long serialVersionUID = 7517576070147366983L;

 public Frame() {
   setTitle("PNG透明窗体");
   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   setUndecorated(true);
   setSize(666, 666);
   setLocationRelativeTo(null);
   AWTUtilities.setWindowOpaque(this, false);
   setContentPane(new Panel(this));
   addMouseListener(new MoveWindow(this));
 }

 public static void main(String[] args) {
   EventQueue.invokeLater(new Runnable() {
     @Override
     public void run() {
       new Frame().setVisible(true);
     }
   });
 }
}

3

package shen;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

import com.sun.awt.AWTUtilities;

import shen.Panel.FadeOut;

public class MoveWindow extends MouseAdapter {

 private Point last;
 private JFrame frame;

 public MoveWindow(JFrame frame) {
   this.frame = frame;
 }

 public void mousePressed(MouseEvent e) {
   last = e.getLocationOnScreen();
 }

 public void mouseReleased(MouseEvent e) {
   Point point = e.getLocationOnScreen();
   Point fPoint = frame.getLocation();
   frame.setLocation(fPoint.x + point.x - last.x, fPoint.y + point.y
       - last.y);
   last = point;
 }

}

顺手点下小广告(点开关闭即可,无需浏览关注等操作)

4

package shen;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

import com.sun.awt.AWTUtilities;
import com.wanju.blessing.ShowFrame;

public class Panel extends JPanel{

 private Image background;
 JFrame frame = null;
 public JFrame getFrame() {
   return frame;
 }

 public void setFrame(JFrame frame) {
   this.frame = frame;
 }

 public Panel(JFrame frame){
   this.frame = frame;
   try {
//      background = ImageIO.read(new File("D:\workspace\maven\blessing\src\shen\window.png"));
//      background = ImageIO.read(new File("D:\workspace\maven\blessing\src\shen\b.jpg"));
     URL url=Panel.class.getResource("c.jpg");  
     ImageIcon icon = new ImageIcon(url);
         background = icon.getImage() ;
//      background = ImageIO.read(new File("D:\workspace\maven\blessing\src\shen\b.jpg"));
     this.addMouseListener(new MouseAdapter() {
       @Override
       public void mouseClicked(MouseEvent e) {
         super.mouseClicked(e);
         new FadeOut(Panel.this.frame).start();
         
       }
     });
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
 
  class FadeOut extends Thread {
       private JFrame wnd;
       public FadeOut(JFrame wnd) {
           this.wnd = wnd;
       }
       public void run() {
           try {
               for (int i = 50; i > 0; i--) {
                   Thread.sleep(50);
                   AWTUtilities.setWindowOpacity(wnd, i / 50f);
               }
           } catch (Exception ex) {
               ex.printStackTrace();
           }
           wnd.setVisible(false);
           wnd.dispose();
           new ShowFrame();
       }
   }
 
 protected void paintComponent(Graphics g) {
   super.paintComponent(g);
   g.drawImage(background,0,0,666,666,null);
 }
}

5 效果图:

七夕程序员的表白神奇(附源码)-好好学Java

七夕程序员的表白神奇(附源码)-好好学Java

代码问题可找小编辅助调试,表白失败小编不背锅。

   👉公众号回复:"小编老邓头"

七夕程序员的表白神奇(附源码)-好好学Java

经典文章回顾:

初识微服务架构

老邓头Java学习大纲(一)

写给Java程序员学习路线图

老邓头Python学习大纲(一)

Linux下Redis的安装和部署

面试官最喜欢问的十道java面试题

老邓头Linux云计算架构师思维导图

一线互联网公司Java高级面试题总结

面试系列-40个Java多线程问题总结

SpringBoot入门系列:第一篇 Hello World

Java集合类: Set、List、Map、Queue使用场景梳理

Java Web架构知识整理——记一次面试经历(文末附内推要求)

美团、滴滴、阿里、京东的 Dubbo / JVM / Spring 面试题及答案

本公众号会不定期给大家发福利,包括送书、学习资源等,敬请期待吧!

推送内容如果现在工作用不上,可以先转发朋友圈或收藏,用的时候方便找。

顺手点点底部小广告哦,爱你们!

原文始发于微信公众号(全栈开发者社区):七夕程序员的表白神奇(附源码)