生活随笔
收集整理的這篇文章主要介紹了
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
;}@Overridepublic 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
總結
以上是生活随笔為你收集整理的java恶搞小程序的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。