example8.java
来自「Micro Window Toolkit(MWT)是一个用于开发J2ME用户界面」· Java 代码 · 共 128 行
JAVA
128 行
package example;
/*
* MWT - Micro Window Toolkit
* Copyright (C) 2007 Lucas Domanico - lucazd@gmail.com
*
* Licensed under the terms of the GNU Lesser General Public License:
* http://www.opensource.org/licenses/lgpl-license.php
*
* For further information visit:
* http://j2me-mwt.sourceforge.net/
*/
import java.util.Random;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import mwt.Button;
import mwt.Component;
import mwt.EventListener;
import mwt.Label;
import mwt.Window;
// Custom Window
// Override the behavior of the window
class MyWindow extends Window {
public MyWindow(int x,int y,int width,int height) {
super(x,y,width,height);
}
// Now the focus may be moved with action keys instead of the key numbers
public int getFocusAction(long key) {
switch(Example8.canvas.getGameAction((int)key)) {
case Canvas.FIRE: return FOCUSACTION_FIRE;
case Canvas.LEFT: return FOCUSACTION_PREV;
case Canvas.RIGHT: return FOCUSACTION_NEXT;
default: return FOCUSACTION_NONE;
}
}
// A class can override the paint method instead of changing the skin
protected void paint(Graphics g, Window window) {
paintChilds(g,window); // must call this method
g.setColor(0x000000);
g.drawRoundRect(0,0,getWidth(),getHeight(),8,4);
}
// Now the focus will be moved only when the key is released
protected void dispatchKey(long key) {
if(key >> 32 != 0) return;
switch(getFocusAction((int)key)) {
case FOCUSACTION_PREV: setFocusPrevious(); break;
case FOCUSACTION_NEXT: setFocusNext(); break;
}
}
}
class Canvas8 extends Canvas implements Runnable, EventListener {
Window win = new MyWindow(20,20,100,100); // the main window -reference-
boolean exit; // setted to true to finish the application
static final int ACTION_EXIT = 0; // button's action ids
// notify input
protected void keyPressed(int keyCode) { win.setKeyState(keyCode,Window.KEYSTATE_PRESSED,true); }
protected void keyReleased(int keyCode) { win.setKeyState(keyCode,Window.KEYSTATE_RELEASED,true); }
// event listener implementation
public void processEvent(int eventType, Component sender, Object[] args) {
switch(eventType) {
case EVENT_ACTION: // when a button is pressed an event action is triggered
switch(((Button)sender).getActionType()) {
case ACTION_EXIT: exit = true; return;
}
break;
default: break;
}
}
Canvas8() {
win.add(new Label(4,4,60,20,"Choose"));
win.add(new Button(20,24,60,20,"Exit 1",this,ACTION_EXIT));
win.add(new Button(20,50,60,20,"Exit 2",this,ACTION_EXIT));
win.add(new Button(20,76,60,20,"Exit 3",this,ACTION_EXIT));
win.setFocusFirst();
}
public void run() {
while(!exit) { // main loop
win.repeatKeys(true);
repaint();
serviceRepaints();
try { Thread.sleep(1); }
catch(Exception e) { e.printStackTrace(); }
}
Example8.instance.notifyDestroyed();
}
protected void paint(Graphics g) {
g.setColor(0xFFFFFFFF); // clear graphics
g.fillRect(0,0,getWidth(),getHeight());
Random r = new Random(); // paint the "background"
for(int i=0; i<10 ;i++) {
g.setColor(Math.abs(r.nextInt()));
g.drawRect(Math.abs(r.nextInt())%128,Math.abs(r.nextInt())%128,Math.abs(r.nextInt())%128,Math.abs(r.nextInt())%128);
}
win.paint(g); // and paint the window...
}
}
public class Example8 extends MIDlet {
static MIDlet instance;
static Canvas canvas;
protected void startApp() throws MIDletStateChangeException {
instance = this;
Canvas8 c = new Canvas8();
canvas = c;
Display.getDisplay(this).setCurrent(c);
new Thread(c).start();
}
protected void pauseApp() { }
protected void destroyApp(boolean arg0) throws MIDletStateChangeException { }
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?