JAVA实现篮球计分计时器

做这个的目的是学校举行的篮球赛决赛要在室内打,需要在大屏幕上投出比分、时间,因为找不到免费的软件用就只好自己写一个勉强能用的,虽然这么说但自己的东西在这种场合上派上用场还是有点开心的
这个东西做起来还是比较简单的,只需要懂基本的图形化界面就能做出来
在这里插入图片描述在这里插入图片描述

思路:

  • 计分计时系统需要的东西:标题、两个队的名字、两个队的比分、比赛剩余的时间、24秒
  • 实现这几个东西:一个记录得分和队名的类,一个记录比赛剩余时间的类、一个记录24秒的类,组合这几个东西的类
  • 得分只需要用KeyListener监听键盘,通过按不同的键进行加分,可设置 +1、+2、+3的情况,为了以防万一多加分,可再设置一个-1的情况
  • 计时只需要一个Timer类的对象每0.1秒或者1秒产生一次ActionEvent,然后将数字不断减少再用一个标签显示出来即可实现计时,在到时间后需要时间保持不变;计时需要实现暂停、继续、重新开始的功能,只需要KeyListener监听键盘实现即可,注意24秒的规则,在时间少于14秒时如果出现需要回表的情况要把剩余时间变为从14秒开始减少,时间大于14秒不用管;为了以防万一忘记停表,可在设置加1秒、加1分钟的情况

完整代码如下:

public class Tool {
	public static void main(String[] args) {
		BasketWindow a = new BasketWindow() ;
	}
}

public class BasketWindow extends JFrame implements KeyListener, ActionListener{		//计分计时窗口
	Score score ;			//得分
	TimeHalf countdown ;	//半场倒计时
	JLabel titil ;			//标题
	Time24 time ;			//24秒
	String titils, team1Name, team2Name ;	//标题、队伍名称
	File titilFile, team1File, team2File ;	//存数据的文件
	JMenuBar menuBar ;		//菜单栏
	JMenu menu ;
	JMenuItem titilName, team1Chage, team2Chage ;
	BasketWindow() {
		menuBar = new JMenuBar() ;
		menu = new JMenu("修改") ;
		titilName = new JMenuItem("标题") ;
		team1Chage = new JMenuItem("队伍1") ;
		team2Chage = new JMenuItem("队伍2") ;
		titilName.addActionListener(this);
		team1Chage.addActionListener(this);
		team2Chage.addActionListener(this);
		menu.add(titilName) ;
		menu.add(team1Chage) ;
		menu.add(team2Chage) ;
		menuBar.add(menu) ;
		setJMenuBar(menuBar) ;
		titilFile = new File("标题.txt") ;
		team1File = new File("队名1.txt") ;
		team2File = new File("队名2.txt") ;
		try { 
			FileReader in1 = new FileReader(titilFile) ; 
			BufferedReader in2 = new BufferedReader(in1) ;
			titils = in2.readLine() ;
			in1 = new FileReader(team1File) ; 
			in2 = new BufferedReader(in1) ;
			team1Name = in2.readLine() ;
			in1 = new FileReader(team2File) ; 
			in2 = new BufferedReader(in1) ;
			team2Name = in2.readLine() ;
			in2.close();
			in1.close();
		} 
		catch (IOException e) {}
		titil=new JLabel(titils) ;
		score=new Score(team1Name, team2Name) ;
		countdown=new TimeHalf() ;
		time=new Time24() ;
		setLayout(null) ;			//设置为null布局,自己控制各个组件的位置
		add(titil) ;
		titil.setBounds(150,10,1970,150) ;
		titil.setFont(new java.awt.Font("24",Font.BOLD,100)) ;
		titil.setForeground(Color.blue) ;
		add(score);
		score.setBounds(0, 150, 1970, 450);
		add(countdown);
		countdown.setBounds(0, 610, 1970, 310);
		add(time);
		time.setBounds(0,900,1970,150);
		addKeyListener(this);
		setFocusable(true);	
		setVisible(true);
		validate();
		setBounds(0,0,1970,1090);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	public void keyTyped(KeyEvent e) {}
	public void keyPressed(KeyEvent e) {}
	public void keyReleased(KeyEvent e) {
		//队伍1计分
		if(e.getKeyCode()==KeyEvent.VK_Q) {	//按Q加1
			score.count1+=1;
			score.score1.setText(""+score.count1);
		}
		else if(e.getKeyCode()==KeyEvent.VK_W) {	//按W加2
			score.count1+=2;
			score.score1.setText(""+score.count1);	
		}
		else if(e.getKeyCode()==KeyEvent.VK_E) {	//按E加3
			score.count1+=3;
			score.score1.setText(""+score.count1);	
		}
		else if(e.getKeyCode()==KeyEvent.VK_R) {		//按R减1
			score.count1-=1;
			score.score1.setText(""+score.count1);	
		}
		//队伍2计分
		else if(e.getKeyCode()==KeyEvent.VK_U) {
			score.count2+=1;
			score.score2.setText(""+score.count2);	
		}
		else if(e.getKeyCode()==KeyEvent.VK_I) {
			score.count2+=2;
			score.score2.setText(""+score.count2);	
		}
		else if(e.getKeyCode()==KeyEvent.VK_O) {
			score.count2+=3;
			score.score2.setText(""+score.count2);	
		}
		else if(e.getKeyCode()==KeyEvent.VK_P) {
			score.count2-=1;
			score.score2.setText(""+score.count2);	
		}
		//半场计时
		else if(e.getKeyCode()==KeyEvent.VK_F1) {	//按F1开始比赛
			countdown.flag = true ;				//控制是否播放提示音
			countdown.min=24;
			countdown.second=0;
			countdown.time.start();
		}
		else if(e.getKeyCode()==KeyEvent.VK_S)	//按S暂停比赛
			countdown.time.stop();
		else if(e.getKeyCode()==KeyEvent.VK_L)	//按L继续比赛
			countdown.time.restart();
		else if(e.getKeyCode()==KeyEvent.VK_F2)	//按F2回一秒
			countdown.second+=1;
		else if(e.getKeyCode()==KeyEvent.VK_F3)	//按F3回一分钟
			countdown.min+=1;
		else if(e.getKeyCode()==KeyEvent.VK_Z) {	//24秒
			time.flag = true ;
			time.second1=23;
			time.second2=9;
			time.time.start();
		}
		else if(e.getKeyCode()==KeyEvent.VK_X) {
			time.time.stop();
			if(time.second1<14) {
				time.second1=13;
				time.second2=9;
			}
		}
		else if(e.getKeyCode()==KeyEvent.VK_C)	
			time.time.restart();
		else if(e.getKeyCode()==KeyEvent.VK_V) {
			time.second1=13;
			time.second2=9;
			time.time.start();
		}
	}
	public void actionPerformed(ActionEvent z) {		//控制菜单栏
		if(z.getSource() == titilName) {
			titils = JOptionPane.showInputDialog(null, "请输入要修改的标题", "修改标题", JOptionPane.INFORMATION_MESSAGE) ;
			if(titils != null) {
				try { 
					FileWriter out1 = new FileWriter(titilFile) ; 
					BufferedWriter out2 = new BufferedWriter(out1) ;
					out2.write(titils);
					out2.flush();
					out2.close();
					out1.close();
					titil.setText(titils);
				} 
				catch (IOException e) {}
			}
		}
		if(z.getSource() == team1Chage) {
			team1Name = JOptionPane.showInputDialog(null, "请输入要修改的名字", "修改队伍1名字", JOptionPane.INFORMATION_MESSAGE) ;
			if(team1Name != null) {
				try { 
					FileWriter out1 = new FileWriter(team1File) ; 
					BufferedWriter out2 = new BufferedWriter(out1) ;
					out2.write(team1Name);
					out2.flush();
					out2.close();
					out1.close();
					score.team1.setText(team1Name);
				} 
				catch (IOException e) {}
			}
		}
		if(z.getSource() == team2Chage) {
			team2Name = JOptionPane.showInputDialog(null, "请输入要修改的名字", "修改队伍2名字", JOptionPane.INFORMATION_MESSAGE) ;
			if(team2Name != null) {
				try { 
					FileWriter out1 = new FileWriter(team2File) ; 
					BufferedWriter out2 = new BufferedWriter(out1) ;
					out2.write(team2Name);
					out2.flush();
					out2.close();
					out1.close();
					score.team2.setText(team2Name);
				} 
				catch (IOException e) {}
			}
		}	
	}
}

public class TimeHalf extends JPanel implements ActionListener	{	//记录半场时间
	JLabel show ;							//显示时间
	Timer time ;							//控制时间
	File sound ;
	URL url ;
	URI uri ;
	AudioClip clip ;
	int min = 24, second = 0 ;
	Boolean flag = true;
	TimeHalf() {
		setLayout(null) ;					//设置为null布局,自己控制各个组件的位置
		time=new Timer(1000,this) ;			//每1秒发生一次事件
		show=new JLabel("20:00") ;
		add(show) ;
		show.setForeground(Color.red) ;		//设置字体颜色
		show.setBounds(650,0, 1970, 330) ;
		show.setFont(new java.awt.Font("24",20,250)) ;	
		sound = new File("篮球提示音.wav") ;
	}
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == time) {			//每过1秒产生一个新事件
			if(second==0 && min!=0) {		//秒到0要重置                           
				second = 59 ;               
				min-- ;						//分要减1
			}
			show.setText(min+":"+second);
			if(second==0 && min==0) {		//时间到就一直显示0:0
				second = 0 ;
				min = 0 ;
				if(flag) {
					try {
						uri = sound.toURI() ;
						url = uri.toURL() ;
						clip = Applet.newAudioClip(url) ;
						clip.play() ;
						flag = false ;
					}
					catch(Exception a) {}
				}		
			}
			else							//每产生一次事件减1秒                                           
				second -= 1 ;
		}
	}
}

public class Score extends JPanel {		//计分
	JLabel team1, team2, score1, score2 ;
	int count1 = 0, count2 = 0 ;
	Score(String team1Name, String team2Name) {
		setLayout(null) ;			//设置为null布局,自己控制各个组件的位置
		team1=new JLabel(team1Name) ;
		team2=new JLabel(team2Name) ;
		score1=new JLabel("0") ;
		score2=new JLabel("0") ;
		add(team1) ;
		team1.setBounds(90,50,985,150) ;
		team1.setFont(new java.awt.Font("24",20,90)) ;
		team1.setForeground(Color.blue) ;
		add(team2) ;
		team2.setBounds(1050,50,985,150) ;
		team2.setFont(new java.awt.Font("24",20,90)) ;
		team2.setForeground(Color.blue) ;
		add(score1) ;
		score1.setBounds(300,210,985,300) ;
		score1.setFont(new java.awt.Font("24",20,270)) ;
		score1.setForeground(Color.blue) ;
		add(score2) ;
		score2.setBounds(1260,210,985,300) ;
		score2.setForeground(Color.blue) ;
		score2.setFont(new java.awt.Font("24",20,270)) ;
	}
}

public class Time24 extends JPanel implements ActionListener{		//记录24秒
	JLabel show ;					//显示24秒
	Timer time ;					//控制时间
	File sound ;
	URL url ;
	URI uri ;
	AudioClip clip ;
	int second1 = 23, second2 = 9 ;		
	Boolean flag ;
	Time24() {
		setLayout(null) ;			//设置为null布局,自己控制各个组件的位置
		time=new Timer(100,this) ;	//每0.1秒发生一次事件
		show=new JLabel("24") ;
		add(show) ;
		show.setBounds(800, 10, 1970,130) ;
		show.setFont(new java.awt.Font("24",50,90)) ;
		sound = new File("篮球提示音.wav") ;
	}
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==time) {	//每过0.1秒产生一个新事件
			if(second2==0 && second1!=0) {		//毫秒到0要重置
				second2=9 ;
				second1-- ;			//秒要减少1
			}
			show.setText(second1+"."+second2) ;                                    
			if(second1 <= 0 && second2 ==0) {			//24秒结束要重置                                                	
				second1 = 0 ;
				second2 = 0 ;
				if(flag) {
					try {
						uri = sound.toURI() ;
						url = uri.toURL() ;
						clip = Applet.newAudioClip(url) ;
						clip.play() ;
						flag = false ;
					}
					catch(Exception a) {}
				}		
			}
			second2-=0.1 ;			//每一次事件要减0.1秒            
		}
	}
}

总结:

  • 本来想做一个功能很完善的东西,可以随意改变队伍名称标题、多选颜色、自定义按键位置等等,但是做了一个改名称的功能之后就停下了,如果接着做下去有点费时间而且感觉进步不大基本都在做重复的事情,所以就放弃,如果以后有机会会把它完善的
    在这里插入图片描述

  • 在做这个东西之前我还没有开始学习图形化界面,但接到这个任务后也给了我动力去学,在学了两天基本操作后花了两个晚上做出来这个效果,所以还是要多逼自己一把

希望我的东西能对你们有启发,如果有想要借鉴的我很欢迎,但请私信和我说一声

发布了21 篇原创文章 · 获赞 51 · 访问量 3201

猜你喜欢

转载自blog.csdn.net/weixin_44689154/article/details/100145301