java恶搞小程序

先上图:
在这里插入图片描述
点击“不是”大于五次后有惊喜哦
点击关闭弹出
在这里插入图片描述
在这里插入图片描述
代码放这里:

package myProject;


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 Project {

	public static void main(String[] args) {
		Project.init();
	}
	
	public static void init() {
		JFrame f=new JFrame("嘿嘿嘿");
		f.setLocation(500,200);
		f.setSize(300,200);
		f.setLayout(null);
		//设置窗口不能调整
		f.setResizable(false);
		//关闭时结束线程
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//去除窗口的标签栏
		f.setUndecorated(true);
		
		//添加标签
		JLabel l=new JLabel("你是傻子吗?");
		JLabel l1=new JLabel();
		ImageIcon icon=new ImageIcon("res/1.jpg");
		l1.setIcon(icon);
		l.setBounds(120,80, 200, 40);
		l1.setBounds(0,0,300,200);
		//设置字体
		l.setFont(new Font("宋体",Font.BOLD,18));
		f.add(l);
		f.add(l1);
		//添加按钮
		JButton j1=new JButton("是");
		JButton j2=new JButton("不是");
		JButton j3=new JButton("关闭");
		j1.setBounds(0,160,80,40);
		j2.setBounds(220,160,80,40);
		j3.setBounds(230,0,80,40);
		f.add(j1);
		f.add(j2);
		f.add(j3);
		//创建事件
		ProjectLister j=new ProjectLister(f, j1, j2,j3);
		j1.addActionListener(j);
		j2.addActionListener(j);
		j3.addActionListener(j);
		f.setVisible(true);
	}
}

class ProjectLister implements ActionListener{

	JFrame f;
	JButton j1;
	JButton j2;
	JButton j3;
	int k=0;//用于标记
	int i=0,j=0;
	Random rand=new Random();
	 public ProjectLister(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();
			i=rand.nextInt(1000);
			j=rand.nextInt(500);
			//创建新窗口
			JFrame f1=new JFrame("哈哈哈");
			f1.setLocation(i,j);
			f1.setSize(250,240);
			f1.setLayout(null);
			f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			//建立标签
			JLabel l1=new JLabel();
			ImageIcon icon=new ImageIcon("res/4.gif");
			l1.setBounds(0,0,250,240);
			l1.setIcon(icon);
			f1.add(l1);
			f1.setVisible(true);
		}
		else if(e.getSource()==j2) {
			//关闭窗口
			f.dispose();
			i=rand.nextInt(1000);
			j=rand.nextInt(500);
			f.setLocation(i,j);
			f.setVisible(true);
			k++;
			if(k>=5) {
				k=0;
				JOptionPane.showMessageDialog(f, "不要挣扎了!!!\n哈哈哈");
				i=rand.nextInt(1000);
				j=rand.nextInt(500);
				//建立窗口
				JFrame f1=new JFrame("哈哈哈");
				f1.setLocation(i,j);
				f1.setSize(310,330);
				f1.setLayout(null);
				//建立标签
				JLabel l1=new JLabel();
				ImageIcon icon=new ImageIcon("res/3.gif");
				l1.setBounds(0,0,300,300);
				l1.setIcon(icon);
				f1.add(l1);
				f1.setVisible(true);
			}
		}
		else if(e.getSource()==j3) {
			JOptionPane.showMessageDialog(f, "傻子!!!\n关不掉!!!\n哈哈哈!");
			i=rand.nextInt(1000);
			j=rand.nextInt(500);
			//建立窗口
			JFrame f1=new JFrame("哈哈哈");
			f1.setLocation(i,j);
			f1.setSize(310,330);
			f1.setLayout(null);
			//建立标签
			JLabel l1=new JLabel();
			ImageIcon icon=new ImageIcon("res/2.jpg");
			l1.setBounds(0,0,300,300);
			l1.setIcon(icon);
			f1.add(l1);
			f1.setVisible(true);
		}
	}
	
}

下载链接放在这里(下载完成后,解压可以直接运行)
链接:https://pan.baidu.com/s/1nIn61IcMWisnbIqNJeJHWg
提取码:2079

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

~浮生~

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

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

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

打赏作者

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

抵扣说明:

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

余额充值