【问题标题】:Scrolling text in Dialog not working对话框中的滚动文本不起作用
【发布时间】:2017-03-30 06:45:08
【问题描述】:

我正在使用以下代码创建一个对话框:

Dialog dlg = new Dialog();
dlg.setUIID("AboutDialog");
dlg.setTitle("About");
dlg.setScrollableY(true);
dlg.setScrollVisible(true);
dlg.setLayout(new BorderLayout());
SpanLabel spl = new SpanLabel(DialogText.aboutTxt[txtItem]);
dlg.addComponent(BorderLayout.CENTER, spl);
height = screenHorizontalSize / 9;
width = screenVerticalSize / 10;
Button close = new Button("Close");
close.addActionListener((ee) -> dlg.dispose());
dlg.addComponent(BorderLayout.SOUTH, close);
dlg.show(height, height, width, width);

DialogText 包含几行需要在低分辨率设备上滚动的行,但上面的代码没有实现这一点。我错过了什么?还尝试使 SpanLabel 可滚动,但没有奏效。 刚刚将此项目从 Eclipse 移至 Netbeans(对此是新的)。使用旧的 GUI 构建器,但不适用于此对话框。

【问题讨论】:

    标签: java codenameone


    【解决方案1】:

    我想我找到了答案 - 使用 Dialog 中的 show 方法,如下所示:

    Dialog dlg = new Dialog();
    dlg.setUIID("AboutDialog");
    String title = "About";
    String txt = DialogText.aboutTxt[txtItem];
    dlg.setLayout(new BorderLayout());
    dlg.setScrollableY(true);
    dlg.setScrollVisible(true);
    dlg.show(title, txt, Dialog.TYPE_INFO, logo_icon, "", "Close");
    

    需要进行一些调整,但现在可以滚动滚动。 如果我浪费了任何人的时间,请道歉。

    后来:无法“调整”上面的代码,所以如果它对其他人有帮助,我终于在对话框中得到了可滚动的文本,使用:

    String title = DialogText.getTitleUIID(txtItem);
    String txt = DialogText.dialogTxt[txtItem];
    
    Dialog dlg = new Dialog();
    dlg.setTitle(title);
    dlg.setLayout(new BorderLayout());
    TextArea txtArea = new TextArea(txt);
    txtArea.setScrollVisible(true);
    dlg.add(BorderLayout.CENTER, txtArea);
    Button close = new Button("Close");
    close.addActionListener((ee) -> dlg.dispose());
    dlg.addComponent(BorderLayout.SOUTH, close);
    dlg.showAtPosition(0, 0, 0, 0, true);
    

    【讨论】:

    • 这里有几个错误。 setScrollableY(true) 将被不允许滚动的BorderLayout 否定。您可以将内容容器放置在边框布局的中心并使其可滚动或使用 Box Y 容器。第一个代码示例创建一个对话框实例,然后使用 Dialog 的静态方法来显示一个完全不同的实例。最后一个示例有效,因为TextArea 默认情况下可在 Y 轴上滚动,因此它接管了滚动任务。
    猜你喜欢
    • 2018-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-17
    • 2011-07-10
    • 1970-01-01
    相关资源
    最近更新 更多