首页 » 技术分享 » 计分器

计分器

 

代码:

public class Sample1 {
	private static Composite container1;
	private static List<String> teamMarks = new ArrayList<String>();
	private static List<CLabel> listTeam = new ArrayList<CLabel>();
	private static List<CLabel> listOrder = new ArrayList<CLabel>();
	private static Timer timer;
	private static CLabel CLabelTimer;

	public static void main(String[] args) {
		Display display = new Display();
		Shell shell = new Shell(display);
		shell.setText("计分器");
		createArea(shell);
		shell.pack();
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}

	private static void createArea(Shell parent) {

		parent.setBackground(new Color(null,173, 223, 237));
		parent.setBackgroundMode(SWT.INHERIT_DEFAULT);
		parent.setLayout(new GridLayout());

		Composite container = new Composite(parent, SWT.NONE);
		container.setLayout(new GridLayout(2, false));
		container.setLayoutData(new GridData(GridData.FILL));

		CLabel cLabel = new CLabel(container, SWT.CENTER | SWT.BORDER);
		cLabel.setText("研发部知识竞赛计分表");
		GridData gridData = new GridData(SWT.CENTER | GridData.FILL_HORIZONTAL);
		gridData.horizontalSpan = 2;
		cLabel.setLayoutData(gridData);
		cLabel.setFont(new Font(null,"新宋体",32,SWT.BOLD));

		container1 = new Composite(container, SWT.BORDER);
		container1.setLayout(new GridLayout(1, true));
		gridData = new GridData(SWT.CENTER | GridData.FILL_HORIZONTAL);
		gridData.verticalSpan = 2;
		container1.setLayoutData(gridData);
		container1.setLayoutData(new GridData(GridData.FILL_BOTH));
		{
			Composite container11 = new Composite(container1, SWT.NONE);
			container11.setLayout(new GridLayout(3, true));
			container11.setLayoutData(new GridData(GridData.FILL_BOTH));

			CLabel CLabel1 = new CLabel(container11, SWT.CENTER | SWT.NONE);
			CLabel1.setText("参赛队");
			CLabel1.setLayoutData(new GridData(SWT.CENTER | GridData.FILL_BOTH));
			CLabel1.setFont(new Font(null,"新宋体",30,SWT.NONE));

			CLabel CLabel11 = new CLabel(container11, SWT.CENTER | SWT.NONE);
			CLabel11.setText("比赛得分");
			CLabel11.setLayoutData(new GridData(SWT.CENTER | GridData.FILL_BOTH));
			CLabel11.setFont(new Font(null,"新宋体",30,SWT.NONE));

			CLabel CLabel2 = new CLabel(container11, SWT.CENTER | SWT.NONE);
			CLabel2.setText("名次");
			CLabel2.setLayoutData(new GridData(SWT.CENTER | GridData.FILL_BOTH));
			CLabel2.setFont(new Font(null,"新宋体",30,SWT.NONE));

			for(int i=0; i<7; i++) {
				addTeam(container1,i);
			}
		}

		Composite container2 = new Composite(container, SWT.BORDER);
		container2.setLayout(new GridLayout(1, true));
		gridData = new GridData(SWT.CENTER | GridData.FILL_VERTICAL);
		gridData.verticalSpan = 1;
		container2.setLayoutData(gridData);
		{
			CLabel CLabel1 = new CLabel(container2, SWT.CENTER | SWT.NONE);
			CLabel1.setText("倒计时");
			CLabel1.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

			CLabelTimer = new CLabel(container2, SWT.CENTER | SWT.BORDER);
			CLabelTimer.setFont(new Font(null,"新宋体",60,SWT.NONE));
			CLabelTimer.setText("0");
			CLabelTimer.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

			final Text text = new Text(container2, SWT.CENTER | SWT.BORDER);
			text.setText("10");
			text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
			text.addVerifyListener(new VerifyListener() {

				public void verifyText(VerifyEvent e) {
					e.doit = e.text.length() == 0
					|| Character.isDigit(e.text.charAt(0));
				}

			});


			Composite container21 = new Composite(container2, SWT.BORDER);
			container21.setLayout(new GridLayout(2, true));
			container21.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

			Button button1 = new Button(container21, SWT.CENTER | SWT.NONE);
			button1.setText("开始");
			button1.setFont(new Font(null,"新宋体",20,SWT.NONE));
			button1.setLayoutData(new GridData(SWT.CENTER));
			button1.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
					CLabelTimer.setText(text.getText());

					if(timer != null) {
						timer.cancel();
					}

					timer = new Timer();
					TimerTask task = new TimerTask(){
						public void run() {
							int tmp = Integer.parseInt(CLabelTimer.getText());
							if(tmp > 0) {
								tmp = tmp - 1;
							}
							final int tmptmp = tmp;
							Display.getDefault().syncExec(new Runnable() {
								public void run() {
									CLabelTimer.setText(tmptmp + "");
								}
							});
							if(tmptmp == 0) {
								timer.cancel();
//								Toolkit toolkit = Toolkit.getDefaultToolkit(); 
//								toolkit.beep();//发出声音
								try {
									FileInputStream fileau = new FileInputStream(new File("src/sound.wav"));
//									FileInputStream("sloop.au");
									AudioStream as=new AudioStream(fileau);
									AudioPlayer.player.start(as);
								}
								catch (Exception e) {}

								Display.getDefault().syncExec(new Runnable() {
									public void run() {
										MessageBox messageBox = new MessageBox(Display.getCurrent().getShells()[0], SWT.YES|SWT.ICON_INFORMATION);
										messageBox.setMessage("时间到!");
										messageBox.setText("提示");
										messageBox.open();
									}
								});
							}
						}
					};

					timer.schedule(task, 1000, 1000);
				}
			});

			Button button2 = new Button(container21, SWT.CENTER | SWT.NONE);
			button2.setText("清零");
			button2.setFont(new Font(null,"新宋体",20,SWT.NONE));
			button2.setLayoutData(new GridData(SWT.RIGHT));
			button2.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
					timer.cancel();
					Display.getDefault().syncExec(new Runnable() {
						public void run() {
							CLabelTimer.setText("0");
						}
					});
				}
			});

		}

		Composite xxxx = new Composite(container2, SWT.NONE);
		xxxx.setLayout(new GridLayout(2, false));
		gridData = new GridData(SWT.CENTER | GridData.FILL_VERTICAL);
		gridData.verticalSpan = 1;
		gridData.verticalAlignment = SWT.BOTTOM;
		xxxx.setLayoutData(gridData);

		Composite container22 = new Composite(container2, SWT.BORDER);
		container22.setLayout(new GridLayout(2, false));
		gridData = new GridData(SWT.CENTER | GridData.FILL_HORIZONTAL);
		gridData.verticalSpan = 1;
		gridData.verticalAlignment = SWT.BOTTOM;
		container22.setLayoutData(gridData);

		{
			CLabel CLabel2 = new CLabel(container22, SWT.CENTER | SWT.NONE);
			CLabel2.setText("队号:");

			final Text text1 = new Text(container22, SWT.CENTER | SWT.BORDER);
			text1.setText("1");
			text1.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
			text1.addVerifyListener(new VerifyListener() {
				public void verifyText(VerifyEvent e) {
					e.doit = e.text.length() == 0
					|| Character.isDigit(e.text.charAt(0));
				}
			});

			CLabel CLabel3 = new CLabel(container22, SWT.CENTER | SWT.NONE);
			CLabel3.setText("队名:");

			final Text text2 = new Text(container22, SWT.BORDER);
			text2.setText("");
			text2.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));


			Button buttonxx = new Button(container22, SWT.CENTER | SWT.NONE);
			buttonxx.setText("改名");
			buttonxx.setVisible(false);
			buttonxx.setLayoutData(new GridData(SWT.CENTER));


			Button button3 = new Button(container22, SWT.CENTER | SWT.NONE);
			button3.setText("改名");
			gridData = new GridData(SWT.CENTER | GridData.FILL_HORIZONTAL);
			gridData.verticalSpan = 1;
			gridData.verticalAlignment = SWT.RIGHT;
			button3.setLayoutData(gridData);
			button3.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
					String s1 = text1.getText();
					String s2 = text2.getText();
					if(s1.length()>0 && s2.length()>0) {
						int i = Integer.parseInt(s1);
						if(i>0 && i<8) {
							listTeam.get(i-1).setText(s2);
						}
					}
				}
			});
		}
	}

	private static void addTeam(Composite container1, final int num) {
		teamMarks.add(num, new String("100"));
		final Button button1, button2, button3;

		Composite container11 = new Composite(container1, SWT.NONE);
		container11.setLayout(new GridLayout(3, true));
		container11.setLayoutData(new GridData(GridData.FILL_BOTH));

		CLabel CLabel1 = new CLabel(container11, SWT.CENTER | SWT.NONE);
		CLabel1.setText("第 "+ (num+1) +" 队");
		CLabel1.setLayoutData(new GridData(SWT.CENTER | GridData.FILL_BOTH));
		CLabel1.setFont(new Font(null,"新宋体",30,SWT.NONE));
		listTeam.add(CLabel1);

		Composite container111 = new Composite(container11, SWT.NONE);
		container111.setLayout(new GridLayout(3, true));
		container111.setLayoutData(new GridData(SWT.CENTER | GridData.FILL_BOTH));

		button1 = new Button(container111, SWT.CENTER | SWT.NONE);
		button1.setText("1");
		button1.setFont(new Font(null,"新宋体",30,SWT.NONE));
		button1.setLayoutData(new GridData(SWT.CENTER | GridData.FILL_BOTH));

		button2 = new Button(container111, SWT.CENTER | SWT.NONE);
		button2.setText("0");
		button2.setFont(new Font(null,"新宋体",30,SWT.NONE));
		button2.setLayoutData(new GridData(SWT.CENTER | GridData.FILL_BOTH));
		button2.addMouseListener(new MouseAdapter() {
			public void mouseDown(MouseEvent e) {
				int tmp = Integer.parseInt(teamMarks.get(num));
				if(e.button == 1) {
					if(tmp < 990) {
						tmp = tmp + 10;
					}
				} else {
					if(tmp >= 10) {
						tmp = tmp - 10;
					}
				}
				teamMarks.set(num, tmp + "");

				char[] charTmp = (tmp + "").toCharArray();
				if(tmp >= 100) {
					button1.setText(charTmp[0] + "");
					button2.setText(charTmp[1] + "");
				} else if (tmp >= 10) {
					button1.setText("0");
					button2.setText(charTmp[0] + "");
				} else {
					button1.setText("0");
					button2.setText("0");
				}


				refreshOrder();
			}
		});

		button3 = new Button(container111, SWT.CENTER | SWT.NONE);
		button3.setText("0");
		button3.setFont(new Font(null,"新宋体",30,SWT.NONE));
		button3.setLayoutData(new GridData(SWT.CENTER | GridData.FILL_BOTH));
		button3.addMouseListener(new MouseAdapter() {
			public void mouseDown(MouseEvent e) {
				int tmp = Integer.parseInt(teamMarks.get(num));
				if(e.button == 1) {
					if(tmp < 995) {
						tmp = tmp + 5;
					}
				} else {
					if(tmp > 0) {
						tmp = tmp - 5;
					}
				}
				teamMarks.set(num, tmp + "");

				char[] charTmp = (tmp + "").toCharArray();
				if(tmp >= 100) {
					button1.setText(charTmp[0] + "");
					button2.setText(charTmp[1] + "");
					button3.setText(charTmp[2] + "");
				} else if (tmp >= 10) {
					button1.setText("0");
					button2.setText(charTmp[0] + "");
					button3.setText(charTmp[1] + "");
				} else {
					button1.setText("0");
					button2.setText("0");
					button3.setText(charTmp[0] + "");
				}

				refreshOrder();
			}


		});

		CLabel CLabel2 = new CLabel(container11, SWT.CENTER | SWT.NONE);
		CLabel2.setText("1");
		CLabel2.setLayoutData(new GridData(SWT.CENTER | GridData.FILL_BOTH));
		CLabel2.setFont(new Font(null,"新宋体",30,SWT.NONE));
		listOrder.add(CLabel2);
	}


	private static void refreshOrder() {

		int[] r = new int[7];
		for(int i=0; i<7; i++) {
			r[i] = 1;
		}

		//逐对比较所有元素
		for ( int i = 0; i < 7; i++) {
			for ( int j = i+1; j < 7; j++) {
				int frs = Integer.parseInt(teamMarks.get(i));
				int sec = Integer.parseInt(teamMarks.get(j));
				if (frs < sec) {
					r[i]++;
				}else if (frs > sec) {
					r[j]++;
				}
			}
		}

		for(int i=0; i<7; i++) {
			listOrder.get(i).setText(r[i] + "");

		}

	}
}

 

图示:


 
实现功能:

1,可以修改队名。

2,有倒计时。

3,倒计时到了,显示对话框和声音。

4,分数分百十个,鼠标点击百没用、十加一、个加5,并且依照总分自动排名次。

可以补充的功能:

1,所有的标题文字来自xml,这样便于多语言的支持。

2,参加队的数目可以变。

3,整体布局自适应窗口的变化。

4,设置可以用sash,可以支持隐藏。

。。。
 

转载自原文链接, 如需删除请联系管理员。

原文链接:计分器,转载请注明来源!

0