0

我正在体验 lanterna 的第 4 个教程。当我创建具有 2 列的 GridLayout 并添加 2 个标签时,每个标签跨越 2 列,程序运行时没有错误。但是,当我使用 4 列创建 GridLayout 并添加 2 个跨越 4 列的标签时,我得到以下 ArrayIndexOutOfBoundsException 运行时错误。

[rmcgregor@cs lanterna]$ make 4
java -cp .:lanterna-3.0.0.jar GUIApp

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    at com.googlecode.lanterna.gui2.GridLayout.getPreferredColumnWidths(GridLayout.java:542)
    at com.googlecode.lanterna.gui2.GridLayout.getPreferredSize(GridLayout.java:387)
    at com.googlecode.lanterna.gui2.Panel$1.getPreferredSize(Panel.java:187)
    at com.googlecode.lanterna.gui2.Panel$1.getPreferredSize(Panel.java:182)
    at com.googlecode.lanterna.gui2.AbstractComponent.calculatePreferredSize(AbstractComponent.java:194)
    at com.googlecode.lanterna.gui2.Panel.calculatePreferredSize(Panel.java:217)
    at com.googlecode.lanterna.gui2.AbstractComponent.getPreferredSize(AbstractComponent.java:178)
    at com.googlecode.lanterna.gui2.AbstractBasePane$ContentHolder$1.getPreferredSize(AbstractBasePane.java:331)
    at com.googlecode.lanterna.gui2.AbstractBasePane$ContentHolder$1.getPreferredSize(AbstractBasePane.java:324)
    at com.googlecode.lanterna.gui2.AbstractComponent.calculatePreferredSize(AbstractComponent.java:194)
    at com.googlecode.lanterna.gui2.AbstractComponent.getPreferredSize(AbstractComponent.java:178)
    at com.googlecode.lanterna.gui2.AbstractWindow.getPreferredSize(AbstractWindow.java:161)
    at com.googlecode.lanterna.gui2.DefaultWindowManager.onAdded(DefaultWindowManager.java:103)
    at com.googlecode.lanterna.gui2.MultiWindowTextGUI.addWindow(MultiWindowTextGUI.java:356)
    at com.googlecode.lanterna.gui2.MultiWindowTextGUI.addWindowAndWait(MultiWindowTextGUI.java:369)
    at GUIApp.main(GUIApp.java:50)

这是源代码。

import java.io.IOException;

import com.googlecode.lanterna.terminal.DefaultTerminalFactory;
import com.googlecode.lanterna.screen.Screen;
import com.googlecode.lanterna.gui2.*;

class GUIApp {
    public static void main(String[] args) {

        DefaultTerminalFactory terminalFactory = new DefaultTerminalFactory();
        Screen screen = null;

        try {
            screen = terminalFactory.createScreen();
            screen.startScreen();

            final WindowBasedTextGUI textGUI = new MultiWindowTextGUI(screen);
            final Window window = new BasicWindow();

            Panel contentPanel = new Panel(new GridLayout(4));

            GridLayout gridLayout = (GridLayout)contentPanel.getLayoutManager();
            gridLayout.setHorizontalSpacing(3);

            Label title = new Label("This is a label that spans two columns");
            title.setLayoutData(GridLayout.createLayoutData(
                GridLayout.Alignment.BEGINNING,
                GridLayout.Alignment.BEGINNING,
                true,
                false,
                4,
                1));
            contentPanel.addComponent(title);

            Label alignedText = new Label("Text Box (aligned)");
            alignedText.setLayoutData(GridLayout.createLayoutData(
                GridLayout.Alignment.BEGINNING,
                GridLayout.Alignment.BEGINNING,
                true,
                false,
                4,
                1));
            contentPanel.addComponent(alignedText);

            //Label moreText = new Label("more text");
            //contentPanel.addComponent(moreText);

            window.setComponent(contentPanel);
            textGUI.addWindowAndWait(window);
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            if(screen != null) {
                try {
                    screen.stopScreen();
                }
                catch(IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

任何帮助解释我在概念上的误解都会很棒!

4

1 回答 1

0

我一直在做一些测试,并认为我在 GridLayout 中发现了一个错误。我已经在lanterna google groups 论坛上报告了它。

您似乎无法将跨越多个列的按钮或标签添加到第一行。它们可以包含在第一行之后的任何行中。有趣的是,您可以在第一行 EmptySpace 中添加跨越多列的内容。

这是我的测试用例。希望他们帮助理解这个问题。

    private static class GUIAppWindow extends BasicWindow {
            GUIAppWindow() { 

                    Panel contentPanel = new Panel(new GridLayout(4)); 
                    GridLayout gridLayout = (GridLayout)contentPanel.getLayoutManager(); 
                    gridLayout.setHorizontalSpacing(3);

                    // Valid First Row ***************************************************** 
                    contentPanel.addComponent(new Label("AAAAA")); 
                    contentPanel.addComponent(new Label("BBBBB")); 
                    contentPanel.addComponent(new Label("DDDDD")); 
                    contentPanel.addComponent(new Label("EEEEE")); 

                    // Valid First Row ***************************************************** 
                    contentPanel.addComponent( 
                            new EmptySpace() 
                                    .setLayoutData( 
                                            GridLayout.createHorizontallyFilledLayoutData(3))); 
                    contentPanel.addComponent(new Label("HHHHH")); 

                    // Valid First Row ***************************************************** 
                    contentPanel.addComponent(new Label("HHHHH")); 
                    contentPanel.addComponent( 
                            new EmptySpace() 
                                    .setLayoutData( 
                                            GridLayout.createHorizontallyFilledLayoutData(2))); 
                    contentPanel.addComponent(new Label("IIIII")); 

                    // Valid First Row ***************************************************** 
                    contentPanel.addComponent(new Label("BBBBB")); 
                    contentPanel.addComponent(new Label("DDDDD")); 
                    contentPanel.addComponent(new Label("EEEEE")); 
                    contentPanel.addComponent(new Button("Exit", new Runnable() { 
                            @Override 
                            public void run() { 
                                    GUIAppWindow.this.close(); 
                            } 
                    })); 

                    // Invalid First Row ***************************************************** 
                    contentPanel.addComponent(new Label("KKKKK")); 
                    contentPanel.addComponent(new Label("LLLLL")); 
                    contentPanel.addComponent( 
                            new Button("Close", new Runnable() { 
                                    @Override 
                                    public void run() { 
                                            GUIAppWindow.this.close(); 
                                    } 
                    }), GridLayout.createHorizontallyEndAlignedLayoutData(2)); 

                    // Invalid First Row ***************************************************** 
                    contentPanel.addComponent( 
                            new EmptySpace() 
                                    .setLayoutData( 
                                            GridLayout.createHorizontallyFilledLayoutData(2))); 
                    contentPanel.addComponent(new Label("CCCCC"), GridLayout.createLayoutData( 
                            GridLayout.Alignment.CENTER, 
                            GridLayout.Alignment.BEGINNING, 
                            false, 
                            false, 
                            2, 
                            1)); 

                    // Invalid First Row ***************************************************** 
                    contentPanel.addComponent(new Label("FFFFF"), GridLayout.createLayoutData( 
                            GridLayout.Alignment.CENTER, 
                            GridLayout.Alignment.BEGINNING, 
                            true, 
                            false, 
                            4, 
                            1)); 

                    // Invalid First Row ***************************************************** 
                    contentPanel.addComponent(new Label("DDDDD")); 
                    contentPanel.addComponent(new Label("EEEEE")); 
                    contentPanel.addComponent(new Label("FFFFF"), GridLayout.createLayoutData( 
                            GridLayout.Alignment.CENTER, 
                            GridLayout.Alignment.BEGINNING, 
                            true, 
                            false, 
                            2, 
                            1)); 

                    this.setComponent(contentPanel); 
            } 
    }
于 2018-01-16T03:44:26.437 回答