📄 button.java
字号:
/*
* 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/
*/
package mwt;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
/**
* <p>A button is a component that can cause an action to happen when is pushed.</p>
*
* The action is determinated by the mwt.EventListener
* given during the button's creation:
* <pre>
* Button b = new Button(20, 4, 40, 30, "Press Me!", new EventListener() {
* public void processEvent(int eventType, Component c, Object[] args) {
* System.out.println("Pressed");
* }
* }, 0);
* </pre>
* <p>This works just fine, however, the java compiler creates a new class for each event
* listener implementation. When using a lot of buttons, the jar size will increase significally.</p>
*
* A solution is to group buttons' actions in the same event listener implementation:
* <pre>
* class MyCanvas extends Canvas implements EventListener {
* Window win = ...
* static final int NEW_GAME = 0;
* static final int OPTIONS = 1;
* static final int EXIT = 2;
*
* MyCanvas() {
* win.add(new Button(4, 4, 30, 20, "New Game", this, NEW_GAME));
* win.add(new Button(4,24, 30, 20, "Options", this, OPTION));
* win.add(new Button(4,44, 30, 20, "Exit", this, EXIT));
* }
*
* public void processEvent(int eventType, Component c, Object[] args) {
* switch(eventType) {
* case EVENT_ACTION:
* Button b = (Button) c;
* switch(c.getActionType()) {
* case NEW_GAME: System.out.println("New Game pressed: " + c); break;
* case OPTIONS: System.out.println("Options pressed: " + c); break;
* case EXIT: System.out.println("Exit pressed: " + c); break;
* }
* break;
* case EVENT_UNDEFINED: break;
* }
* }
*
* ...
* }
* </pre>
* <p>The component parameter is the button that is triggering the event, the args parameter is null.<br>
* See also mwt.EventListener.</p>
*
* <p>A button's action is triggered when it has focus and its owner window
* reports a {@link mwt.Window#FOCUSACTION_FIRE}.<br>
* You can trigger the action programmatically also. See {@link #processAction()}.<p>
*
* <h3>Styles</h3>
* <p>A button has four different "styles", and for each one, a skin and a font.<br>
* When a button is created, a <b>copy</b> of the default skin and fonts is assigned.<br>
* You can set the default style using {@link #setDefaultSkin(int, Skin)} and
* {@link #setDefaultFont(int, Font)}.<br>
* After the button is created, you can still change its style using
* {@link #setSkin(int, Skin)} and {@link #setFont(int, Font)}.<br>
* The style value must be any of the STYLE_* constants:<p>
* <body>
* <table width="228" border="0" cellspacing="8" cellpadding="0">
* <tr>
* <td width="60"><img src="{@docRoot}/resources/button_default.png"></td>
* <td width="156">{@link #STYLE_DEFAULT}</td>
* </tr>
* <tr>
* <td><img src="{@docRoot}/resources/button_focused.png"></td>
* <td>{@link #STYLE_FOCUSED}</td>
* </tr>
* <tr>
* <td><img src="{@docRoot}/resources/button_pressed.png"></td>
* <td>{@link #STYLE_PRESSED}</td>
* </tr>
* <tr>
* <td><img src="{@docRoot}/resources/button_disabled.png"></td>
* <td>{@link #STYLE_DISABLED}</td>
* </tr>
* </table>
*
*/
public class Button extends Component {
/** Constant value to get/set a skin/font. */
static final public int STYLE_DEFAULT = 0;
/** Constant value to get/set a skin/font. */
static final public int STYLE_FOCUSED = 1;
/** Constant value to get/set a skin/font. */
static final public int STYLE_PRESSED = 2;
/** Constant value to get/set a skin/font. */
static final public int STYLE_DISABLED = 3;
private String text;
private int textAlign = ALIGN_MIDDLE_CENTER;
private boolean showAsPressed; // forces to display this button using the STYLE_PRESSED
private int keyPressed; // stores temporary the last keycode that pressed this button
private final EventListener action;
private final int actionType;
private final Skin[] skins;
private final Font[] fonts;
//private String onClick; // since 1.2
// Default MWT style
static private final Skin[] defaultSkins = {
new Skin(new int[] {0xFFFFFF,0,0xC6C6C6,0xC6C6C6}),
new Skin(new int[] {0xFFFFFF,0,0xA1C632,0xCFFF40}),
new Skin(new int[] {0xCFFF40,0,0xC6C6C6}),
new Skin(new int[] {0xFFFFFF,0x888888,0xC6C6C6,0xC6C6C6})
};
static private final Font[] defaultFonts = {
new Font(0,0,0,0),
new Font(0,0,0,0),
new Font(0,0,0,0),
new Font(0x888888,0,0,0)
};
/** Creates a new button at the given position and size */
public Button(int x, int y, int width, int height, String text, EventListener action, int actionType) {
super(x,y,width,height,false);
skins = new Skin[] { defaultSkins[0].clone(), defaultSkins[1].clone(), defaultSkins[2].clone(), defaultSkins[3].clone() };
fonts = new Font[] { defaultFonts[0].clone(), defaultFonts[1].clone(), defaultFonts[2].clone(), defaultFonts[3].clone() };
this.setText(text);
this.action = action;
this.actionType = actionType;
}
/** Gets the default skin for the given style. */
static final public Skin getDefaultSkin(int style) { return defaultSkins[style]; }
/** Sets the default skin for the given style. */
static final public void setDefaultSkin(int style, Skin skin) { defaultSkins[style] = skin; }
/** Gets the default font for the given style. */
static final public Font getDefaultFont(int style) { return defaultFonts[style]; }
/** Sets the default font for the given style. */
static final public void setDefaultFont(int style, Font font) { defaultFonts[style] = font; }
/** Gets this button skin for the given style. */
public Skin getSkin(int style) { return skins[style]; }
/** Sets this button skin for the given style. */
public void setSkin(int style, Skin skin) { skins[style] = skin; }
/** Gets this button font for the given style. */
public Font getFont(int style) { return fonts[style]; }
/** Sets this button font for the given style. */
public void setFont(int style, Font font) { fonts[style] = font; }
/** Gets this button's text. */
public String getText() { return text; }
/** Sets this button's text. */
public void setText(String text) { this.text = text; }
/** Gets the text align. A <a href="./Component.html#align">Component ALIGN constant</a>. */
public int getTextAlign() { return textAlign; }
/** Sets the text align. A <a href="./Component.html#align">Component ALIGN constant</a>. */
public void setTextAlign(int textAlign) { this.textAlign = textAlign; }
/** Method permormed when the event listener is null. Default implementation is empty.
* @since 1.2 */
protected void onClick() {}
/** Performs a "click" programmatically.
* @since 1.2 */
final public void click() {
showAsPressed = true;
if(action != null) { action.processEvent(EventListener.EVENT_ACTION,this,null); } // removeme
else onClick();
}
/** Gets the action type value given when this button was created. */
final public int getActionType() { return actionType; }
/** Performs a "click" programmatically.
* @deprecated use {@link #click()} instead */
final public void processAction() { click(); }
// overrides
protected boolean keyEvent(long key, Window window) {
if(window.getFocusAction((int)key) == Window.FOCUSACTION_FIRE) {
keyPressed = (int) key;
if((key >> 32) == 0) click();
return true;
}
else return false;
}
// overrides
protected void paint(Graphics g, Window window) {
final int i;
if(!isHierarchyEnabled()) i = 3;
else if(showAsPressed || (window.getFocus() == this && window.getKeyState(keyPressed) != 0))
{ i = 2; showAsPressed = false; }
else if(window.getFocus() == this) i = 1;
else i = 0;
getSkin(i).paint(this,g);
getFont(i).write(g,getText(),0,0,getWidth(),getHeight(),getTextAlign());
}
private Image iconImage;
public Image getIconImage() {
return iconImage;
}
public void setIconImage(Image iconImage) {
this.iconImage = iconImage;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -