Uber Clone 的问题

A trouble with Uber Clone

大家好:我正在尝试使用 Uber 克隆代码,并且我正在使用 Netbeans。我有两个问题: 1).- 在 countryPickerForm Listing 4.12 中(Listing number 是书中的列表“Create an Uber Clone...”); Netbeans 标记我一个错误,(“找不到符号变量 CommonCode”),当然,在 CommonCode 对象中,我不知道要使用什么库

´´´

public class CountryPickerForm extends Form{
    //@SuppressWarnings("LeakingThisInConstructor")
    public CountryPickerForm(Button sourceButton, Resources Flag){
        super(BoxLayout.y());
        **CommonCode.initBlackTitleForm(this,"Select a Country", val-> search(val));**
        Image blankIcon = Image.createImage(100, 70, 0);

´´´ 2).- 第二个问题:Listing (5.22) 的正确位置是什么?当焦点改变时,切换“WhereTo?”UI “。我将它放在任何方法之外的 MapForm class 内,但 Neatbeans 给我标记了一个错误:"< identifier > expected. Ilegal start of type " 这是代码:

from.addFocusListener(new FocusListener(){
    public void focusGained(Component cmp){
        fromSelected.setIcon(square);
        if(layer.getComponentCount()> 1){
            Component c = layer.getComponentAt(1);
            c.setY(getDisplayHeight());
            layer.animateUnlayout(200,150,() ->{
            c.remove();
            revalidate();
        });
      }
    }
    
    public void focusLost(){
        fromSelected.setIcon(circle);
    }
});
to.addFocusListener(new FocusListener(){
    public void focusGained(Component cmp){
        fromSelected.setIcon(circle):
        toSelected.setIcon(square);
        showToNavigationBar(layer);
    }
    public void focusLost(Component cmp){
        toSelecte3dsetIcon(circle);
    }
});

谢谢大家!!!

CommonCode 在本书的一次编辑中以某种方式丢失了。它是本书开头列出的可下载 zip 的一部分,应该在那里。这是 class:

的完整列表
package com.codename1.apps.uberclone.forms;

import com.codename1.apps.uberclone.server.UserService;
import com.codename1.components.MultiButton;
import com.codename1.io.Log;
import com.codename1.ui.Button;
import static com.codename1.ui.CN.*;
import com.codename1.ui.Command;
import com.codename1.ui.Container;
import com.codename1.ui.Display;
import com.codename1.ui.FontImage;
import com.codename1.ui.Form;
import com.codename1.ui.Graphics;
import com.codename1.ui.Image;
import com.codename1.ui.Label;
import com.codename1.ui.TextField;
import com.codename1.ui.Toolbar;
import com.codename1.ui.animations.CommonTransitions;
import com.codename1.ui.animations.Transition;
import com.codename1.ui.events.ActionEvent;
import com.codename1.ui.events.ActionListener;
import com.codename1.ui.layouts.BorderLayout;
import com.codename1.ui.layouts.LayeredLayout;
import com.codename1.ui.plaf.Style;
import com.codename1.util.LazyValue;
import com.codename1.util.SuccessCallback;
import java.io.IOException;

/**
 * Common code for construction and initialization of various classes e.g. the side menu logic etc.
 *
 * @author Shai Almog
 */
public class CommonCode {
    private static Image avatar;
    public static Image getAvatar(SuccessCallback<Image> avatarChanged) {
        if(avatar == null) {
            int size = convertToPixels(10);
            Image temp = Image.createImage(size, size, 0xff000000);
            Graphics g = temp.getGraphics();
            g.setAntiAliased(true);
            g.setColor(0xffffff);
            g.fillArc(0, 0, size, size, 0, 360);
            Object mask = temp.createMask();
            UserService.fetchAvatar(i -> {
                avatar = i.fill(size, size).applyMask(mask);
                avatarChanged.onSucess(avatar);
            });
            if(avatar != null) {
                return avatar;
            }
            Style s = new Style();
            s.setFgColor(0xc2c2c2);
            s.setBgTransparency(255);
            s.setBgColor(0xe9e9e9);
            FontImage x = FontImage.createMaterial(FontImage.MATERIAL_PERSON, s, size);
            avatar = x.fill(size, size);
            if(avatar instanceof FontImage) {
                avatar = ((FontImage)avatar).toImage();
            }
            avatar = avatar.applyMask(mask);
        }
        return avatar;
    }
    
    public static Image setAvatar(String imageFile) {
        int size = convertToPixels(10);
        Image temp = Image.createImage(size, size, 0xff000000);
        Graphics g = temp.getGraphics();
        g.setAntiAliased(true);
        g.setColor(0xffffff);
        g.fillArc(0, 0, size, size, 0, 360);
        Object mask = temp.createMask();
        
        try {
            Image img = Image.createImage(imageFile);
            avatar = img.fill(size, size).applyMask(mask);
        } catch(IOException err) {
            // this is unlikely as we just grabbed the image...
            Log.e(err);
        }
        return avatar;
    }
    
    public static MultiButton createEntry(char icon, String title) {
        MultiButton b = new MultiButton(title);
        b.setUIID("Container");
        b.setUIIDLine1("WhereToButtonLine1");
        b.setIconUIID("WhereToButtonIcon");
        FontImage.setMaterialIcon(b, icon);
        return b;
    }
    
    public static MultiButton createEntry(char icon, String title, String subtitle) {
        MultiButton b = new MultiButton(title);
        b.setTextLine2(subtitle);
        b.setUIID("Container");
        b.setUIIDLine1("WhereToButtonLineNoBorder");
        b.setUIIDLine2("WhereToButtonLine2");
        b.setIconUIID("WhereToButtonIcon");
        FontImage.setMaterialIcon(b, icon);
        return b;
    }
    
    
    public static Label createSeparator() {
        Label sep = new Label("", "WhereSeparator");
        sep.setShowEvenIfBlank(true);
        return sep;
    }
    
    public static void constructSideMenu(Toolbar tb) {
        Button userAndAvatar = new Button("Shai Almog", "AvatarBlock");
        userAndAvatar.setIcon(getAvatar(i -> userAndAvatar.setIcon(i)));
        userAndAvatar.setGap(convertToPixels(3));
        userAndAvatar.addActionListener(e -> new EditAccountForm().show());
        tb.addComponentToSideMenu(userAndAvatar);
        
        MultiButton uberForBusiness = new MultiButton("Do you Uber for business?");
        uberForBusiness.setTextLine2("Tap to create your business profile");
        uberForBusiness.setUIID("UberForBusinessBackground");
        uberForBusiness.setUIIDLine1("UberForBusinessLine1");
        uberForBusiness.setUIIDLine2("UberForBusinessLine2");
        tb.addComponentToSideMenu(uberForBusiness);
        
        tb.addCommandToSideMenu("Payment", null, e -> {});
        tb.addCommandToSideMenu("Your Trips", null, e -> {});
        tb.addCommandToSideMenu("Help", null, e -> {});
        tb.addCommandToSideMenu("Free Rides", null, e -> {});
        tb.addCommandToSideMenu("Settings", null, e -> new SettingsForm().show());
        
        Button legalButton = new Button("Legal", "Legal");
        Container legal = BorderLayout.centerCenterEastWest(null, new Label("v4.178.1001", "VersionNumber"), legalButton);
        legal.setLeadComponent(legalButton);
        legal.setUIID("SideNavigationPanel");
        tb.setComponentToSideMenuSouth(legal);
    } 
    
    
    /**
     * Initializes a form with a black background title animation style
     * @param f the form
     */
    public static void initBlackTitleForm(Form f, String title, SuccessCallback<String> searchResults) {
        Form backTo = getCurrentForm();
        f.getContentPane().setScrollVisible(false);
        Button back = new Button("", "TitleCommand");
        removeTransitionsTemporarily(backTo);
        back.addActionListener(e -> backTo.showBack());
        back.getAllStyles().setFgColor(0xffffff);
        FontImage.setMaterialIcon(back, FontImage.MATERIAL_ARROW_BACK);
                
        f.setBackCommand(new Command("") {
            @Override
            public void actionPerformed(ActionEvent evt) {
                backTo.showBack();
            }
        });

        Container searchBack = null;
        if(searchResults != null) {
            Button search = new Button("", "TitleCommand");
            search.getAllStyles().setFgColor(0xffffff);
            FontImage.setMaterialIcon(search, FontImage.MATERIAL_SEARCH);
            search.addActionListener(e -> {
                
            });
            searchBack = BorderLayout.north(
                BorderLayout.centerEastWest(null, search, back));
        } else {
            searchBack = BorderLayout.north(
                BorderLayout.centerEastWest(null, null, back));
        }
        
        Label titleLabel = new Label(title, "WhiteOnBlackTitle");

        titleLabel.getAllStyles().setMarginTop(back.getPreferredH());
        titleLabel.getAllStyles().setMarginUnit(Style.UNIT_TYPE_PIXELS, Style.UNIT_TYPE_DIPS, Style.UNIT_TYPE_DIPS, Style.UNIT_TYPE_DIPS);
        
        f.getToolbar().setTitleComponent(LayeredLayout.encloseIn(searchBack, titleLabel));
        
        f.getAnimationManager().onTitleScrollAnimation(titleLabel.createStyleAnimation("WhiteOnBlackTitleLeftMargin", 200));
        
        f.setTransitionInAnimator(CommonTransitions.createCover(CommonTransitions.SLIDE_VERTICAL, false, 300));
        f.setTransitionOutAnimator(CommonTransitions.createUncover(CommonTransitions.SLIDE_VERTICAL, true, 300));        
    }
    
    public static void removeTransitionsTemporarily(final Form f) {
        final Transition originalOut = f.getTransitionOutAnimator();
        final Transition originalIn = f.getTransitionInAnimator();
        f.setTransitionOutAnimator(CommonTransitions.createEmpty());
        f.setTransitionInAnimator(CommonTransitions.createEmpty());
        f.addShowListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
                f.setTransitionOutAnimator(originalOut);
                f.setTransitionInAnimator(originalIn);
                f.removeShowListener(this);
            }
        });
    }
}

第二个列表出现在 MapFormshowNavigationToolbar。此代码稍后经过一些额外的重构,看起来像这样:

void showNavigationToolbar() {
    final Container layer = getLayeredPane(MapForm.class, true);
    final Container pinLayer = createPinLayer(layer);
    Button back = new Button("", "TitleCommand");
    FontImage.setMaterialIcon(back, FontImage.MATERIAL_ARROW_BACK);

    CompletionContainer cc = new CompletionContainer();
    AutoCompleteAddressInput from = new AutoCompleteAddressInput("Current Location", "From", layer, cc);
    AutoCompleteAddressInput to = new AutoCompleteAddressInput("", "Where To?", layer, cc);
    from.setCurrentLocation(LocationService.getCurrentLocation());
                    
    Image circle = createCircle();
    Label fromSelected = new Label(circle);
    Label toSelected = new Label(square);
    
    SearchService.nameMyCurrentLocation(LocationService.getCurrentLocation(), name -> from.setTextNoEvent(name));
    to.requestFocus();
    lastFocused = to;
    from.addFocusListener(createFromFocusListener(fromSelected, from, circle));
    to.addFocusListener(createToFocusListener(fromSelected, circle, toSelected, to));
    
    addMapListener((source, zoom, center) -> onMapChangeEvent(center));
    
    Container navigationToolbar = BoxLayout.encloseY(back, 
            BorderLayout.centerCenterEastWest(from, null, fromSelected), 
            BorderLayout.centerCenterEastWest(to, null, toSelected)
    );
    navigationToolbar.setUIID("WhereToToolbar");
    navigationToolbar.getUnselectedStyle().setBgPainter((g1, rect) -> 
        paintWhereToToolbarBackground(g1, layer, rect, fromSelected, circle, toSelected)
    );
    
    cc.addCompletionListener(e -> 
        onCompletionEvent(to, from, pinLayer, navigationToolbar, layer));

    back.addActionListener(e -> 
        onBackFromNavigation(pinLayer, navigationToolbar, layer));
    layer.add(NORTH, navigationToolbar);
    navigationToolbar.setWidth(getDisplayWidth());
    navigationToolbar.setHeight(getPreferredH());
    navigationToolbar.setY(-navigationToolbar.getHeight());
    getAnimationManager().addAnimation(layer.createAnimateLayout(200), 
                () -> cc.showCompletionBar(layer));
}

private FocusListener createToFocusListener(final Label fromSelected, Image circle, final Label toSelected, AutoCompleteAddressInput to) {
    return new FocusListener() {
        @Override
        public void focusGained(Component cmp) {
            fromSelected.setIcon(circle);
            toSelected.setIcon(square);
            lastFocused = to;
        }
        
        @Override
        public void focusLost(Component cmp) {
            toSelected.setIcon(circle);
        }
    };
}

private FocusListener createFromFocusListener(final Label fromSelected, AutoCompleteAddressInput from, Image circle) {
    return new FocusListener() {
        @Override
        public void focusGained(Component cmp) {
            fromSelected.setIcon(square);
            lastFocused = from;
        }
        
        @Override
        public void focusLost(Component cmp) {
            fromSelected.setIcon(circle);
        }
    };
}