运行截图 代码实现 Jframe部分一个比较简单的swing程序,关于swing的一点基础在我的java游戏项目中有。需要修改的自行修改吧,当前只支持设置一个闹钟,可以保存设置的闹钟,闹钟响起后50s自动关闭,就不花时间完善了。
文章目录
- 运行截图
- 代码实现
- Jframe部分
- Panel部分
- MusicTimer 计时器
- TimeUtil 获取时间方法包
- 小结
public class Clock
{
public static void main(String[] args) {
Jframe jframe =new Jframe();
jframe .setTitle("某个闹钟");
int width= Toolkit.getDefaultToolkit().getScreenSize().width;
int height= Toolkit.getDefaultToolkit().getScreenSize().height;
jframe .setBounds((width-1500)/2,(height-700)/2,1500,700);
jframe .setResizable(true);
jframe .setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
BoringPanel panel=new BoringPanel();
jframe.add(panel);
jframe .setVisible(true);
}
}
Panel部分
public class BoringPanel extends JPanel {
String alarm;
String date;
String weekDay;
String time;
JLabel jLabel;
JLabel jLabelHour;
JLabel jLabelMin;
JLabel jLabelSec;
JButton jButton;
JTextField jHour;
JTextField jMin;
JTextField jSec;
public BoringPanel() {
init();
this.setFocusable(true);
showTime();
}
public void init() {
date = TimeUtil.getDate();
time = TimeUtil.refreshTime();
jLabel = new JLabel("设置闹钟");
jButton = new JButton("确定");
jHour = new JTextField(2);
jLabelHour = new JLabel("时");
jLabelMin = new JLabel("分");
jLabelSec = new JLabel("秒");
jSec = new JTextField(2);
jMin = new JTextField(2);
weekDay = "Day." + TimeUtil.getWeekDay();
this.add(jLabel);
this.add(jHour);
this.add(jLabelHour);
this.add(jMin);
this.add(jLabelMin);
this.add(jSec);
this.add(jLabelSec);
this.add(jButton);
alarm=GetValueByKey("src\main\java\exp05\alarm.properties","alarm");
jButton.addActionListener(e -> {
try {
makeAlarm();
} catch (IOException ex) {
ex.printStackTrace();
}
});
}
public void showTime() {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
repaint();
if(TimeUtil.refreshTime().equals(GetValueByKey("src\main\java\exp05\alarm.properties","alarm"))) {
try {
PlayMusic();
} catch (Exception e) {
e.printStackTrace();
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
thread.start();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(new Color(191, 179, 225));
g.setColor(new Color(231, 145, 118, 249));
g.setFont(new Font("宋体", Font.BOLD, 30));
g.drawString(date, 50, 50);
g.setFont(new Font("宋体", Font.BOLD, 100));
g.drawString(TimeUtil.refreshTime(), 520, 400);
g.setFont(new Font("宋体", Font.BOLD, 30));
g.drawString("已设置闹钟:"+ GetValueByKey("src\main\java\exp05\alarm.properties","alarm"),1100,70);
}
public void makeAlarm() throws IOException {
String hour = jHour.getText();
String min = jMin.getText();
String sec = jSec.getText();
String time = hour + ":" + min + ":" + sec;
if (time.matches("([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])")) {
writeProperties("src\main\java\exp05\alarm.properties","alarm",time);
JOptionPane.showMessageDialog(this, "闹钟设置成功");
} else {
Object[] options = {"确定 ", "取消 "};
JOptionPane.showOptionDialog(null, "您输入的不是闹钟格式 ", "提示", JOptionPane.DEFAULT_OPTION,
JOptionPane.WARNING_MESSAGE, null, options, options[0]);
}
}
public static void writeProperties(String fileName, String key, String value) throws IOException {
Properties pps = new Properties();
InputStream in = new FileInputStream(fileName);
pps.load(in);//加载所有原来的K-V对
OutputStream out = new FileOutputStream(fileName);
pps.setProperty(key, value);//写入一个K-V对
pps.store(out, "update " + key + ":" + value);//将所有K-V对写回到文件中
out.close();
}
public static String GetValueByKey(String fileName,String key){//获取单个属性值
Properties pps=new Properties();
String value;
try {
InputStream in =new BufferedInputStream(new FileInputStream(fileName));
pps.load(in);//只取一个也要加载所有K-V对
value=pps.getProperty(key);
return value;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public static void PlayMusic() throws Exception{
File file=new File("src\main\java\exp05\beyond.mp3");
FileInputStream stream= null;
stream = new FileInputStream(file);
Player player=new Player(new BufferedInputStream(stream));
Thread thread=new Thread(new Runnable() {
@Override
public void run() {
try {
player.play();
} catch (JavaLayerException e) {
e.printStackTrace();
}
}
});
thread.start();
Timer timer = new Timer();
timer.schedule(new MusicTimer(timer, player), 50*1000);
}
}
MusicTimer 计时器
public class MusicTimer extends TimerTask {
private Timer timer;
private Player player;
public MusicTimer(Timer timer,Player player)
{
this.timer=timer;
this.player=player;
}
@Override
public void run() {
if (timer != null ) {
timer.cancel();
player.close();
}
}
}
TimeUtil 获取时间方法包
public class TimeUtil {
String alarm;
static Calendar calendar=Calendar.getInstance();
static int year=calendar.get(Calendar.YEAR);
static int month=calendar.get(Calendar.MONTH);
static int day=calendar.get(Calendar.DAY_OF_MONTH);
static int weekDay=calendar.get(Calendar.DAY_OF_WEEK);
static String date=String.valueOf(year)+"年"+String.valueOf(month+1)+"月"+String.valueOf(day)+"日";
public static String getDate()
{
return date;
}
public static int getWeekDay()
{
return weekDay;
}
public static String refreshTime()
{
Calendar calendar1=Calendar.getInstance();
String hour=String.valueOf(calendar1.get(Calendar.HOUR_OF_DAY));
String minute=String.valueOf(calendar1.get(Calendar.MINUTE));
String second=String.valueOf(calendar1.get(Calendar.SECOND));
if(calendar1.get(Calendar.HOUR_OF_DAY)<10)
hour="0"+hour;
if(calendar1.get(Calendar.MINUTE)<10)
minute="0"+minute;
if(calendar1.get(Calendar.SECOND)<10)
second="0"+second;
return hour+":"+minute+":"+second;
}
}
小结
说实话,这学期学的Java课老师讲的挺差的,作业直接把代码发出来让大家改?(搞什么开发QAQ)
Java的很多精髓这门课并不能带给大家,作业要求也是一届比一届低,可能老师也有自己的无奈吧,毕竟学生水平也是一届比一届低(包括我自己0.0)



