java惡搞小程式
阿新 • • 發佈:2021-01-30
技術標籤: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