java swing闹钟小项目

很简单的一个闹钟小项目
样式:
java swing闹钟小项目_第1张图片
功能:
按键介绍:
选择星期 输入24小时制时间 选择铃声 点击确定即锁定选择 输入 订闹钟(星期默认星期一 铃声默认情景剧 安利下孟美岐版情景剧)
点击重置可以重置选择与输入
铃声响起时点击关闭闹钟可关闭铃声
点击上传音乐 弹出上传界面 选择文件(暂只限mp3格式)进行上传
各个功能实现原理:
1.动态时间显示 用线程控制 每隔1秒获取一次当前时间
2.星期与日期显示 直接获取当前星期和日期
3.选择使用复选框
4.确定时输入与选择设置为不可编辑或不可用状态
5.上传音乐:本来是假上传 直接得到选择文件的路径 直接播放路径音乐(这种方法更简单点 直接在播放音乐时加判断即可) 但是还是改成了将本地音乐读写到本工程下的Sounds文件夹里 用到IO流读写
因为没用数据库 但想永久改变复选框的选项就每次程序运行时都会读取Sounds内的mp3文件并将内容加到复选框中
6.定时响铃功能:使用字符串比较方法 使用线程监视控制时间 一旦输入与当前时间一样就响铃 (不过铃声可能会迟几秒播放)

需要优化:
1.整个程序结构还是有点乱 几个窗体 线程任务各种参数传来传去 有点乱 还需重构
2.界面优化
3.速度效率优化
4.文件传输与上传 没加是否传输完成的判断 只是简单延迟

这个小项目把之前学的一些知识练了下 但是还是发现很多不足 很多知识未掌握 尤其是线程与参数传递这块儿
还有一些功能实现思路都是网上搜的 写的时候直接就写了也没怎么想大致框架 写到哪在哪 所以结构乱 以后写前要划类图 小项目也要正规化 还需继续学习 继续锻炼

部分代码:
主窗体:


```java
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;

import javax.sound.midi.VoiceStatus;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.omg.CORBA.PUBLIC_MEMBER;

import com.m123.Task.JudgeTask;
import com.m123.Task.TimeTask;
import com.m123.util.AudioPlayer;

import javazoom.jl.player.advanced.PlaybackEvent;


public class ClockFrame extends JFrame{
	JudgeTask task;
	private Font defaultFont = new Font("微软雅黑", 1, 16);
	private JPanel contentPanel=null;
	private JLabel lblTime_now=new JLabel();
	private JLabel lblTime_date=new JLabel();
	private JLabel lblTime_week=new JLabel();
	private JButton btnUP=new JButton("上传音乐");//上传音乐
	private JTextField txtInHour=new JTextField();//输入小时
	private JTextField txtInMin=new JTextField();//输入分
	private JButton btnConfirm=new JButton("确定");//确定选择
	private JLabel lblHour=new JLabel("时");
	private JLabel lblMin=new JLabel("分");
	private JButton btnReset =new JButton("重置");
	private JButton btnClose=new JButton("关闭闹钟");
	private static String SelectW;
	private static String SelectM;
	private static String InputH;
	private static String InputM;
	private JOptionPane message=new JOptionPane();
	private static String Input;
	public String newLblNow;
	AudioPlayer audioPlayer;
	private int flag=0;
	public  String finalPath;
	List Mlist=new ArrayList<>();
	File file=new File(".\\Sounds");
	
	//选择铃声
	private JComboBox cmbSelctM=new JComboBox<>(new String[] {"情景剧.mp3"}
	);
	//选择定时 自定义现不实现
	private JComboBox cmbSelectWeek=new JComboBox(new String[] {
			"星期一","星期二","星期三","星期四","星期五","星期六","星期日"
	});
	
	public ClockFrame() {
		setTitle("闹钟----By 123");
		setSize(1000,500);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		initComponents();
		initEvents();
		setLocationRelativeTo(null);
		
	}
	/*
	 * 没有数据库 所有就每次扫描Sounds文件夹获取音乐文件
	 * 并添加进复选框中
	 */
	public  void setJCom() {
		String MusicName[]= file.list();
		for(int i=0;i RcmbW(){
		return cmbSelectWeek;
	}
	public JComboBox RcmbM(){
		return cmbSelctM;
	}
	public String getSelectW() {
		//为组合框控件添加选择事件
			cmbSelectWeek.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				 SelectW=(String) cmbSelectWeek.getSelectedItem();
					}
				}); 
		return SelectW;
	}
	public String getSelectM() {
		
		//为组合框控件添加选择事件
		cmbSelctM.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				 SelectM=(String) cmbSelctM.getSelectedItem();
					}
				}); 
		return SelectM;
	}
	public JButton getbtnC() {
		return btnClose;
	}
	public JComboBox getCmbM() {
		return cmbSelctM;
	}
	
	private void initEvents() {
		getSelectW();
		getSelectM();
		//点击确定则订一个闹钟 各个选择输入框不可再改变
		btnConfirm.addActionListener(e->{
				getSelectW();
			getSelectM();
			InputH=txtInHour.getText();
			txtInHour.setEditable(false);
			InputM=txtInMin.getText();
			txtInMin.setEditable(false);
			cmbSelectWeek.setEnabled(false);
			cmbSelctM.setEnabled(false);
			 Input=InputH+":"+InputM+":00";
			//System.out.println(Input);
			Judge();
		});
		
		//重置
		btnReset.addActionListener(e->{
			txtInHour.setEditable(true);
			txtInMin.setEditable(true);
			cmbSelectWeek.setEnabled(true);
			cmbSelctM.setEnabled(true);
			Judge();
		});
		
		//上传 弹出上传面板
		btnUP.addActionListener(e->{
			UPloadBox uPloadBox=new UPloadBox(this);
			uPloadBox.setVisible(true);
			
		});
	}
	
	public String getInput() {
		return Input;
	}
	
	public void Judge() {
		 Timer timer=new Timer();
		 JudgeTask task=new JudgeTask(this);
		 timer.schedule(task, 1000,1000);
}
	
	public JLabel lbl() {
		return lblTime_now;
	}
	
	public JLabel lblW() {
		return lblTime_week;
	}
	
	public JLabel lblD() {
		return lblTime_date;
	}

	public void setlblW() {
		Date week=new Date(System.currentTimeMillis());
		 SimpleDateFormat dateFm=new SimpleDateFormat("EEEE");
		String StrWeek=dateFm.format(week);
		lblTime_week.setText(StrWeek);
	}
	
	public void setlblD() {
		Date date=new Date(System.currentTimeMillis());
		 SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
		String StrDate=dateFormat.format(date);
		lblTime_date.setText(StrDate);
	}

	public void setlblNow() {
		 Timer timer=new Timer();
		 TimeTask task=new TimeTask(this);
			timer.schedule(task,1000,1000);
	}
	
	
	private void initComponents() {
		setJCom();
		contentPanel=(JPanel)getContentPane();
		contentPanel.setLayout(null);
		lblTime_now.setBounds(250,100,600,200);
		lblTime_now.setFont(new Font("微软雅黑", Font.BOLD, 110));
		setlblNow();
		setlblW();
		setlblD();
		
		lblTime_week.setBounds(190,130,50,50);
		lblTime_week.setFont(defaultFont);
		lblTime_date.setBounds(710,130,100,50);
		lblTime_date.setFont(defaultFont);
		cmbSelectWeek.setBounds(80,350,150,30);
		lblHour.setBounds(280,350,50,30);
		txtInHour.setBounds(300,350,50,30);
		lblMin.setBounds(360,350,50,30);
		txtInMin.setBounds(380,350,50,30);
		btnConfirm.setBounds(460,350,70,30);
		btnReset.setBounds(560,350,70,30);
		cmbSelctM.setBounds(660,350,150,30);
		btnUP.setBounds(850,350,100,30);
		btnClose.setBounds(400,400, 100, 30);
		contentPanel.add(lblTime_now);
		contentPanel.add(lblTime_week);
		contentPanel.add(lblTime_date);
		contentPanel.add(cmbSelectWeek);
		contentPanel.add(lblHour);
		contentPanel.add(txtInHour);
		contentPanel.add(lblMin);
		contentPanel.add(txtInMin);
		contentPanel.add(btnConfirm);
		contentPanel.add(btnReset);
		contentPanel.add(cmbSelctM);
		contentPanel.add(btnUP);
		contentPanel.add(btnClose);
	}
}

上传界面:

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.filechooser.FileNameExtensionFilter;

import org.omg.CORBA.PUBLIC_MEMBER;

/*
 * 上传音乐窗体 
 *
 */
public class UPloadBox extends JFrame{
	private JLabel lblSelectMus=new JLabel("选择上传音乐");
	public JTextField textField=new JTextField(100);
	private JButton btnSelect=new JButton("选择");
	private JButton btnUpLoad=new JButton("上传");
	private JPanel contentPane=null;
	private JFileChooser jfChooser=null;
	private JPanel fcJPanel=new JPanel();
	private Font defaultFont=new Font("微软雅黑", 16, 18);
	public static String MusicPath="";//获得的选择音乐路径
	public static  String finalPath;
	JButton btnUp;
	ClockFrame clockFrame;
	public UPloadBox(ClockFrame clockFrame) {
		setTitle("上传音乐");
		setSize(800,350);
		//DISPOSE_ON_CLOSE 保证关闭一个窗体不会使所有窗体关闭
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		initComponents();
		initEvents();
		setLocationRelativeTo(null);
		this.clockFrame=clockFrame;
		
	}
	//显示文件选择器 还需进一步进行文件筛选
	public String showFileChooser() {
		jfChooser=new JFileChooser();
		jfChooser.setFileSelectionMode(jfChooser.OPEN_DIALOG);
		//设置文件过滤
		FileNameExtensionFilter filter=new FileNameExtensionFilter("mp3","mp3");
		jfChooser.setFileFilter(filter);
		jfChooser.showOpenDialog(null);
		String path=jfChooser.getSelectedFile().getAbsolutePath();
		return path;
	}
	private void initEvents() {
		//选择按钮 打开文件选择器
		btnSelect.addActionListener(e->{
		MusicPath=showFileChooser();
		textField.setFont(defaultFont);
			textField.setText(MusicPath);
		});
		
		//上传按钮  
		btnUpLoad.addActionListener(e->{
			/*
			 * 1.获得文件路径
			 * 2.读取本地文件
			 * 3.将本地文件写进此工程中
			 * 4.这里还需要改变铃声选择的复选框
			 */
		
			finalPath=MusicPath;
			finalPath=finalPath.substring(finalPath.lastIndexOf("\\")+1);
			
			//读写文件 给复选框添加mp3文件
			clockFrame.getCmbM().insertItemAt(finalPath, 0);
			try {
				DealFile();
			} catch (IOException e2) {
				// TODO Auto-generated catch block
				e2.printStackTrace();
			}
		
			
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			JOptionPane jOptionPane=new JOptionPane();
			jOptionPane.showMessageDialog(this,"上传成功");
			
		
			//销毁当前窗体
			this.dispose();
		});
		
	}
	//处理选择上传文件 上传到本工程的Sounds文件中
	public void DealFile() throws IOException {
		FileInputStream inputStream = null;
		BufferedInputStream bufferedInputStream = null;
		FileOutputStream outputStream = new FileOutputStream(
				".\\Sounds\\" + finalPath);
		BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
		byte[] bytes = null;
		try {
			inputStream = new FileInputStream(MusicPath);
			bufferedInputStream = new BufferedInputStream(inputStream);
			bytes = new byte[bufferedInputStream.available()];
			int blocks = bytes.length / (2097152); // 文件块数量
			// 最后一块有可能不够一块
			if (bytes.length % 2097152 != 0) {
				blocks++;
			}
			for (int i = 0; i < blocks; i++) {
				if (i == blocks - 1) {
					// 最后一块需要特殊处理 因为不够一块的内容
					bufferedInputStream.read(bytes, i * 2097152, bytes.length % 2097152);
				} else {
					bufferedInputStream.read(bytes, i * 2097152, 2097152);
				}
			}
		} catch (IOException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		} finally {
			try {
				bufferedInputStream.close();
				inputStream.close();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		bufferedOutputStream.write(bytes);
	}
	public static String FinalPath() {
		
		//System.out.println(finalPath);
		return finalPath;
	}
	private void initComponents() {
	contentPane=(JPanel)getContentPane();
		contentPane.setLayout(null);
		lblSelectMus.setBounds(20, 80, 100, 50);
		textField.setBounds(110,80,500,50);
		btnSelect.setBounds(650,80,100,50);
		btnUpLoad.setBounds(650,200,100,50);
		contentPane.add(fcJPanel);
		contentPane.add(lblSelectMus);
		contentPane.add(textField);
		contentPane.add(btnUpLoad);
		contentPane.add(btnSelect);
	}

}

大概项目就是这样 有问题请指教

你可能感兴趣的:(java)