Codename One模拟器中新增Form左滚致空白屏问题求助
问题解决:Codename One 第二个Form滚动出屏幕空白
你的问题出在Form的重复创建与显示逻辑冲突上:你定义的MoveByDrag本身是Form的子类,但类内部又额外创建了独立的playfield Form实例,构造函数里直接调用playfield.show(),同时外部代码又调用mbd.show()——这种双重Form操作会导致显示逻辑混乱,引发滚动空白问题。而且你继承的MoveByDrag实例本身未配置任何内容,反而依赖内部的playfield,不符合Codename One的Form使用规范。
修正后的代码
启动屏调用代码(无需修改)
// Call on Splash Screen public boolean openField2() throws IOException { Form mbd = new MoveByDrag(); mbd.show(); return false; }
修正后的MoveByDrag类
package com.kcconsulting.soccersession.ui; import com.codename1.components.ScaleImageLabel; import com.codename1.ui.*; import com.codename1.ui.layouts.*; import com.codename1.io.Log; import java.io.IOException; import static com.codename1.ui.CN.getCurrentForm; import static com.codename1.ui.layouts.BoxLayout.Y_AXIS; import com.codename1.ui.table.TableLayout; public class MoveByDrag extends Form { Container controls = new Container(); Container field = new Container(); Image img30; public MoveByDrag() throws IOException { // 直接使用当前Form实例(this),不再创建额外的playfield TableLayout t2 = new TableLayout(2, 1); this.setLayout(t2); // 配置当前Form的工具栏 this.getToolbar().addMaterialCommandToSideMenu("Design by Coordinates", FontImage.MATERIAL_CHECK, 4, e -> { // Rebuild Menu after stopping Scroll to blank screen }); // 设置背景图片 field.getAllStyles().setBgImage(Image.createImage("/BlankField.jpg")); // 向当前Form添加组件 this.add(t2.createConstraint().heightPercentage(10).widthPercentage(100), controls); this.add(t2.createConstraint().heightPercentage(90).widthPercentage(100), field); // 移除构造函数内的show()调用,由外部统一控制显示 } //Build Drag and Drop /* public void drop(Component dragged, int x, int y) { int i = this.getComponentIndex(dragged); if(i > -1) { Component dest = this.getComponentAt(x, y); if(dest != dragged) { int destIndex = this.getComponentIndex(dest); if(destIndex > -1 && destIndex != i) { // this.setComponentIndex(dragged,destIndex); } } this.animateLayout(400); } else { Container oldParent = dragged.getParent(); if(oldParent != null) { oldParent.removeComponent(dragged); } Component pos = this.getComponentAt(x, y); i = this.getComponentIndex(pos); if(i > -1) { this.addComponent(i, dragged); } else { this.addComponent(dragged); } this.getComponentForm().animateHierarchy(400); } } */ }
额外检查点
- 确认
/BlankField.jpg图片存在于项目的src/main/resources目录下,避免因图片加载失败导致空白 - 如果需要禁用Form的滚动,可以添加
this.setScrollable(false);到构造函数中,防止意外滚动 - 检查TableLayout的约束是否符合预期,确保组件占满屏幕宽度,不会出现横向溢出
内容的提问来源于stack exchange,提问作者curtjacobs1
相关产品推荐
相关产品推荐

