有趣的java代码之恶搞小程序

185 阅读2分钟

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

image.png

代码放这里:

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);
		}
	}
}