📄 example5.java
字号:
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.Calendar;
import java.util.Date;
import java.util.Random;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import mwt.Button;
import mwt.Component;
import mwt.EventListener;
import mwt.Label;
import mwt.Skin;
import mwt.Window;
// Look and Feel
//
class Canvas5 extends Canvas implements Runnable, EventListener {
Window win = new Window(8,8,100,100); // the main window -reference-
boolean exit; // setted to true to finish the application
static final int ACTION_BACKGROUND = 0; // button's action ids
static final int ACTION_BORDER = 1;
static final int ACTION_EXIT = 2;
final int[] winColors = {0x2033FF,0x000000,0xCCCCCC,0x000000,0x444444}; // colours for the win background
Random r = new Random(); // will be used to change colours randomly
// 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_BACKGROUND: winColors[0] = Math.abs(r.nextInt()); break;
case ACTION_BORDER: winColors[2] = Math.abs(r.nextInt()); break;
case ACTION_EXIT: exit = true; return;
}
win.setSkin(Window.STYLE_DEFAULT, new Skin(winColors));
break;
default: break;
}
}
Canvas5() {
// set white color as default for labels
Label.getDefaultFont(Label.STYLE_DEFAULT).setColor(0xFFFFFF);
// the "menu" label
Label menu = new Label(0,6,win.getWidth(),20,"Menu");
menu.setTextAlign(Component.ALIGN_TOP_CENTER);
win.add(menu);
// display the time
Label time = new Label(0,20,win.getWidth(),20,"") {
protected void paint(Graphics g, Window window) { // override paint method
Calendar now = Calendar.getInstance();
now.setTime(new Date());
setText("Time " + now.get(Calendar.HOUR) + ":" + now.get(Calendar.MINUTE) + ":" + now.get(Calendar.SECOND));
super.paint(g, window);
}
};
time.setTextAlign(Component.ALIGN_TOP_CENTER);
win.add(time);
// change the background button (ImageSkin" defined below)
Button background = new Button(6,50,42,20,"",this,ACTION_BACKGROUND);
win.add(background);
try {
background.setSkin(Button.STYLE_DEFAULT, new ImageSkin(Image.createImage("/color_def.png")));
background.setSkin(Button.STYLE_FOCUSED, new ImageSkin(Image.createImage("/color_foc.png")));
background.setSkin(Button.STYLE_PRESSED, new ImageSkin(Image.createImage("/color_def.png")));
} catch(Exception e) { e.printStackTrace(); }
// this sets the skin and font for the last two buttons
Button.setDefaultSkin(Button.STYLE_DEFAULT, new Skin()); // null skin
Button.setDefaultSkin(Button.STYLE_FOCUSED, new Skin());
Button.setDefaultSkin(Button.STYLE_PRESSED, new Skin());
Button.getDefaultFont(Button.STYLE_DEFAULT).setColor(0xFFFFFF);
Button.getDefaultFont(Button.STYLE_FOCUSED).setColor(0xF8FF66);
win.add(new Button(50,52,40,20,"Border",this,ACTION_BORDER));
win.add(new Button(30,70,30,20,"Exit",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(); }
}
Example5.instance.notifyDestroyed();
}
protected void paint(Graphics g) {
g.setColor(0xFFFFFFFF); // clear graphics
g.fillRect(0,0,getWidth(),getHeight());
win.paint(g); // and paint the window...
}
}
// this skin displays an image and show how this class must be extended
class ImageSkin extends Skin {
Image image;
ImageSkin(Image image) { this.image = image; }
protected void paint(Graphics g, int width, int height) { // override the paint method
g.drawImage(image,width/2,height/2,Graphics.VCENTER|Graphics.HCENTER);
}
// override the clone and copy methods
private ImageSkin() {}
public Skin clone() {
ImageSkin t = new ImageSkin();
copy(t);
return t;
}
protected void copy(ImageSkin skin) {
super.copy(skin);
skin.image = image;
}
}
public class Example5 extends MIDlet {
static MIDlet instance;
protected void startApp() throws MIDletStateChangeException {
instance = this;
Canvas5 c = new Canvas5();
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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -