📄 form.java
字号:
* Returns the number of buttons on the menu bar for use with getSoftButton()
*/
public int getSoftButtonCount() {
return menuBar.getSoftButtons().length;
}
/**
* Returns the button representing the softbutton, this allows modifying softbutton
* attributes and behavior programmatically rather than by using the command API.
* Notice that this API behavior is fragile since the button mapped to a particular
* offset might change based on the command API
*
* @return a button that can be manipulated
*/
public Button getSoftButton(int offset) {
return menuBar.getSoftButtons()[offset];
}
/**
* Returns the style of the menu
*/
public Style getMenuStyle() {
return menuBar.getMenuStyle();
}
/**
* Returns the style of the title
*/
public Style getTitleStyle() {
return title.getStyle();
}
/**
* Allows the display to skip the menu dialog if that is the current form
*/
Form getPreviousForm() {
return previousForm;
}
/**
* @inheritDoc
*/
void initLaf(LookAndFeel laf) {
transitionOutAnimator = laf.getDefaultFormTransitionOut();
transitionInAnimator = laf.getDefaultFormTransitionIn();
}
/**
* Resets the cache focus vectors, this is a good idea when we remove
* or add an element to the layout.
*/
void clearFocusVectors() {
focusDownSequence = null;
focusRightSequence = null;
}
synchronized void initFocusRight() {
if (focusRightSequence == null) {
focusRightSequence = new Vector();
findAllFocusable(contentPane, focusRightSequence, true);
}
}
synchronized void initFocusDown() {
if (focusDownSequence == null) {
focusDownSequence = new Vector();
findAllFocusable(contentPane, focusDownSequence, false);
}
}
/**
* Adds a component to the vector in the appropriate location based on its
* focus order
*/
private void addSortedComponentRight(Vector components, Component c) {
int componentCount = components.size();
int componentX = c.getAbsoluteX();
int bestSpot = 0;
Component scrollableParent = findScrollableAncestor(c);
// find components in the same row and add the component either at the end
// of the line or at its start
for (int iter = 0; iter < componentCount; iter++) {
Component current = (Component) components.elementAt(iter);
// this component is in the same row...
Component currentScrollParent = findScrollableAncestor(current);
if (currentScrollParent == scrollableParent) {
if (isInSameRow(current, c)) {
int currentX = current.getAbsoluteX();
if (currentX > componentX) {
continue;
}
bestSpot = iter + 1;
continue;
}
} else {
Component tempScrollableParent = scrollableParent;
if (scrollableParent == null) {
tempScrollableParent = c;
}
Component tempCurrentScrollParent = currentScrollParent;
if (currentScrollParent == null) {
tempCurrentScrollParent = current;
}
if (tempCurrentScrollParent.getAbsoluteX() > tempScrollableParent.getAbsoluteX()) {
continue;
}
if (isInSameRow(tempCurrentScrollParent, tempScrollableParent)) {
bestSpot = iter + 1;
continue;
}
}
if (current.getAbsoluteY() < c.getAbsoluteY()) {
bestSpot = iter + 1;
}
}
components.insertElementAt(c, bestSpot);
}
/**
* Returns the first scrollable ancestor for this component or null if no
* such ancestor exists
*/
private Component findScrollableAncestor(Component c) {
c = c.getParent();
if (c == null || c.isScrollable()) {
return c;
}
return findScrollableAncestor(c);
}
/**
` * Adds a component to the vector in the appropriate location based on its
* focus order
*/
private void addSortedComponentDown(Vector components, Component c) {
int componentCount = components.size();
int componentY = c.getAbsoluteY();
int bestSpot = 0;
Component scrollableParent = findScrollableAncestor(c);
// find components in the same column and add the component either at the end
// of the line or at its start
for (int iter = 0; iter < componentCount; iter++) {
Component current = (Component) components.elementAt(iter);
// this component is in the same column...
Component currentScrollParent = findScrollableAncestor(current);
if (currentScrollParent == scrollableParent) {
if (isInSameColumn(current, c)) {
int currentY = current.getAbsoluteY();
if (currentY > componentY) {
continue;
}
bestSpot = iter + 1;
continue;
}
} else {
Component tempScrollableParent = scrollableParent;
if (scrollableParent == null) {
tempScrollableParent = c;
}
Component tempCurrentScrollParent = currentScrollParent;
if (currentScrollParent == null) {
tempCurrentScrollParent = current;
}
if (tempCurrentScrollParent.getAbsoluteY() > tempScrollableParent.getAbsoluteY()) {
continue;
}
if (isInSameColumn(tempCurrentScrollParent, tempScrollableParent)) {
bestSpot = iter + 1;
continue;
}
}
if (current.getAbsoluteX() < c.getAbsoluteX()) {
bestSpot = iter + 1;
}
}
components.insertElementAt(c, bestSpot);
}
/**
* Returns true if the given dest component is in the column of the source component
*/
private boolean isInSameColumn(Component source, Component dest) {
return Rectangle.intersects(source.getAbsoluteX(), source.getAbsoluteY(),
source.getWidth(), Integer.MAX_VALUE, dest.getAbsoluteX(), dest.getAbsoluteY(),
dest.getWidth(), dest.getHeight());
}
/**
* Returns true if the given dest component is in the row of the source component
*/
private boolean isInSameRow(Component source, Component dest) {
return Rectangle.intersects(source.getAbsoluteX(), source.getAbsoluteY(),
Integer.MAX_VALUE, source.getHeight(), dest.getAbsoluteX(), dest.getAbsoluteY(),
dest.getWidth(), dest.getHeight());
}
/**
* Adds a component to the vector in the appropriate location based on its
* focus order
*/
private void addSortedComponent(Vector components, Component c, boolean toTheRight) {
if (toTheRight) {
addSortedComponentRight(components, c);
} else {
addSortedComponentDown(components, c);
}
}
/**
* Default command is invoked when a user presses fire, this functionality works
* well in some situations but might collide with elements such as navigation
* and combo boxes. Use with caution.
*/
public void setDefaultCommand(Command defaultCommand) {
this.defaultCommand = defaultCommand;
}
/**
* Default command is invoked when a user presses fire, this functionality works
* well in some situations but might collide with elements such as navigation
* and combo boxes. Use with caution.
*/
public Command getDefaultCommand() {
if (selectCommand != null) {
return selectCommand;
}
return defaultCommand;
}
/**
* Indicates the command that is defined as the clear commnand in this form.
* A clear command can be used both to map to a "clear" hardware button
* if such a button exists.
*/
public void setClearCommand(Command clearCommand) {
this.clearCommand = clearCommand;
}
/**
* Indicates the command that is defined as the clear commnand in this form.
* A clear command can be used both to map to a "clear" hardware button
* if such a button exists.
*/
public Command getClearCommand() {
return clearCommand;
}
/**
* Indicates the command that is defined as the back commnand out of this form.
* A back command can be used both to map to a hardware button (e.g. on the Sony Ericsson devices)
* and by elements such as transitions etc. to change the behavior based on
* direction (e.g. slide to the left to enter screen and slide to the right to exit with back).
*/
public void setBackCommand(Command backCommand) {
this.backCommand = backCommand;
}
/**
* Indicates the command that is defined as the back commnand out of this form.
* A back command can be used both to map to a hardware button (e.g. on the Sony Ericsson devices)
* and by elements such as transitions etc. to change the behavior based on
* direction (e.g. slide to the left to enter screen and slide to the right to exit with back).
*/
public Command getBackCommand() {
return backCommand;
}
/**
* Finds all focusable components in the hierarchy
*/
private void findAllFocusable(Container c, Vector v, boolean toTheRight) {
int size = c.getComponentCount();
for (int iter = 0; iter < size; iter++) {
Component current = c.getComponentAt(iter);
if (current instanceof Container) {
findAllFocusable((Container) current, v, toTheRight);
}
if (current.isFocusable()) {
addSortedComponent(v, current, toTheRight);
}
}
}
/**
* Sets the title after invoking the constructor
*
* @param title the form title
*/
public Form(String title) {
this();
this.title.setText(title);
}
/**
* @inheritDoc
*/
protected String getUIID() {
return "Form";
}
/**
* This method returns the Content pane instance
*
* @return a content pane instance
*/
public Container getContentPane() {
return contentPane;
}
/**
* Removes all Components from the Content Pane
*/
public void removeAll() {
contentPane.removeAll();
}
/**
* Sets the background image to show behind the form
*
* @param bgImage the background image
*/
public void setBgImage(Image bgImage) {
getStyle().setBgImage(bgImage);
}
/**
* @inheritDoc
*/
public void setLayout(Layout layout) {
contentPane.setLayout(layout);
}
/**
* Sets the Form title to the given text
*
* @param title the form title
*/
public void setTitle(String title) {
this.title.setText(title);
}
/**
* Returns the Form title text
*/
public String getTitle() {
return title.getText();
}
/**
* Adds Component to the Form's Content Pane
*
* @param cmp the added param
*/
public void addComponent(Component cmp) {
contentPane.addComponent(cmp);
setShouldCalcPreferredSize(true);
}
/**
* Adds Component to the Form's Content Pane
*
* @param constraints this method is useful when the Layout requires a constraint
* such as the BorderLayout.
* In this case you need to specify an additional data when you add a Component,
* such as "CENTER", "NORTH"...
*
* @param cmp component to add
*/
public void addComponent(String constraints, Component cmp) {
contentPane.addComponent(constraints, cmp);
setShouldCalcPreferredSize(true);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -