helpui.java

来自「俄罗斯方块。适用于初学者学习。想念很好」· Java 代码 · 共 130 行

JAVA
130
字号
package xn.tetris;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
/**
 * 控制界面中操作方法按钮弹出的界面
 * */
public class HelpUI {

	private Shell shell;
	private Display display;
	private Label up;
	private Label down;
	private Label left;
	private Label right;
	private Label blank;
	private Label pause;
	private Label resume;
	private Label notice;
	private Button btOk;
	
	public HelpUI(Display display){
		this.display = display;
		init();
	}
	//初始化
	private void init(){
		shell = new Shell(display, SWT.MOD2 | SWT.SHELL_TRIM & ~SWT.RESIZE &
				~SWT.MAX & ~SWT.MIN & ~SWT.CLOSE);
		shell.setText("操作方法");
		shell.setMinimumSize(300, 350);
		int xCenter = shell.getSize().x / 2;
		int yCenter = shell.getSize().y / 2;

		shell.setLocation(xCenter, yCenter);
		GridLayout gdLayout = new GridLayout(1, true);
		gdLayout.marginHeight = 20;
		gdLayout.marginWidth = 80;
		shell.setLayout(gdLayout);
		
		up = new Label(shell, SWT.NONE);
		up.setText("↑ :    旋转变形");
		GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
		up.setLayoutData(gd);
		
		down = new Label(shell, SWT.NONE);
		down.setText("↓ :    向下移动");
		gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
		gd.verticalIndent = 15;
		down.setLayoutData(gd);
		
		left = new Label(shell, SWT.NONE);
		left.setText("← :    向左移动");
		gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
		gd.verticalIndent = 15;
		left.setLayoutData(gd);
		
		right = new Label(shell, SWT.NONE);
		right.setText("→ :    向右移动");
		gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
		gd.verticalIndent = 15;
		right.setLayoutData(gd);
		
		blank = new Label(shell, SWT.NONE);
		blank.setText("空格 :  直降到底");
		gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
		gd.verticalIndent = 15;
		blank.setLayoutData(gd);
		
		pause = new Label(shell, SWT.NONE);
		pause.setText("p :     暂停游戏");
		gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
		gd.verticalIndent = 15;
		pause.setLayoutData(gd);
		
		resume = new Label(shell, SWT.NONE);
		resume.setText("r :     继续游戏");
		gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
		gd.verticalIndent = 15;
		resume.setLayoutData(gd);
		
		notice = new Label(shell, SWT.NONE);
		notice.setText("按钮 :  开始游戏,\n\n        暂停游戏,\n" +
				"\n        继续游戏,\n" +
				"\n        重新开始游戏");
		gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
		gd.verticalIndent = 15;
		notice.setLayoutData(gd);
		
		btOk = new Button(shell, SWT.PUSH);
		btOk.setText("  确定  ");
		gd = new GridData(GridData.HORIZONTAL_ALIGN_CENTER);
		gd.verticalIndent = 30;
		btOk.setLayoutData(gd);
		addListener();
		shell.pack();
		shell.open();
		retainShell();
	}
	
	private void retainShell(){
		
		while(!shell.isDisposed()){
			if(!display.readAndDispatch()){
				display.sleep();
				}
		}
		display.dispose();
	}
	//添加监听器
	private void addListener(){
		btOk.addSelectionListener(new btOkAdapter());
	}
	//确定按钮事件处理
	class btOkAdapter extends SelectionAdapter{
		public void widgetSelected(SelectionEvent arg0) {
			// TODO Auto-generated method stub
				shell.setVisible(false);

		}
	}
}

⌨️ 快捷键说明

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