我正在体验 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();
}
}
}
}
}
任何帮助解释我在概念上的误解都会很棒!