我是一名很普通很普通的大学生。大一接触C,大二接触C++,而现在开始学习JAVA,因为我有C++的基础,所以书一到我就开始看GUI部分,不过,入手第一程序想尝试写数字电子钟。因为完全没有看前面的内容,直接看得GUI,所以里面涉及线程,接口,类继承等,都是依照经验来做的。
做出程序如下:(没权限上图,把代码帖出来,望大家指教)
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.*;
import javax.swing.*;
public class TimeTread extends TimePanel implements Runnable
{
private JButton aButton; //报时键
private JButton bButton; //调节键
private JButton cButton; //调模键
private Font font; //显示字体
private Thread clock;
//小时 分钟 秒 全局用updataTime()更新
//Calendar now;
private int hour;
private int min;
private int sec;
private int beepHour;
private int beepMin;
private int beepSec;
private int state;
private boolean isVis;
private String musicName;
private URL localUrl;
private AudioClip music;
public TimeTread()
{
//初始化全局时分秒
hour = 0;
min = 0;
sec = 0;
//初始化全局时分秒闹钟
beepHour = 0;
beepMin = 0;
beepSec = 0;
isVis = false;
state = 0;
aButton = new JButton("报时");
bButton = new JButton("调节");
cButton = new JButton("调模");
aButton.setBackground(Color.LIGHT_GRAY);
bButton.setBackground(Color.LIGHT_GRAY);
cButton.setBackground(Color.LIGHT_GRAY);
aButton.addActionListener(this);
bButton.addActionListener(this);
cButton.addActionListener(this);
//嵌套显示三按钮
JPanel southPanel = new JPanel();
southPanel.add(aButton);
southPanel.add(bButton);
southPanel.add(cButton);
JPanel northPanel = new JPanel();
JPanel westPanel = new JPanel();
JPanel eastPanel = new JPanel();
southPanel.setBackground(Color.lightGray);
northPanel.setBackground(Color.lightGray);
westPanel.setBackground(Color.lightGray);
eastPanel.setBackground(Color.lightGray);
//布局
southPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
setLayout(new BorderLayout());
add(southPanel,BorderLayout.SOUTH);
add(northPanel,BorderLayout.NORTH);
add(westPanel,BorderLayout.WEST);
add(eastPanel,BorderLayout.EAST);
//开始进程
start();
}
public void setState(int s)
{
state = s;
}
public int getState()
{
return state;
}
public void delay(int ms)
{
try{
Thread.sleep(ms); //线程暂停
}
catch (InterruptedException ex){
ex.printStackTrace(); //输出出错信息
}
}
public void playNumber(int n)
{
if(n >= 0 && n <= 10)
musicName = "data//sounds//" + String.valueOf(n)+ ".wav"; //定义音乐文件名/
try { //定义文件路径
localUrl = new URL("file:" + new File(".").getCanonicalPath() + "//" + musicName);
music = Applet.newAudioClip(localUrl); //返回AudioClip对象
}
catch (IOException e) {
System.out.println(e);
}
music.play();
}
public void playH()
{
musicName = "data//sounds//d.wav"; //定义音乐文件名/
try { //定义文件路径
localUrl = new URL("file:" + new File(".").getCanonicalPath() + "//" + musicName);
music = Applet.newAudioClip(localUrl); //返回AudioClip对象
}
catch (IOException e) {
System.out.println(e);
}
music.play();
}
public void playM()
{
musicName = "data//sounds//f.wav"; //定义音乐文件名/
try { //定义文件路径
localUrl = new URL("file:" + new File(".").getCanonicalPath() + "//" + musicName);
music = Applet.newAudioClip(localUrl); //返回AudioClip对象
}
catch (IOException e) {
System.out.println(e);
}
music.play();
}
public void playPointHour(int h)
{
if(h < 10)
musicName = "data//sounds//0"+ String.valueOf(h)+".wav"; //定义音乐文件名/
else if(h == 10)
musicName = "data//sounds//10d.wav";
else if(h > 10 && h < 24 )
musicName = "data//sounds//"+ String.valueOf(h)+".wav";
try { //定义文件路径
localUrl = new URL("file:" + new File(".").getCanonicalPath() + "//" + musicName);
music = Applet.newAudioClip(localUrl); //返回AudioClip对象
}
catch (IOException e) {
System.out.println(e);
}
music.play();
}
public void playS()
{
musicName = "data//sounds//s.wav"; //定义音乐文件名/
try { //定义文件路径
localUrl = new URL("file:" + new File(".").getCanonicalPath() + "//" + musicName);
music = Applet.newAudioClip(localUrl); //返回AudioClip对象
}
catch (IOException e) {
System.out.println(e);
}
music.play();
}
public void playTime(int h , int m , int s )
{
int h1 = h/10;
int h2 = h%10;
int m1 = m/10;
int m2 = m%10;
musicName = "data//sounds//x.wav"; //定义音乐文件名/
try { //定义文件路径
localUrl = new URL("file:" + new File(".").getCanonicalPath() + "//" + musicName);
music = Applet.newAudioClip(localUrl); //返回AudioClip对象
}
catch (IOException e) {
System.out.println(e);
}
music.play();
delay(1500); //暂停1500MS
if( h1 != 0 )
{
if( h1 != 1)
{playNumber(h1);;}
delay(500);
playNumber(10);
delay(500);
}
if(!( h2 == 0 && h1 != 0))
{playNumber(h2);}
delay(500);
playH();
delay(500);
if( m1 != 0 )
{
if( m1 != 1)
{playNumber(m1);;}
delay(500);
playNumber(10);
delay(500);
}
if(!( m2 == 0 && m1 != 0))
{playNumber(m2);}
delay(500);
playM();
}
public void playMusic(int c)
{
musicName = "data//sounds//00"+String.valueOf(c)+".wav"; //定义音乐文件名/
try { //定义文件路径
localUrl = new URL("file:" + new File(".").getCanonicalPath() + "//" + musicName);
music = Applet.newAudioClip(localUrl); //返回AudioClip对象
}
catch (IOException e) {
System.out.println(e);
}
music.play();
}
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if(source == aButton )
{
playTime(hour, min , sec);
setState(0);
// playPointHour(24);
}
else if(source == cButton)
{
setState(getState()+1);
if(getState() == 4)
setState(0);
}
else if(source == bButton )
{
switch(state)
{
case 1:
beepSec++;
if(beepSec >= 60 )
beepSec %= 60;
break;
case 2:
beepMin++;
if(beepMin >= 60 )
beepMin %= 60;
break;
case 3:
beepHour++;
if(beepHour >= 24 )
beepHour %= 24;
break;
default :break;
}
}
}
public void start()
{
font = new Font("Times New Roman",Font.BOLD,100);
//开始进程
if (clock==null)
{ //如果进程为空值
clock=new Thread(this); //实例化进程
clock.start(); //开始进程
}
}
public void run()
{
while (clock!=null)
{
updataTime();
repaint(); //调用paint方法重绘界面
if(min==0 &&sec == 0)
playPointHour(hour);
if(hour == beepHour && min == beepMin && sec == beepSec )
playMusic(1);
try{
Thread.sleep(200); //线程暂停一秒(1000毫秒)
repaint();
Thread.sleep(200);
repaint();
Thread.sleep(200);
repaint();
Thread.sleep(200);
repaint();
Thread.sleep(200);
repaint();
}
catch (InterruptedException ex){
ex.printStackTrace(); //输出出错信息
}
}
}
public void paintComponent(Graphics g)
{
String timeInfo = "";
if(state == 0)
{
if (hour<=9)
{
timeInfo+="0"+hour+":"; //格式化输出
}
else
{
timeInfo+=hour+":";
}
if (min<=9)
timeInfo+="0"+min+":";
else
timeInfo+=min+":";
if (sec<=9)
timeInfo+="0"+sec;
else
timeInfo+=sec;
}
else
{
if (beepHour<=9)
{
timeInfo+="0"+beepHour+":"; //格式化输出
}
else
{
timeInfo+=beepHour+":";
}
if (beepMin<=9)
timeInfo+="0"+beepMin+":";
else
timeInfo+=beepMin+":";
if (beepSec<=9)
timeInfo+="0"+beepSec;
else
timeInfo+=beepSec;
}
super.paintComponent(g);
g.setFont(font);
Dimension dim=getSize(); //得到窗口尺寸
g.fillRect(0,0,dim.width,dim.height); //填充背景色为白色
g.setColor(Color.orange);
g.drawString(timeInfo, 10,100);
}
public void updataTime()
{
Calendar now=new GregorianCalendar(); //实例化日历对象
hour =now.get(Calendar.HOUR_OF_DAY);
min =now.get(Calendar.MINUTE);
sec =now.get(Calendar.SECOND);
}
}
通过调用系统时钟,所以就没有校准键的设置。
闹钟那个也没怎么改进,只用了一首闹钟。
希望高手们多多指教!!!如果有幸有谁看不懂,也可以评论附上问题即可