表白小程序(java版本,已经打包,解压后运行)

放假在家,写的表白小程序,喜欢的可以下载看看

先上图:
在这里插入图片描述

图片可以换成心仪对象的照片哦!
文章最后的下载包里面有打包好的exe文件,将在同目录下的res文件中的love2.jpg删除,将女朋友的美照复制进去,命名为love2.jpg(注意照片的分辨率最好为128x128,不然显示不完整)就可以替换程序的文件了

点击一次“不是”按钮后:

在这里插入图片描述
点击两次“不是”按钮后
在这里插入图片描述
点击“是”以后:
在这里插入图片描述
最后会弹出:
在这里插入图片描述

源代码放这里:

package biaobai;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;



public class Love {

	//控制窗口的位置
	int width=450;
	int high=200;
    JFrame f=new JFrame("回答我");
	//定义窗口的储存数组
	static Love a[][]=new Love[5][5];
	
	public static void main(String[] args) {
		new Love().init();
		
	}

	public Love() {};
	
	public Love(int width,int high) {
		this.width=width;
		this.high=high;
	}
	
	public void init() {
		
		
		f.setLocation(width,high);
		f.setSize(300,210);
		f.setLayout(null);
		//设置窗口不能调整
		f.setResizable(false);
		//去除窗口的标签栏
		f.setUndecorated(true);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		//添加标签
		ImageIcon icon=new ImageIcon("res/love2.jpg");
		JLabel l=new JLabel("你是不是喜欢我?");
		JLabel l3=new JLabel();
		l.setBounds(145,90,200,20);
		l3.setIcon(icon);
		l3.setBounds(0,0,135,160);
		l.setFont(new Font("宋体",Font.BOLD,18));
		f.add(l3);
		f.add(l);
		
		JButton j1=new JButton("是");
		JButton j2=new JButton("不是");
		JButton j3=new JButton("关闭");
		j1.setBounds(0,160,80,50);
		j2.setBounds(220,160,80,50);
		j3.setBounds(220,0,80,50);
		f.add(j1);
		f.add(j2);
		f.add(j3);
		//创建事件
		Love_1 l1=new Love_1(f, j1, j2,j3);
		j1.addActionListener(l1);
		j2.addActionListener(l1);
		j3.addActionListener(l1);
		f.setVisible(true);
	}
	
	//创建一个窗口,表白图片,播放音乐???
	//此窗口结束时,退出程序
	public static void init2() {
		JFrame f1=new JFrame("I LOVE YOU !!!");
		f1.setLocation(400,250);
		f1.setSize(486,217);
		f1.setLayout(null);
		f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//定义图片和标签
		ImageIcon icon=new ImageIcon("res/love.gif");
		JLabel l=new JLabel();
		l.setBounds(0,0,468,217);
		l.setIcon(icon);
		f1.add(l);
		f1.setVisible(true);
	}
	
}


//事件监听
class Love_1 implements ActionListener{

	JFrame f;
	JButton j1;
	JButton j2;
	JButton j3;
	int i=0;
	int j=0;
	static int k=0,k1=0,k2=0;//用于标记
	
	public Love_1(JFrame f,	JButton j1,	JButton j2,JButton j3){
		this.f=f;
		this.j1=j1;
		this.j2=j2;
		this.j3=j3;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		//点击了是
		if(e.getSource()==j1) {
			JOptionPane.showMessageDialog(f, "喜欢就直说嘛~~~");
			//关闭窗口
			f.dispose();
			
			//判断,当k2==1的时候 关闭 点击 不是按钮 引起的25个窗口
			if(k2==1) {
				k2=3;//改变数值,防止重复关闭
				for(int i=0;i<5;i++)
					for(int j=0;j<5;j++) {
						Love.a[i][j].f.dispose();
					}
				
			}
			
			//调用方法
			Love.init2();
			
		}
		//点击了不是
		else if(e.getSource()==j2) {
			Random rand=new Random();
			i=rand.nextInt(1000);
			j=rand.nextInt(500);
			f.setLocation(i,j);
			f.setVisible(true);
			k++;
			if(k==1) {
				JOptionPane.showMessageDialog(f, "不许再点了!\n再点我生气了");
				k1=k;
			}
			else if(k1==1) {
				//关闭原窗口
				f.dispose();
				k2=1;//标记赋值
				k1=0;//重置为0,防止程序再次执行
				for(int i=0;i<5;i++)
				for(int j=0;j<5;j++){
					//实例化窗口
					Love.a[i][j]=new Love(i*300,j*200);
					Love.a[i][j].init();
				}
				
				
			}
			
		}
		//点击了关闭按钮
		else if(e.getSource()==j3) {
			JOptionPane.showMessageDialog(f, "你以为关闭就可以回避这个话题吗?");
		}
		
	}
	
}

源代码文件在这里,需要的可以下载:

注:下载包里有已经打包好的exe文件,点击可以直接运行,压缩后可以
直接发给对方,不需要java环境(已经自带jre包)

链接: https://download.csdn.net/download/qq_52889967/14990399

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

~浮生~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值