整体APP代码
该项目的工程图如下。
注意:JRE是1.8的版本哦!
Main.java的代码如下(我好像不用说,想一想就应该知道)。
package main;
import surface.MyFrame;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
new MyFrame().setVisible(true);
}
}
MyFrame的代码如下:
package surface;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
public class MyFrame extends JFrame {
Point pressedPoint;
public MyFrame() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(300,150);
setLocationRelativeTo(null);
setResizable(false);
setUndecorated(true);
this.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {}
@Override
public void mousePressed(MouseEvent e) {
pressedPoint = e.getPoint();
}
@Override
public void mouseReleased(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
});
this.addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseMoved(MouseEvent e) {}
@Override
public void mouseDragged(MouseEvent e) {
Point point = e.getPoint();
Point locationPoint = getLocation();
int x = locationPoint.x + point.x - pressedPoint.x;
int y = locationPoint.y + point.y - pressedPoint.y;
setLocation(x,y);
}
});
addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {}
@Override
public void keyPressed(KeyEvent e) {}
@Override
public void keyReleased(KeyEvent e) {
int code = e.getKeyCode();
if (code == KeyEvent.VK_ESCAPE) {
System.exit(0);
}
}
});
MyPanel panel = new MyPanel();
this.add(panel);
new Thread() {
public void run() {
while(true) {
panel.repaint();
}
}
}.start();
}
}
MyPanel.java的代码如下。
package surface;
import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.JPanel;
import java.util.*;
public class MyPanel extends JPanel {
Calendar cal = Calendar.getInstance();
public MyPanel() {
}
public void paint(Graphics g) {
//背景
g.setColor(Color.white);
g.fillRect(0, 0, 300, 200);
g.setFont(new Font("宋体",Font.PLAIN,25));
//时间
g.setColor(Color.black);
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
g.drawString(sdf.format(date).toString(), 0, 30);
//星期几
g.drawString(chooseDay(), 110, 60);
//距离跨年还剩多少天
// g.setColor(Color.red);
StringBuilder sb = new StringBuilder();
sb.append("距离");
int nextyear = cal.get(Calendar.YEAR)+1;
sb.append(nextyear);
sb.append("年还剩");
int dayOfYear = cal.get(Calendar.DAY_OF_YEAR);
sb.append(checkYear() - dayOfYear);
sb.append("天!");
g.drawString(sb.toString(), 15, 90);
sb = null;
System.gc();
g.setFont(new Font("宋体",Font.PLAIN,23));
g.drawString("不想看了请按一下Esc退出哦!", 0, 120);
}
private String chooseDay() {
String str = "星期";
String what = null;
int day = cal.get(Calendar.DAY_OF_WEEK);
if (day==1) {
what = "日";
} else if (day==2) {
what = "一";
} else if (day==3) {
what = "二";
} else if (day==4) {
what = "三";
} else if (day==5) {
what = "四";
} else if (day==6) {
what = "五";
} else if (day==7) {
what = "六";
}
return str + what;
}
private int checkYear() {
int dayOfYear = cal.get(Calendar.DAY_OF_YEAR);
if (dayOfYear%4==0 || dayOfYear%400==0) {
return 366;
} else {
return 365;
}
}
}
实现效果如下。
换成exe更爽!
敲知识点
有一些App,为了让界面更好看,就把外框全部取消掉,自己来定。这种效果能否用大名鼎鼎的Java实现呢?答案是肯定的。我们要用到JFrame的这个函数。
setUndecorated(boolean b);
如果参数b是true的话,就会实现这样的效果。
然而你运行的话,又会搞来一个问题:外面的无啦~拖拽的功能也无啦~
所以我们要用一个类!
import java.awt.Point;
先给你的JFrame添加变量全局变量Point类型的pressedPoint,然后写这些代码,就可以了。
this.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {}
@Override
public void mousePressed(MouseEvent e) {
pressedPoint = e.getPoint();
}
@Override
public void mouseReleased(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
});
this.addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseMoved(MouseEvent e) {}
@Override
public void mouseDragged(MouseEvent e) {
Point point = e.getPoint();
Point locationPoint = getLocation();
int x = locationPoint.x + point.x - pressedPoint.x;
int y = locationPoint.y + point.y - pressedPoint.y;
setLocation(x,y);
}
});