⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 answerright.java

📁 抢答器
💻 JAVA
字号:
/*
抢答权显显屏
界面制做人:程希望
类名AnswerRight 

*/
//package    包名没定呢
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import sun.audio.*;
import java.io.*;
public class AnswerRight extends JFrame implements ActionListener{
AudioStream music;	
JTextField f1;
JTextField f2;
JButton b1;
JButton b2;
JButton b3;
JRadioButton r1;
JRadioButton r2;
JRadioButton r3;
Timer tr;
Timer tr1;
ServerMain sm;
boolean b=false;
boolean on=true;
int type=0;// 用来记录事件代表的状态,1 :是正确加分 2:是错误减分 3是犯规罚分
int typ=1;//用来记录事件代表的难易成度1:易 ,2中,3,难
//****************************************************************************
public AnswerRight(String code,String name,ServerMain sm){//构造方法开始 ,等传入参数做为文本中显示的名字
		this.sm=sm;
		try{//******************
				music = new AudioStream(  new FileInputStream("铃.au")   );
				AudioPlayer.player.start(music);
				}catch(Exception e1){
					e1.printStackTrace();	
				} //*********************end trycatch						
	  Container c = getContentPane();
			  c.setLayout(new BorderLayout());
		JPanel p=new JPanel();
				Label l1=new Label("获得答题权的是:");
				l1.setFont( new Font(null,Font.BOLD,40) ); 
				Label l2=new Label("倒计时");
				//f1=new JTextField("60");       //	试验里
				f1=new JTextField(new Integer(sm.getServerInfo().getAnswerTime()).toString());
				f1.setEditable(false);
				f1.setFont( new Font(null,Font.BOLD,30) );
				f1.setBackground(new Color(206,222,190));	
				f1.setForeground(Color.red);
				Label l3=new Label("秒");			
				p.add(l1);		
				p.add(l2);
				p.add(f1);
				p.add(l3);
				p.setBackground(new Color(85,141,142));
		ButtonGroup p2=new ButtonGroup();
				r1=new JRadioButton("难");
				r2=new JRadioButton("中");
				r3=new JRadioButton("易",true);
				p2.add(r1);	
				p2.add(r2);
				p2.add(r3);
				r1.addActionListener(this);
				r2.addActionListener(this);
				r3.addActionListener(this);
		JPanel p1=new JPanel();
				b1=new JButton("答对了");
				b2=new JButton("答错了");
				b3=new JButton("犯规了");	
				b1.setBackground(new Color(123,175,176));
				b1.setForeground(Color.red);
				b2.setBackground(new Color(123,175,176));
				b2.setForeground(Color.red);
				b3.setBackground(new Color(123,175,176));
				b3.setForeground(Color.red);
				b1.addActionListener(this);
				b2.addActionListener(this);
				b3.addActionListener(this);
			  tr=new Timer(1000, this);
			  tr.start();	
 			  tr1=new Timer(2000, this);
			  tr1.start();	
		f2=new JTextField("No."+code+":"+name);//直接用构造方法的行参做实参
		f2.setBackground(new Color(206,222,190));	
		f2.setForeground(Color.red);
		f2.setEditable(false);
		//p1.add(p2);
				p1.add(r1);
				p1.add(r2);
				p1.add(r3);
				p1.add(b1);
				p1.add(b2);
				p1.add(b3);
				p1.setBackground(new Color(85,141,142));
			  f2.setFont( new Font(null,Font.BOLD,120) );     //设置字体大小		
		c.add(p,"North");
		c.add(f2,"Center");
		c.add(p1,"South");
		this.setTitle("抢答时间控制器");
		this.pack();	//自动调window大小
		this.show();   //等同于setvisible(true)
		//this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置win关毕
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				AnswerRight.this.sm.setCanAnswer(true);
				AnswerRight.this.on = false;
				AnswerRight.this.dispose();
				
		}
			});
}//构造方法完工
//*******************************************************************************
public void close(){//关掉本窗口
	on = false;
	this.setVisible(false);
	
	//this.dispose();
}//关掉它的方法
//****************************************************************************
public void actionPerformed(ActionEvent e){//监听方法开始
		 Object temp = e.getSource();
		
		 
					
		if( temp==b1){
		type=1;  
			try{//************************
				music = new AudioStream(  new FileInputStream("喝采.au")   );
				AudioPlayer.player.start(music);
			}catch(Exception e1){
				e1.printStackTrace();	
			} //*********************end tryactch
			finally{
				sm.update(type,typ);
				sm.addQuesNum();
				sm.setCanAnswer(true);
				b=true;
				on=false;
				close();
			}
			System.out.println("答对了给他加分吧");    
		}		//如果正确情况结束										
		else if(temp==b2){
		type=2;				
					try{//****************
				music = new AudioStream(  new FileInputStream("错.au")   );
				AudioPlayer.player.start(music);
			}catch(Exception e1){
				e1.printStackTrace();	
			} 
			finally{
			sm.update(type,typ);
			sm.setCanAnswer(true);
			b=true;
			on=false;
			close();
			}	//*********************end trycatch	
System.out.println("答错了给他扣分吧");   
		} //如果答错情况结束	
		else if(temp==b3)
		{
			type=3;
					
			try
			{//******************
				music = new AudioStream(  new FileInputStream("啊哦.au")   );
				AudioPlayer.player.start(music);
			}catch(Exception e1)
			{
				e1.printStackTrace();	
			} 
			finally
			{
				sm.update(type,typ);
				sm.setCanAnswer(true);
				b=true;
				on=false;
				close();
			}//*********************end trycatch	
			System.out.println("犯规给他罚分吧");   
		}	//如果犯规情况结束	
		else if(temp==tr&&on==true)
		{
			String y=f1.getText();
			int i=Integer.parseInt(y);
			String ss=String.valueOf(--i);
			
			if(i>10)
			{						
				f1.setText(ss);
			}
			else if(i>0)
			{
				f1.setText(ss);
				//type=3; //如果超过时间也算犯规	
				try
				{//******************
					music = new AudioStream(  new FileInputStream("叮.au")   );
					AudioPlayer.player.start(music);
				}catch(Exception e1)
				{
					e1.printStackTrace();	
				} 
				finally
				{
					//sm.update(type);
					//b=true;
					//on=false;
							
				}
			}//*********************end trycatch	
			else if(i == 0)
			{
				f1.setText(ss);
				try
				{//******************
					music = new AudioStream(  new FileInputStream("啊哦.au")   );
					AudioPlayer.player.start(music);
				}catch(Exception e1){
					e1.printStackTrace();	
				} 
			}
			else
			{
				on=false;
				System.out.println("超时了给他加分吧");
			}//else小 结束
			   
						
		}//else if 结束	
		else if(temp==r1)
		{
			typ=3; //难
			try{}catch(Exception e1){}
		}
		else if(temp==r2)
		{
			typ=2;//中
			try{}catch(Exception e1){}
		}
		else if(temp==r3)
		{
			typ=1;//易
			try{}catch(Exception e1){}
		}
		else if(temp==tr1)
		{
			if (b==true)
			{
				
				close();
			}	
		}
						
						
}//监听方法结束
//***********************************************************************************
public static void  main(String args[]){//主函数用来测试
	//ServerMain sm=new ServerMain();
	//AnswerRight t= new AnswerRight("1","程希望",sm);
	//t.close();	//要调用的关毕函数
}//主函数结束
}//Jframe 类结束





⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -