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

📄 speech.java

📁 用java实现的一个随机抽查学生回答问题的一个软件
💻 JAVA
字号:
package speech;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Checkbox;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Label;
import java.awt.List;
import java.awt.Rectangle;
import java.awt.ScrollPane;
import java.awt.TextField;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.Calendar;
import java.util.Date;
import java.awt.Scrollbar;


public class Speech extends Frame{

	private ScrollPane scrollPane = null;
	private List namelist = null;
	private TextField stuName = null;
	private Checkbox sex = null;
	private Label label = null;
	private Label label1 = null;
	private Button chou = null;
	private Label label2 = null;
	private Button start = null;
	private TextField waittime = null;
	private Label label3 = null;
	
	private Label label4 = null;
	private boolean canBreak = false;
	private long breakTime = 0;
	private Button history = null;
	private Dialog dialog = null;  //  @jve:decl-index=0:visual-constraint="516,31"
	private ScrollPane scrollPane1 = null;
	private List historyList = null;  //  @jve:decl-index=0:visual-constraint="841,342"
	private Scrollbar sudu = null;

	/**
	 * This method initializes 
	 * 
	 */
	public Speech() {
		super();
		initialize();
		initData();
		this.addWindowListener(new FrameListener());
		this.setResizable(false);
	}
	
	private Rectangle getFrameBounds(){
		return this.getBounds();
	}
	
	
	/**
	 * This method initializes start	
	 * 	
	 * @return java.awt.Button	
	 */
	private Button getStart() {
		if (start == null) {
			start = new Button();
			start.setBounds(new Rectangle(245, 352, 108, 23));
			start.setLabel("启动");
			start.addActionListener(new StartRun());
		}
		return start;
	}


	/**
	 * This method initializes waittime	
	 * 	
	 * @return java.awt.TextField	
	 */
	private TextField getWaittime() {
		if (waittime == null) {
			waittime = new TextField("0");
			waittime.setBounds(new Rectangle(300, 104, 61, 23));
		}
		return waittime;
	}


	/**
	 * This method initializes history	
	 * 	
	 * @return java.awt.Button	
	 */
	private Button getHistory() {
		if (history == null) {
			history = new Button();
			history.setBounds(new Rectangle(245, 437, 106, 23));
			history.setLabel("抽奖记录");
			history.addActionListener(new ViewHistory());
		}
		return history;
	}


	/**
	 * This method initializes dialog	
	 * 	
	 * @return java.awt.Dialog	
	 */
	private Dialog getDialog() {
		if (dialog == null) {
			dialog = new Dialog(this);
			dialog.setModal(true);
			dialog.setSize(new Dimension(314, 481));
			dialog.setTitle("抽奖记录");
			dialog.add(getScrollPane1(), BorderLayout.CENTER);
			dialog.addWindowListener(new DialogListener(dialog));
		}
		return dialog;
	}


	/**
	 * This method initializes scrollPane1	
	 * 	
	 * @return java.awt.ScrollPane	
	 */
	private ScrollPane getScrollPane1() {
		if (scrollPane1 == null) {
			scrollPane1 = new ScrollPane();
			scrollPane1.add(getHistoryList());
		}
		return scrollPane1;
	}


	/**
	 * This method initializes historyList	
	 * 	
	 * @return java.awt.List	
	 */
	private List getHistoryList() {
		if (historyList == null) {
			historyList = new List();
		}
		return historyList;
	}


	/**
	 * This method initializes sudu	
	 * 	
	 * @return java.awt.Scrollbar	
	 */
	private Scrollbar getSudu() {
		if (sudu == null) {
			sudu = new Scrollbar();
			sudu.setOrientation(Scrollbar.HORIZONTAL);
			sudu.setBounds(new Rectangle(231, 140, 182, 12));
			sudu.setMinimum(0);
			sudu.setMaximum(110);
			sudu.addAdjustmentListener(new SuduListener());
		}
		return sudu;
	}


	public static void main(String[] args){
		new Speech().setVisible(true);
	}
	
	private void initData(){
		Student[] stus = StudentProduct.productStu();
		for(int i = 0;i < stus.length;i ++){
			
			getNamelist().add(stus[i].toString());
		}
	}
	

	/**
	 * This method initializes this
	 * 
	 */
	private void initialize() {
        label4 = new Label();
        label4.setBounds(new Rectangle(368, 105, 45, 23));
        label4.setText("毫秒");
        label3 = new Label();
        label3.setBounds(new Rectangle(228, 105, 61, 23));
        label3.setText("滚动速度");
        label2 = new Label();
        label2.setBounds(new Rectangle(236, 197, 142, 23));
        label2.setText("恭喜这位同学");
        label1 = new Label();
        label1.setBounds(new Rectangle(238, 287, 45, 23));
        label1.setText("性别");
        label = new Label();
        label.setBounds(new Rectangle(241, 233, 45, 23));
        label.setText("姓名");
        this.setLayout(null);
        this.setSize(new Dimension(466, 527));
        this.setTitle("抽奖系统");
        this.add(getScrollPane(), null);
        this.add(getStuName(), null);
        this.add(getSex(), null);
        this.add(label, null);
        this.add(label1, null);
        this.add(getChou(), null);
        this.add(label2, null);
        this.add(getStart(), null);
        this.add(getWaittime(), null);
        this.add(label3, null);
        this.add(label4, null);
        this.add(getHistory(), null);
        this.add(getSudu(), null);
        
	}

	/**
	 * This method initializes scrollPane	
	 * 	
	 * @return java.awt.ScrollPane	
	 */
	private ScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new ScrollPane();
//			scrollPane.setLayout(new BorderLayout());
			scrollPane.setBounds(new Rectangle(19, 36, 177, 467));
			scrollPane.add(getNamelist());
		}
		return scrollPane;
	}

	/**
	 * This method initializes namelist	
	 * 	
	 * @return java.awt.List	
	 */
	private List getNamelist() {
		if (namelist == null) {
			namelist = new List();
			namelist.setBounds(new Rectangle(202, 212, 209, 446));
			
		}
		return namelist;
	}

	/**
	 * This method initializes stuName	
	 * 	
	 * @return java.awt.TextField	
	 */
	private TextField getStuName() {
		if (stuName == null) {
			stuName = new TextField();
			stuName.setBounds(new Rectangle(297, 230, 125, 26));
		}
		return stuName;
	}

	/**
	 * This method initializes sex	
	 * 	
	 * @return java.awt.Checkbox	
	 */
	private Checkbox getSex() {
		if (sex == null) {
			sex = new Checkbox();
			sex.setBounds(new Rectangle(299, 286, 19, 23));
		}
		return sex;
	}

	/**
	 * This method initializes button	
	 * 	
	 * @return java.awt.Button	
	 */
	private Button getChou() {
		if (chou == null) {
			chou = new Button();
			chou.setBounds(new Rectangle(245, 395, 107, 27));
			chou.setLabel("抽奖");
			chou.addActionListener(new Choujiang());
		}
		return chou;
	}
	
	
	
	private class Choujiang implements ActionListener{

		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			setCanBreak(true);
			String key = getNamelist().getItem(0);
			Student stu = (Student)StudentProduct.stuHash().get(key);
			getStuName().setText(stu.getName());
			getSex().setState(stu.isSex());
			
			
			stu.setSpeechDate(getCurrrentDate());
			SaveAndQuery.getInstance().saveStudentHistory(stu);
		}
		
	}
	private Date getCurrrentDate(){
		return Calendar.getInstance().getTime();
	}

	
	private void waitFor(){
		try {
			Thread.sleep(breakTime);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private class StartRun implements ActionListener{

		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			Thread th = new Thread(){
				public void run(){
					int lastItemIndex = getNamelist().getItemCount() - 1;
					while(true){
							String lastItem = getNamelist().getItem(lastItemIndex);
							getNamelist().remove(lastItemIndex);
							getNamelist().add(lastItem,0);
							waitFor();
							if(isCanBreak()){
								break;
							}
					}
					
					setCanBreak(false);

				}
			};
			th.start();
			
		}
		
	}
	
	
	private class ViewHistory implements ActionListener{

		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			getHistoryList().removeAll();
			Student[] stuInfo = SaveAndQuery.getInstance().queryStudentHistory();
			if(stuInfo != null){
				for(int i = 0;i < stuInfo.length;i ++){
					getHistoryList().add(stuInfo[i].speechInfo());
				}
			}
			
			Dimension ds = Toolkit.getDefaultToolkit().getScreenSize();
			
			int[] frameLoc = getFrameLocationAndSize();
			getDialog().setLocation(frameLoc[2] + frameLoc[0], frameLoc[3]);
			getDialog().setVisible(true);
			
			
		}
		
	}
	
	
	private class SuduListener implements AdjustmentListener{

		public void adjustmentValueChanged(AdjustmentEvent e) {
			// TODO Auto-generated method stub
			getWaittime().setText(e.getValue()+"");
			setBreakTime(e.getValue());
		}
		
	}
	/**
	 * 第0个,宽
	 * 第1个,高
	 * 第2个,横坐标
	 * 第3个,纵坐标
	 * @return
	 */
	private int[] getFrameLocationAndSize(){
		int[] locAndSzie = new int[4];
		locAndSzie[0] = this.getWidth();
		locAndSzie[1] = this.getHeight();
		locAndSzie[2] = this.getX();
		locAndSzie[3] = this.getY();
		
		return locAndSzie;
		
	}

	private boolean isCanBreak() {
		return canBreak;
	}


	private void setCanBreak(boolean canBreak) {
		this.canBreak = canBreak;
	}


	public long getBreakTime() {
		return breakTime;
	}


	public void setBreakTime(long breakTime) {
		this.breakTime = breakTime;
	}

}  //  @jve:decl-index=0:visual-constraint="46,3"

⌨️ 快捷键说明

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