⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nov01_ericg.txt

📁 TechTips j2me的常用技巧. 网络功能
💻 TXT
📖 第 1 页 / 共 3 页
字号:
        
        this.label = label;
    }

    public static int calcMinWidth( 
                                 String text, Font f ){
        return f.stringWidth( text ) + 8;
    }

    public static int calcMinHeight( 
                                 String text, Font f ){
        return f.getHeight() + 8;
        }

    public String getLabel(){ return label; }

    protected void paintArea( 
                        Graphics g, boolean hasFocus ){
        g.setStrokeStyle( g.SOLID );
        g.drawRect( x, y, w-1, h-1 );
        
        if( selected ){
            g.setColor( getForeColor() );
            g.fillRect( x, y, w-1, h-1 );
            g.setColor( getBackColor() );
        } else if( hasFocus ){
            g.setStrokeStyle( g.DOTTED );
            g.drawRect( x+2, y+2, w-5, h-5 );
            g.setStrokeStyle( g.SOLID );
        }
        
        g.drawString( 
                     label, x+4, y+4, g.TOP | g.LEFT );
        }

    public void keyPressed( int keyCode ){
        int action = getGameAction( keyCode );
        switch( action ){
            case UP:
            case LEFT:
                moveFocus( false );
                break;
            case DOWN:
            case RIGHT:
                moveFocus( true );
                break;
            case FIRE:
                selected = true;
                repaintArea( this, true );
                break;
        }
    }

    public void keyReleased( int keyCode ){
        int action = getGameAction( keyCode );
        switch( action ){
            case FIRE:
                selected = false;
                repaintArea( this, true );
                
                if( listener != null ){
                    listener.buttonPressed( this );
                }
                break;
        }
    }
    
    public void setListener( Listener listener ){
        this.listener = listener;
    }
}

Most of the logic in PushButton has to do with the painting of 
the component. The push button can draw itself in three states: 
unselected with focus, unselected without focus, and selected.  
It responds to key events in order to change its selected state
or move the focus away from it to another component. (The term 
focus, as used here, refers to the concept of input focus, that 
is, the particular component on screen to which keyboard/keypad 
input is directed.)

The only thing missing now is the manager class. Its purpose is 
to track the other components and to pass along paint and input 
events as appropriate.

import java.util.*;
import javax.microedition.lcdui.*;

// A subclass of Area that can act as
// the parent for other components.

public class Manager extends Area {
    protected Vector children = new Vector();
    protected Area   focus = null;

    public Manager(){
        super( 0, 0, 0, 0, null );
        w = getCanvasWidth();
        h = getCanvasHeight();
    }
    
    public void add( Area child ){
        if( !children.contains( child ) ){
            children.addElement( child );
            child.setParent( this );
            repaintArea( child, false );
        }
    }

    protected Area getFocus(){
        if( focus == null && children.size() > 0 ){
            focus = (Area) children.elementAt( 0 );
        }

        return focus;
    }
    
    public void keyPressed( int keyCode ){
        Area focus = getFocus();
        if( focus != null && focus != this ){
            focus.keyPressed( keyCode );
        }
    }
    
    public void keyReleased( int keyCode ){
        Area focus = getFocus();
        if( focus != null && focus != this ){
            focus.keyReleased( keyCode );
        }
    }
    
    public void keyRepeated( int keyCode ){
        Area focus = getFocus();
        if( focus != null && focus != this ){
            focus.keyRepeated( keyCode );
        }
    }
    
    // Called to move the focus to the next
    // or previous component

    protected void moveFocus( boolean forward ){
        Area oldFocus = getFocus();
        if( oldFocus != null ){
            int i = children.indexOf( oldFocus );
            int last = children.size() - 1;
            if( forward ){
                if( ++i > last ) i = 0;
            } else {
                if( --i < 0 ) i = last;
            }
            
            focus = (Area) children.elementAt( i );
            repaintArea( oldFocus, false );
            repaintArea( focus, true );
        }
    }

    public void remove( Area child ){
        if( children.removeElement( child ) ){
            child.setParent( null );
            repaintArea( child, false );
        }
    }

    protected void paint( Graphics g ){
        if( focus == null ) getFocus();

        eraseBackground( g );
        g.setColor( getForeColor() );

        int n = children.size();
        for( int i = n-1; i >= 0; --i ){
             try {
                Area area = 
                    (Area) children.elementAt( i );
                area.paint( g, ( focus == area ) );
            }
            catch( Exception e ){
            }
        }
    }

    protected void paintArea( 
                        Graphics g, boolean hasFocus ){
    }
}

Finally, here's a simple MIDlet that uses the Manager and 
PushButton classes to display three push buttons:

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

// A simple example that shows how to use
// custom-drawn components.  Creates a
// Manager that has three PushButtons on it.

public class Tester extends MIDlet
                    implements CommandListener,
                               PushButton.Listener {

    private Display display;
    private Command exitCommand =
                         new Command( "Exit",
                                     Command.EXIT, 1 ); 

    public Tester(){
    }

    protected void destroyApp( boolean unconditional )
                    throws MIDletStateChangeException {
        exitMIDlet();
    }

    protected void pauseApp(){
    }

    protected void startApp()
                    throws MIDletStateChangeException {
        if( display == null ){ // first time called...
            initMIDlet();
        }
    }

    private void initMIDlet(){
        display = Display.getDisplay( this );

        Manager m = new Manager();

        PushButton pb = new PushButton( 
                                     "First", 0, 0 );
        pb.setListener( this );
        m.add( pb );

        pb = new PushButton( "Second", 20, 20 );
        pb.setListener( this );
        m.add( pb );

        pb = new PushButton( "Third", 0, 60, 50, 0 );
        pb.setListener( this );
        m.add( pb );

        m.addCommand( exitCommand );
        m.setCommandListener( this );

        display.setCurrent( m );
    }

    public void exitMIDlet(){
        notifyDestroyed();
    }

    public void commandAction( Command c,
                               Displayable d ){
        exitMIDlet();
    }
    
    public void buttonPressed( PushButton which ){
        String label = which.getLabel();

        Alert a = new Alert( "Pressed!",
            "You pressed the " + label + " button.",
            null, null );
            
        display.setCurrent( a, which.getParent() );
    }
}

If you're familiar with AWT or Swing programming in J2SE, the 
code in the initMIDlet method should look very familiar. Run this 
sample and you'll see three push buttons on screen, two of which 
are overlapping. Use the UP, DOWN, LEFT and RIGHT keys to move 
the input focus from one button to another, and press and release
the FIRE key to display an alert.

Use the code above as the basis for your own custom component 
coding. There are many improvements you can make. For example, 
you could add support for pointer events. Or you can optimize the 
painting and support the hiding of components. Start with this 
small bit of code and only add the functionality that you need.

.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

IMPORTANT: Please read our Terms of Use and Privacy policies:
http://www.sun.com/share/text/termsofuse.html
http://www.sun.com/privacy/ 

* FEEDBACK
  Comments? Send your feedback on the J2ME Tech Tips to: 
  
  jdc-webmaster@sun.com

* SUBSCRIBE/UNSUBSCRIBE
  - To subscribe, go to the subscriptions page,
    (http://developer.java.sun.com/subscription/), choose
    the newsletters you want to subscribe to and click "Update".
  - To unsubscribe, go to the subscriptions page,
    (http://developer.java.sun.com/subscription/), uncheck the
    appropriate checkbox, and click "Update".
  - To use our one-click unsubscribe facility, see the link at 
    the end of this email:
    
- ARCHIVES
You'll find the J2ME Tech Tips archives at:

http://java.sun.com/jdc/J2METechTips/index.html


- COPYRIGHT
Copyright 2001 Sun Microsystems, Inc. All rights reserved.
901 San Antonio Road, Palo Alto, California 94303 USA.

This document is protected by copyright. For more information, see:

http://java.sun.com/jdc/copyright.html

J2ME Tech Tips 
November 14, 2001

Sun, Sun Microsystems, Java, Java Developer Connection, J2ME, and
J2SE are trademarks or registered trademarks of Sun Microsystems, 
Inc. in the United States and other countries.




⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -