贴个SWT的例子 swing的自己修改吧 package jm.game.swt.game;import jm.game.base.View;import org.eclipse.swt.SWT; import org.eclipse.swt.events.KeyAdapter; import org.eclipse.swt.events.KeyEvent; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell;/** * This code was edited or generated using CloudGarden's Jigloo * SWT/Swing GUI Builder, which is free for non-commercial * use. If Jigloo is being used commercially (ie, by a corporation, * company or business for any purpose whatever) then you * should purchase a license for each developer using Jigloo. * Please visit www.cloudgarden.com for details. * Use of Jigloo implies acceptance of these licensing terms. * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE. */ /** * 格子游戏模板 * * @author spook * @version 1.0.1 * @see jdk 1.4.7 */ public class JMGGame extends org.eclipse.swt.widgets.Composite { private View Hero_; /** * Auto-generated main method to display this * org.eclipse.swt.widgets.Composite inside a new Shell. */ public static void main(String[] args) { showGUI(); } /** * Auto-generated method to display this org.eclipse.swt.widgets.Composite * inside a new Shell. */ public static void showGUI() { Display display = Display.getDefault(); Shell shell = new Shell(display); JMGGame inst = new JMGGame(shell, SWT.NULL); Point size = inst.getSize(); shell.setLayout(null); shell.layout(); if (size.x == 0 && size.y == 0) { inst.pack(); shell.pack(); } else { Rectangle shellBounds = shell.computeTrim(0, 0, size.x, size.y); shell.setSize(shellBounds.width, shellBounds.height); } shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } } public JMGGame(org.eclipse.swt.widgets.Composite parent, int style) { super(parent, style); initGUI(); } private void initGUI() { try { this.setLayout(null); this.setSize(V_SIZE * X_NUMBER, V_SIZE * Y_NUMBER); this.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent evt) { rootKeyPressed(evt); } }); { Hero_ = new View(this, SWT.NONE, V_SIZE, V_SIZE); Hero_.setBounds(V_SIZE * 0, V_SIZE * 0); Hero_.setText("Hero"); } this.layout(); } catch (Exception e) { e.printStackTrace(); } } // //////////////////////////////////////////////////// // 格子游戏用来移动视图的操作 String UP_key = "UP"; String DOWN_key = "DOWN"; String LEFT_key = "LEFT"; String RIGHT_key = "RIGHT"; int V_SIZE = 25; int X_NUMBER = 10; int Y_NUMBER = 10; /** * 移动主控件 */ private void rootKeyPressed(KeyEvent evt) { int x = Hero_.getBounds().x; int y = Hero_.getBounds().y; switch (evt.keyCode) { case 16777217: if (y - V_SIZE < 0) return; y -= V_SIZE; Hero_.setBounds(x, y); Hero_.setText(UP_key); break; case 16777218: if (y + V_SIZE > this.getSize().y) return; y += V_SIZE; Hero_.setBounds(x, y); Hero_.setText(DOWN_key); break; case 16777219: if (x - V_SIZE < 0) return; x -= V_SIZE; Hero_.setBounds(x, y); Hero_.setText(LEFT_key); break; case 16777220: if (x + V_SIZE > this.getSize().x) return; x += V_SIZE; Hero_.setBounds(x, y); Hero_.setText(RIGHT_key); break; default: break; } }}
一辆车使用新得JPANEL
然后加入到applet
applet里面添加移动得键盘事件
swing的自己修改吧
package jm.game.swt.game;import jm.game.base.View;import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
/**
* 格子游戏模板
*
* @author spook
* @version 1.0.1
* @see jdk 1.4.7
*/
public class JMGGame extends org.eclipse.swt.widgets.Composite {
private View Hero_; /**
* Auto-generated main method to display this
* org.eclipse.swt.widgets.Composite inside a new Shell.
*/
public static void main(String[] args) {
showGUI();
} /**
* Auto-generated method to display this org.eclipse.swt.widgets.Composite
* inside a new Shell.
*/
public static void showGUI() {
Display display = Display.getDefault();
Shell shell = new Shell(display);
JMGGame inst = new JMGGame(shell, SWT.NULL);
Point size = inst.getSize();
shell.setLayout(null);
shell.layout();
if (size.x == 0 && size.y == 0) {
inst.pack();
shell.pack();
} else {
Rectangle shellBounds = shell.computeTrim(0, 0, size.x, size.y);
shell.setSize(shellBounds.width, shellBounds.height);
}
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
} public JMGGame(org.eclipse.swt.widgets.Composite parent, int style) {
super(parent, style);
initGUI();
} private void initGUI() {
try {
this.setLayout(null);
this.setSize(V_SIZE * X_NUMBER, V_SIZE * Y_NUMBER);
this.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent evt) {
rootKeyPressed(evt);
}
});
{
Hero_ = new View(this, SWT.NONE, V_SIZE, V_SIZE);
Hero_.setBounds(V_SIZE * 0, V_SIZE * 0);
Hero_.setText("Hero");
}
this.layout();
} catch (Exception e) {
e.printStackTrace();
}
} // ////////////////////////////////////////////////////
// 格子游戏用来移动视图的操作
String UP_key = "UP"; String DOWN_key = "DOWN"; String LEFT_key = "LEFT"; String RIGHT_key = "RIGHT"; int V_SIZE = 25; int X_NUMBER = 10; int Y_NUMBER = 10; /**
* 移动主控件
*/
private void rootKeyPressed(KeyEvent evt) {
int x = Hero_.getBounds().x;
int y = Hero_.getBounds().y;
switch (evt.keyCode) { case 16777217:
if (y - V_SIZE < 0)
return;
y -= V_SIZE;
Hero_.setBounds(x, y);
Hero_.setText(UP_key);
break;
case 16777218:
if (y + V_SIZE > this.getSize().y)
return;
y += V_SIZE;
Hero_.setBounds(x, y);
Hero_.setText(DOWN_key);
break;
case 16777219:
if (x - V_SIZE < 0)
return;
x -= V_SIZE;
Hero_.setBounds(x, y);
Hero_.setText(LEFT_key);
break;
case 16777220:
if (x + V_SIZE > this.getSize().x)
return;
x += V_SIZE;
Hero_.setBounds(x, y);
Hero_.setText(RIGHT_key);
break;
default:
break; } }}