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秒            
		}
	}
}

總結:

  • 本來想做一個功能很完善的東西,可以隨意改變隊伍名稱標題、多選顏色、自定義按鍵位置等等,但是做了一個改名稱的功能之後就停下了,如果接着做下去有點費時間而且感覺進步不大基本都在做重複的事情,所以就放棄,如果以後有機會會把它完善的
    在這裏插入圖片描述

  • 在做這個東西之前我還沒有開始學習圖形化界面,但接到這個任務後也給了我動力去學,在學了兩天基本操作後花了兩個晚上做出來這個效果,所以還是要多逼自己一把

希望我的東西能對你們有啓發,如果有想要借鑑的我很歡迎,但請私信和我說一聲

發佈了22 篇原創文章 · 獲贊 58 · 訪問量 7287
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章