为什么在这种情况下不显示对话框?

时间:2011-10-24 13:55:59

标签: java-me lwuit

当我点击我创建的用户定义组件时,我想显示Dialog:我将其类名命名为ListBox。我想用我的用户定义组件模拟LWUIT ComboBox,该组件将接受空白区域;因为LWUIT ComboBox不接受空白区域。

问题是当我的ListBox位于坐标下方时,其下方没有更多空间来显示Dialog,则Dialog未显示;但如果下方有更多空格,则显示Dialog

以下是未显示Dialog的拍摄图像:

enter image description here

在捕获的图片中,用户定义的ListBox是手机屏幕底部两个Button正上方的组件。

以下是代码:

public class ListBox extends Container implements ActionListener
{
    private Container cListBox = new Container(new BorderLayout());
    private Label[] tLabel;
    private int[] tLabelW;
    private int largestLabelW;
    private Label libelle = new Label();
    private Label arrow = new Label((MenuPrincipalForm.r).getImage("listboxarrow"));
    private int preferredWidth, preferredHeight, screenWidth, screenHeight;
    private Vector vData = new Vector();
    private final int leftPadding = 3;
    private CListCellListBox listRenderer;
    private List list;
    private Dialog dialog;
    private String selectedData;

    public ListBox(String[] lData, int prefHeight, int formWidth, int formHeight, int topMargin, int bottomMargin)
    {
        super(new FlowLayout(Component.CENTER));
        setFocusable(true);
        screenWidth = formWidth;
        screenHeight = formHeight;
        tLabel = new Label[lData.length + 1];
        tLabelW = new int[lData.length + 1];
        for (int i = 0 ; i < lData.length + 1 ; i++)
        {
            if (i < lData.length)
            {
                vData.addElement(new String(lData[i]));
                tLabel[i] = new Label(lData[i]);
                tLabelW[i] = tLabel[i].getPreferredW();
            }
            else
            {
                vData.addElement(new String(""));
                tLabel[i] = new Label("");
                tLabelW[i] = 0;
            }
        }
        largestLabelW = Comparator.max(tLabelW);
        preferredWidth = leftPadding + largestLabelW + arrow.getPreferredW();
        preferredHeight = prefHeight - 2 ;
        selectedData = String.valueOf(vData.lastElement());
        libelle.setText(String.valueOf(vData.lastElement()));
        libelle.setTextPosition(Label.LEFT);
        libelle.setPreferredW(preferredWidth);
        libelle.setPreferredH(preferredHeight);
        arrow.setAlignment(Label.CENTER);
        arrow.setPreferredH(preferredHeight);
        listRenderer = new CListCellListBox(false);
        list = (new CList(vData, false)).createList(listRenderer, this);
        list.setItemGap(0);
        list.setSelectedIndex(vData.indexOf(vData.lastElement()));
        list.getUnselectedStyle().setPadding(0, 0, 0, 0);
        list.getSelectedStyle().setPadding(0, 0, 0, 0);
        list.setPreferredW(leftPadding+preferredWidth+arrow.getPreferredW());
        dialog = new Dialog();
        dialog.setScrollableY(false);
        dialog.getContentPane().getSelectedStyle().setPadding(0, 0, leftPadding, 0);
        dialog.getContentPane().getUnselectedStyle().setPadding(0, 0, leftPadding, 0);
        dialog.addComponent(list);
        cListBox.addComponent(BorderLayout.WEST, libelle);
        cListBox.addComponent(BorderLayout.EAST, arrow);
        cListBox.setPreferredH(preferredHeight);
        getUnselectedStyle().setPadding(Component.LEFT, leftPadding);
        getSelectedStyle().setPadding(Component.LEFT, leftPadding);
        getUnselectedStyle().setBorder(Border.createLineBorder(1));
        getSelectedStyle().setBorder(Border.createLineBorder(1));
        addComponent(cListBox);
        setPreferredH(preferredHeight);
        getUnselectedStyle().setMargin(Component.TOP, topMargin);
        getSelectedStyle().setMargin(Component.TOP, topMargin);
        getUnselectedStyle().setMargin(Component.BOTTOM, bottomMargin);
        getSelectedStyle().setMargin(Component.BOTTOM, bottomMargin);
    }
    public void actionPerformed(ActionEvent ae)
    {
        if ( (ae.getSource() instanceof List) && ((List)ae.getSource()).equals(list) )
        {
            dialog.dispose();
            if (list.getSelectedItem() instanceof Content)
            {
                Content valeur = (Content)list.getSelectedItem();
                selectedData = valeur.getEnreg();
                libelle.setText(selectedData);
                repaint();
            }
        }
    }
    public void setSelectedIndex(int idx)
    {
        list.setSelectedIndex(idx);
        selectedData = String.valueOf(vData.elementAt(idx));
        libelle.setText(String.valueOf(vData.elementAt(idx)));
        repaint();
    }
    public String getSelectedData()
    {
        return selectedData;
    }
    public void pointerPressed(int x, int y)
    {
        int espaceVertRestant, top, bottom, left, right;
        espaceVertRestant = screenHeight - ( libelle.getAbsoluteY() + preferredHeight );
        if (espaceVertRestant > list.getPreferredH())
        {
            top = getAbsoluteY() + preferredHeight - 1 ;
            bottom = screenHeight - ( getAbsoluteY() + preferredHeight + list.getPreferredH() ) ;
        }
        else
        {
            top = screenHeight - ( list.getPreferredH() + preferredHeight + espaceVertRestant ) ;
            bottom = getAbsoluteY() ;
        }
        left = getAbsoluteX() ;
        right = screenWidth - ( getAbsoluteX() + getPreferredW() );
        dialog.show(top, bottom, left, right, false, true);
    }
}

以下是我添加此Form组件的ListBox的代码:

public class ModifierEcheanceForm extends Form implements ActionListener, DataChangedListener {

    private VirtualKeyboard vkNombre = new VirtualKeyboard();
    private String textFieldStatus, listBoxStatus;
    private Container cntnr = new Container();
    private Container c = new Container();
    private Container c1 = new Container();
    private Container c2 = new Container();
    private Container c3 = new Container();
    private Container c4 = new Container();
    private Container c5 = new Container();
    private Container c6 = new Container();
    private Container c7 = new Container();
    private Container c8 = new Container();
    private Container c9 = new Container();
    private Container c10 = new Container(new FlowLayout(Component.CENTER));
    private Container cz1 = new Container();
    private Container cz2 = new Container();
    private Container cz3 = new Container();
    private Container cz4 = new Container();
    private Container cz5 = new Container();
    private TextField t1=new TextField("zzz"),t2=new TextField("zzz"),t3=new TextField("zzz"),t4=new TextField("zzz"),t5=new TextField("zzz");
    private Button modifierBtn, reset;
    private BoxLayout bxl = new BoxLayout(BoxLayout.Y_AXIS);
    private BoxLayout bxltx = new BoxLayout(BoxLayout.X_AXIS);
    private boolean isMenuShown;
    private Command annulerCmd;
    private Command listeMenu, listeClientCmd, listeCreditCmd, ppalCmd;
    private Command ficheMenu, backCmd;
    private SmartPhoneBanking controler;
    private Form backForm, OldbackForm;
    private EcheanceDB echeancedb;
    private Vector listecheance = new Vector();
    private int idEcheance;
    private Label echedatelbl, montantlbl, eche_payelbl, eche_retardlbl, ecehe_nbretardlbl, eche_crdlbl, eche_rembourselbl, eche_interetlbl,flagPayelbl;
    private TextField echedatetxt, montanttxt, eche_payetxt, eche_retardtxt, ecehe_nbretardtxt, eche_crdtxt, eche_remboursetxt, eche_interettxt;
    private ListBox flagPayetxt;   // here is the ListBox
    private CreditDB creditdb = new CreditDB();
    private Vector listeCredit = new Vector();
    private String idClient, idCredit;
    public static int listEcheanceSelectedRow;

    public ModifierEcheanceForm(SmartPhoneBanking ctrl, int idEcheance, Form prevForm)
    {
        super("Modification échéance");
        vkNombre.setInputModeOrder(new String[]{VirtualKeyboard.NUMBERS_SYMBOLS_MODE});
        this.controler = ctrl;
        this.idEcheance = idEcheance;
        backForm = prevForm;
        OldbackForm = FicheCreditForm.backForm;

        echeancedb = new EcheanceDB();
        listecheance = echeancedb.echeanceParId(String.valueOf(idEcheance));

        idCredit = String.valueOf(listecheance.elementAt(5));
        listeCredit = creditdb.listCredit(Integer.parseInt(idCredit));
        idClient = String.valueOf(listeCredit.elementAt(12));

        c.setLayout(bxl);
        c1.setLayout(bxltx);
        c2.setLayout(bxltx);
        c3.setLayout(bxltx);
        c4.setLayout(bxltx);
        c5.setLayout(bxltx);
        c6.setLayout(bxltx);
        c7.setLayout(bxltx);
        c8.setLayout(bxltx);
        c9.setLayout(bxltx);

        echedatelbl = new Label("Date d'échéance");
        echedatelbl.setUIID("FicheLibelle");
        echedatetxt = new TextField();
        echedatetxt.addDataChangeListener(this);
        VirtualKeyboard.bindVirtualKeyboard(echedatetxt, vkNombre);

        montantlbl = new Label("Montant(Ar)");
        montantlbl.setUIID("FicheLibelle");
        montanttxt = new TextField();
        montanttxt.addDataChangeListener(this);
        VirtualKeyboard.bindVirtualKeyboard(montanttxt, vkNombre);

        eche_payelbl = new Label("Payé (Ar)");
        eche_payelbl.setUIID("FicheLibelle");
        eche_payetxt = new TextField();
        eche_payetxt.addDataChangeListener(this);
        VirtualKeyboard.bindVirtualKeyboard(eche_payetxt, vkNombre);

        eche_retardlbl = new Label("Retard");
        eche_retardlbl.setUIID("FicheLibelle");
        eche_retardtxt = new TextField();
        eche_retardtxt.addDataChangeListener(this);
        VirtualKeyboard.bindVirtualKeyboard(eche_retardtxt, vkNombre);

        ecehe_nbretardlbl = new Label("Nombre de retard");
        ecehe_nbretardlbl.setUIID("FicheLibelle");
        ecehe_nbretardtxt = new TextField();
        ecehe_nbretardtxt.addDataChangeListener(this);
        VirtualKeyboard.bindVirtualKeyboard(ecehe_nbretardtxt, vkNombre);

        eche_crdlbl = new Label("Crédit (Ar)");
        eche_crdlbl.setUIID("FicheLibelle");
        eche_crdtxt = new TextField();
        eche_crdtxt.addDataChangeListener(this);
        VirtualKeyboard.bindVirtualKeyboard(eche_crdtxt, vkNombre);

        eche_rembourselbl = new Label("Remboursé (Ar)");
        eche_rembourselbl.setUIID("FicheLibelle");
        eche_remboursetxt = new TextField();
        eche_remboursetxt.addDataChangeListener(this);
        VirtualKeyboard.bindVirtualKeyboard(eche_remboursetxt, vkNombre);

        eche_interetlbl = new Label("Intérêt (Ar)");
        eche_interetlbl.setUIID("FicheLibelle");
        eche_interettxt = new TextField();
        eche_interettxt.addDataChangeListener(this);
        VirtualKeyboard.bindVirtualKeyboard(eche_interettxt, vkNombre);

        flagPayelbl = new Label("Payé");
        flagPayelbl.setUIID("FicheLibelle");
        flagPayetxt = new ListBox(new String[]{"oui","non","aaaaaa","bbbb"},echedatetxt.getPreferredH(),getPreferredW(),getPreferredH(),eche_interettxt.getSelectedStyle().getMargin(Component.TOP),eche_interettxt.getSelectedStyle().getMargin(Component.BOTTOM));

        c10.getStyle().setPadding(Component.LEFT, 0);
        c10.getStyle().setPadding(Component.RIGHT, 0);

        reset = new Button("Rétablir les données");
        reset.addActionListener(this);
        reset.getUnselectedStyle().setMargin(0, 0, 0, 0);
        reset.getSelectedStyle().setMargin(0, 0, 0, 0);
        reset.setAlignment(Label.CENTER);
        reset.setUIID("btnDetailCreditFieldset");
        c10.addComponent(reset);

        modifierBtn = new Button("Valider");
        modifierBtn.addActionListener(this);
        modifierBtn.getUnselectedStyle().setMargin(0, 0, 0, 0);
        modifierBtn.getSelectedStyle().setMargin(0, 0, 0, 0);
        modifierBtn.setAlignment(Label.CENTER);
        modifierBtn.setUIID("btnDetailCreditFieldset");

        c1.addComponent(echedatelbl);
        c1.addComponent(echedatetxt);

        c2.addComponent(montantlbl);
        c2.addComponent(montanttxt);

        c3.addComponent(eche_payelbl);
        c3.addComponent(eche_payetxt);

        c4.addComponent(eche_retardlbl);
        c4.addComponent(eche_retardtxt);

        c5.addComponent(ecehe_nbretardlbl);
        c5.addComponent(ecehe_nbretardtxt);

        c6.addComponent(eche_crdlbl);
        c6.addComponent(eche_crdtxt);

        c7.addComponent(eche_rembourselbl);
        c7.addComponent(eche_remboursetxt);

        c8.addComponent(eche_interetlbl);
        c8.addComponent(eche_interettxt);

        c9.addComponent(flagPayelbl);
        c9.addComponent(flagPayetxt);

        cz1.addComponent(t1);
        cz2.addComponent(t2);
        cz3.addComponent(t3);
        cz4.addComponent(t4);
        cz5.addComponent(t5);

        int[] labelW = new int[]{echedatelbl.getPreferredW(), montantlbl.getPreferredW(), eche_payelbl.getPreferredW(), eche_retardlbl.getPreferredW(), ecehe_nbretardlbl.getPreferredW(), eche_crdlbl.getPreferredW(), eche_rembourselbl.getPreferredW(), eche_interetlbl.getPreferredW(),flagPayelbl.getPreferredW()};
        int largeW = Comparator.max(labelW);
        echedatelbl.setPreferredW(largeW);
        montantlbl.setPreferredW(largeW);
        eche_payelbl.setPreferredW(largeW);
        eche_retardlbl.setPreferredW(largeW);
        ecehe_nbretardlbl.setPreferredW(largeW);
        eche_crdlbl.setPreferredW(largeW);
        eche_rembourselbl.setPreferredW(largeW);
        eche_interetlbl.setPreferredW(largeW);
        flagPayelbl.setPreferredW(largeW+2);

        c10.addComponent(modifierBtn);

        c.addComponent(c1);
        c.addComponent(c2);
        c.addComponent(c3);
        c.addComponent(c4);
        c.addComponent(c5);
        c.addComponent(c6);
        c.addComponent(c7);
        c.addComponent(c8);
        c.addComponent(cz1);
        c.addComponent(cz2);
        c.addComponent(cz3);
        c.addComponent(cz4);
        c.addComponent(cz5);
        c.addComponent(c9);
        c.addComponent(c10);

        cntnr.addComponent(c);
        this.setScrollableY(true);
        this.addComponent(cntnr);

        echedatetxt.setText(listecheance.elementAt(0).toString());
        montanttxt.setText(listecheance.elementAt(1).toString());
        eche_payetxt.setText(listecheance.elementAt(6).toString());
        eche_retardtxt.setText(listecheance.elementAt(7).toString());
        ecehe_nbretardtxt.setText(listecheance.elementAt(8).toString());
        eche_crdtxt.setText(listecheance.elementAt(9).toString());
        eche_remboursetxt.setText(listecheance.elementAt(10).toString());
        eche_interettxt.setText(listecheance.elementAt(11).toString());
        textFieldStatus = "NORMAL";
        if (String.valueOf(listecheance.elementAt(2)).toLowerCase().startsWith("o"))
            flagPayetxt.setSelectedIndex(0);
        else if (String.valueOf(listecheance.elementAt(2)).toLowerCase().startsWith("n"))
            flagPayetxt.setSelectedIndex(1);

        listeMenu      = new Command("Liste");
        listeClientCmd = new Command("Clients");
        listeCreditCmd = new Command("Crédits");
        ppalCmd        = new Command("Menu principal");
        ficheMenu      = new Command("Fiche");
        backCmd        = new Command("Retour");
        annulerCmd     = new Command("Annuler");
        isMenuShown    = false;
        this.addCommand(listeMenu);
        this.addCommand(ficheMenu);
        this.addCommandListener(this);
    } // end of constructor
    ...
}

如果ListBox类的pointerPressed方法中的ListBox下方有空格,您可以看到我已经考虑了两种情况。

当我没有在调用Form的代码中写入容器cz1到cz5的addComponent时,Dialog显示在ListBox以下成分

那么为什么当底部没有更多空格时Dialog显示呢?

1 个答案:

答案 0 :(得分:1)

您调用对话框show方法,其大小不会为对话框留下任何空间。您需要检查height - top - bottom > dialogPreferreHeight是否应该适用宽度。

如果没有下面的空间,请参阅组合框弹出代码,我们有条件地向上弹出对话框。