📄 menupage.java
字号:
package myGame.gui.menu;
import java.util.Vector;
import javax.microedition.lcdui.Image;
/**
* A <code>MenuPage</code> class represents a title and a number of choices in
* a <code>Menu</code>. The <code>MenuPage</code>'s choices are added as
* <code>PageItem</code>s via the <code>addItem</code> method.
*
* @see myGame.gui.menu.Menu
* @see myGame.gui.menu.PageItem
* @author YuBingxing
*/
public class MenuPage {
/** Left aligned image, right aligned text */
public static final int LAYOUT_LEFT = 0;
/** Right aligned image, left aligned text */
public static final int LAYOUT_RIGHT = 1;
/** The title of the page */
protected char[] m_title;
/** The title image of the page */
protected Image m_titleImage;
/** The alignment of title and image */
protected int m_layout = LAYOUT_LEFT;
/** The page items */
protected Vector m_items = new Vector();
/** Current selected item index */
protected int m_currentIndex = -1;
/**
* Creates a new page for a menu.
*
* @param title
* The title of the page or null.
* @param titleImage
* The image of the page or null.
*/
public MenuPage(char[] title, Image titleImage) {
setTitle(title);
setTitleImage(titleImage);
}
/**
* Sets the layout of the image, any of LAYOUT_LEFT, LAYOUT_RIGHT.
*
* @param layout
* Image layout.
*/
public void setLayout(int layout) {
m_layout = layout;
}
/**
* Returns the layout of the image, any of LAYOUT_LEFT, LAYOUT_RIGHT.
*
* @return Image layout.
*/
public int getLayout() {
return m_layout;
}
/**
* Returns the title of this page.
*
* @return The page title.
*/
public char[] getTitle() {
return m_title;
}
/**
* Sets the title of this page.
*
* @param title
* The page title.
*/
public void setTitle(char[] title) {
m_title = title;
}
/**
* Returns the title of this page.
*
* @return The page title image.
*/
public Image getTitleImage() {
return m_titleImage;
}
/**
* Sets the title image of this page.
*
* @param titleImage
* The page title iamge.
*/
public void setTitleImage(Image titleImage) {
m_titleImage = titleImage;
}
/**
* Adds an item to this page.
*
* @param item
* The item to add.
*/
public synchronized void addItem(PageItem item) {
m_items.addElement(item);
item.addedToPage();
if (m_currentIndex == -1) {
m_currentIndex = 0;
}
}
/**
* Removes an item from this page.
*
* @param item
* The item to remove.
*/
public synchronized void removeItem(PageItem item) {
m_items.removeElement(item);
if (size() == 0) {
m_currentIndex = -1;
}
}
/**
* Removes the item at specified index from this page.
*
* @param index
* The index of the item to remove.
*/
public synchronized void removeItem(int index) {
m_items.removeElementAt(index);
}
/**
* Returns number of items in this page.
*
* @return Number of items.
*/
public synchronized int size() {
return m_items.size();
}
/**
* Returns the currently selected index in this page. In special cases this
* method may return -1, for no selected items.
*
* @return Selected item index.
*/
public synchronized int getSelectedIndex() {
PageItem item = itemAt(m_currentIndex);
if (item != null && !item.isEnabled()) {
setSelectedIndex(m_currentIndex + 1);
}
return m_currentIndex;
}
/**
* Sets the currently selected index in this page. If the index is greater
* than number of items in the page, it is wrapped to the first item. If the
* index is below zero, it is wrapped to the last item. If correct index is
* not possible to set index will be set to -1. This happens if there are no
* items, or all items are disabled.
*
* @param index
* Index of selected item.
*/
public synchronized void setSelectedIndex(int index) {
int size = size();
boolean dirDown = index - m_currentIndex > 0;
boolean allDisabled = true;
for (int i = 0; allDisabled && i < size; i++) {
allDisabled = !itemAt(i).isEnabled();
}
if (size == 0 || allDisabled) {
index = -1;
} else {
boolean enabled = true;
do {
if (index >= size) {
index = 0;
} else if (index < 0) {
index = size - 1;
}
enabled = itemAt(index).isEnabled();
if (!enabled) {
if (dirDown) {
index++;
} else {
index--;
}
}
} while (!enabled);
}
m_currentIndex = index;
}
/**
* Returns item at specified index.
*
* @param index
* The index of the idem
* @return The item at specified index, or null if not found.
*/
public synchronized PageItem itemAt(int index) {
if (index < 0 || index >= size()) {
return null;
} else {
return (PageItem) m_items.elementAt(index);
}
}
/**
* Returns the index of specified item. It this item does not belong to the
* list, -1 is returned.
*
* @param item
* The item whose index to find.
* @return index of the item, or -1 if item is not part of the menu.
*/
public synchronized int getIndex(PageItem item) {
return m_items.indexOf(item);
}
/**
* Removes all items on this page
*/
public synchronized void removeAllItems() {
m_items.removeAllElements();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -