Java 窗口 小马时钟
阅读原文时间:2023年07月10日阅读:2

写在前面:

  eclipse爽到

  好多都是抄的,记不住原网址了

  摸爆了

  搞了一个无边框JFrame,给JFrame加入鼠标监听器实现了拖动

  搞了按钮,可以关闭、最小化、始终显示在前、静音

  icon是抄(截)Fimfiction上的,rt

  默认背景是粉毛毛和邪茧(不要乱改图片,窗口大小是根据这张图片定的)

  在JFrame上加了JLabel、AudioClip、JButton

  可以整点报时(白天是音乐组荒岛舞曲,晚上是小蝶(云宝讲故事)+“两姐妹玩游戏”的旋律,yay~)

  ps: 电脑必须有java才能用

  运行效果:

上图为一般时候

上图为整点报时(白天)

Java代码如下:

1 package chryssi;
2
3 import java.applet.Applet;
4 import java.applet.AudioClip;
5 import java.net.URL;
6 import java.awt.*;
7 import java.awt.event.ActionEvent;
8 import java.awt.event.ActionListener;
9 import java.awt.event.MouseAdapter;
10 import java.awt.event.MouseEvent;
11 import java.awt.event.MouseMotionAdapter;
12 import java.io.*;
13 import java.util.*;
14 import javax.swing.ImageIcon;
15 import javax.swing.JButton;
16 import javax.swing.JFrame;
17 import javax.swing.JLabel;
18
19 public class PonyClock extends JFrame
20 {
21 static String ponyString;
22 static boolean ponymute=true;
23 static int pframexOri,pframeyOri,pframex,pframey;
24
25 public static void main(String[] args) throws Exception
26 {
27 JFrame ponyFrame=new JFrame("PonyClock");
28 JLabel ponyLabel = new JLabel("",JLabel.CENTER);
29 File directory = new File("");
30 ponyFrame.setIconImage(Toolkit.getDefaultToolkit().getImage(directory.getCanonicalPath()+"\\icons\\32.png"));
31 ImageIcon img00=new ImageIcon(Toolkit.getDefaultToolkit().getImage(directory.getCanonicalPath()+"\\pictures\\bg.png"));
32 ImageIcon img01=new ImageIcon(Toolkit.getDefaultToolkit().getImage(directory.getCanonicalPath()+"\\pictures\\kisses.gif"));
33 ImageIcon img02=new ImageIcon(Toolkit.getDefaultToolkit().getImage(directory.getCanonicalPath()+"\\pictures\\03.jpg"));
34 ImageIcon img03=new ImageIcon(Toolkit.getDefaultToolkit().getImage(directory.getCanonicalPath()+"\\pictures\\02.jpg"));
35 AudioClip ponyam,ponypm;
36 File ponyf1=new File(directory.getCanonicalPath()+"\\audio\\Octavia_in_Tropics.wav");
37 URL ponyurl1=ponyf1.toURI().toURL();
38 File ponyf2=new File(directory.getCanonicalPath()+"\\audio\\Two_Best_Sisters.wav");
39 URL ponyurl2=ponyf2.toURI().toURL();
40 JButton ponyb_front = new JButton("");
41 JButton ponyb_exit = new JButton("");
42 JButton ponyb_mute = new JButton("");
43 JButton ponyb_min = new JButton("");
44 ImageIcon ponyb_fronti = new ImageIcon(Toolkit.getDefaultToolkit().getImage(directory.getCanonicalPath()+"\\icons\\front.png"));
45 ImageIcon ponyb_nfronti = new ImageIcon(Toolkit.getDefaultToolkit().getImage(directory.getCanonicalPath()+"\\icons\\nfront.png"));
46 ImageIcon ponyb_exiti = new ImageIcon(Toolkit.getDefaultToolkit().getImage(directory.getCanonicalPath()+"\\icons\\exit.png"));
47 ImageIcon ponyb_mutei = new ImageIcon(Toolkit.getDefaultToolkit().getImage(directory.getCanonicalPath()+"\\icons\\mute.png"));
48 ImageIcon ponyb_nmutei = new ImageIcon(Toolkit.getDefaultToolkit().getImage(directory.getCanonicalPath()+"\\icons\\nmute.png"));
49 ImageIcon ponyb_mini = new ImageIcon(Toolkit.getDefaultToolkit().getImage(directory.getCanonicalPath()+"\\icons\\min.png"));
50
51 /*Whole*/
52 ponyFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
53 ponyFrame.setLayout(null);
54 ponyFrame.setResizable(false);
55 ponyFrame.setLocationRelativeTo(null);
56 ponyFrame.setSize(img01.getIconWidth(),img01.getIconHeight());
57 ponyFrame.setUndecorated(true);
58 ponyFrame.setAlwaysOnTop(false);
59 ponyFrame.setBackground(new Color(255,255,255,255));
60 ponyFrame.addMouseListener(new MouseAdapter() {
61 @Override
62 public void mousePressed(MouseEvent e) {
63 pframexOri=e.getX();
64 pframeyOri=e.getY();
65 }
66 });
67 ponyFrame.addMouseMotionListener(new MouseMotionAdapter() {
68 @Override
69 public void mouseDragged(MouseEvent e) {
70 ponyFrame.setLocation(e.getXOnScreen()-pframexOri,e.getYOnScreen()-pframeyOri);
71 }
72 });
73
74 /*BUTTON_FRONT*/
75 ponyb_front.setIcon(ponyb_nfronti);
76 ponyb_front.setSize(30, 30);
77 ponyb_front.setLocation(img01.getIconWidth()-125, 10);
78 ponyb_front.addActionListener(new ActionListener(){
79 public void actionPerformed(ActionEvent e)
80 {
81 if(ponyFrame.isAlwaysOnTop()==true)
82 {
83 ponyFrame.setAlwaysOnTop(false);
84 ponyb_front.setIcon(ponyb_nfronti);
85 }
86 else
87 {
88 ponyFrame.setAlwaysOnTop(true);
89 ponyb_front.setIcon(ponyb_fronti);
90 }
91 }
92 });
93 ponyFrame.add(ponyb_front);
94
95 /*BUTTON_EXIT*/
96 ponyb_exit.setIcon(ponyb_exiti);
97 ponyb_exit.setSize(30, 30);
98 ponyb_exit.setLocation(img01.getIconWidth()-45, 10);
99 ponyb_exit.addActionListener(new ActionListener(){
100 public void actionPerformed(ActionEvent e)
101 {
102 System.exit(0);
103 }
104 });
105 ponyFrame.add(ponyb_exit);
106
107 /*BUTTON_MUTE*/
108 ponyb_mute.setIcon(ponyb_mutei);
109 ponyb_mute.setSize(30, 30);
110 ponyb_mute.setLocation(img01.getIconWidth()-165, 10);
111 ponyb_mute.addActionListener(new ActionListener(){
112 public void actionPerformed(ActionEvent e)
113 {
114 if(ponymute==true)
115 {
116 ponymute=false;
117 ponyb_mute.setIcon(ponyb_nmutei);
118 }
119 else
120 {
121 ponymute=true;
122 ponyb_mute.setIcon(ponyb_mutei);
123 }
124 }
125 });
126 ponyFrame.add(ponyb_mute);
127
128 /*BUTTON_MIN*/
129 ponyb_min.setIcon(ponyb_mini);
130 ponyb_min.setSize(30, 30);
131 ponyb_min.setLocation(img01.getIconWidth()-85, 10);
132 ponyb_min.addActionListener(new ActionListener(){
133 public void actionPerformed(ActionEvent e)
134 {
135 ponyFrame.setState(Frame.ICONIFIED);
136 }
137 });
138 ponyFrame.add(ponyb_min);
139
140 /*TEXT*/
141 ponyFrame.add(ponyLabel);
142 ponyLabel.setSize(img01.getIconWidth(),img01.getIconHeight());
143 ponyLabel.setFont(new Font("",1,82));
144 ponyLabel.setForeground(new Color(255,255,255,255));
145
146 /*AUDIO*/
147 ponyam=Applet.newAudioClip(ponyurl1);
148 ponypm=Applet.newAudioClip(ponyurl2);
149
150 /*IMG00*/
151 JLabel Label00 = new JLabel(img00);
152 Label00.setSize(img00.getIconWidth(),img00.getIconHeight());
153 ponyFrame.add(Label00);
154
155 /*IMG01*/
156 JLabel Label01 = new JLabel(img01);
157 Label01.setSize(img01.getIconWidth(),img01.getIconHeight());
158 ponyFrame.add(Label01);
159
160 /*IMG02*/
161 JLabel Label02 = new JLabel(img02);
162 Label02.setSize(0,0);
163 ponyFrame.add(Label02);
164
165 /*IMG03*/
166 JLabel Label03 = new JLabel(img03);
167 Label03.setSize(0,0);
168 ponyFrame.add(Label03);
169
170 /*FINAL*/
171 ponyFrame.setVisible(true);
172
173 while(true)
174 {
175 if(Calendar.getInstance().get(Calendar.MINUTE)==0 && Calendar.getInstance().get(Calendar.SECOND)<=8) 176 { 177 ponyString=Calendar.getInstance().get(Calendar.HOUR_OF_DAY)+""; 178 ponyLabel.setText(ponyString); 179 ponyLabel.setForeground(new Color(0,0,0,255)); 180 Label00.setSize(0,0); 181 Label01.setSize(0,0); 182 if(Calendar.getInstance().get(Calendar.HOUR_OF_DAY)>=8 && Calendar.getInstance().get(Calendar.HOUR_OF_DAY)<=19)
183 {
184 Label02.setSize(img02.getIconWidth(),img02.getIconHeight());
185 try{
186 if(ponymute==false) {ponyam.play();}
187 Thread.sleep(15000);
188 }catch (InterruptedException e){}
189 Label02.setSize(0,0);
190 }
191 else
192 {
193 Label03.setSize(img03.getIconWidth(),img03.getIconHeight());
194 try{
195 if(ponymute==false) {ponypm.play();}
196 Thread.sleep(12000);
197 }catch (InterruptedException e){}
198 Label03.setSize(0,0);
199 }
200 ponyLabel.setForeground(new Color(255,255,255,255));
201 Label00.setSize(img01.getIconWidth(),img01.getIconHeight());
202 Label01.setSize(img01.getIconWidth(),img01.getIconHeight());
203 }
204 else
205 {
206 ponyString=Calendar.getInstance().get(Calendar.HOUR_OF_DAY) + ":" + Calendar.getInstance().get(Calendar.MINUTE) + ":" + Calendar.getInstance().get(Calendar.SECOND);
207 ponyLabel.setText(ponyString);
208 try{
209 Thread.sleep(1000);
210 }catch (InterruptedException e){}
211 }
212 }
213 }
214
215 }

注释太少了,将就看吧

附件:

*jar所在目录*\\icons\\

32.png

exit.png

front.png

min.png

mute.png

nfront.png

nmute.png

*jar所在目录*\\pictures\\

01.jpg02.jpg03.jpgbg.pngkisses.gif

*jar所在目录*\\audio\\

(见网盘)

完整版歌曲:https://www.bilibili.com/video/av42429289?from=search&seid=4061369087358968249

所有:

  链接: https://盘.baidu.com/s/13SHi和谐Cdi0和谐7RP和谐lyKtp-ob和谐m4Q

  提取码: 7rig

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章