You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Lanterna技术咨询:如何创建简单复选框菜单并添加至MultiWindowTextGUI?

Solution for Adding CheckBoxList to MultiWindowTextGUI

Got it, let's sort this out for you! The core issue here is that MultiWindowTextGUI is built to manage windows, not individual UI components directly. You need to wrap your CheckBoxList inside a window first, then add that window to the GUI. Here's how to adjust your code:

import com.googlecode.lanterna.gui2.*;
import com.googlecode.lanterna.screen.TerminalScreen;
import com.googlecode.lanterna.terminal.DefaultTerminalFactory;
import com.googlecode.lanterna.terminal.Terminal;

public class CheckBoxListDemo {
    public static void main(String[] args) throws Exception {
        Terminal terminal = new DefaultTerminalFactory().createTerminal();
        TerminalScreen screen = new TerminalScreen(terminal);
        MultiWindowTextGUI mwtg = new MultiWindowTextGUI(screen);
        
        CheckBoxList<String> checkBoxList = new CheckBoxList<>();
        checkBoxList.addItem("Check one");
        checkBoxList.addItem("Check two");

        // Step 1: Wrap your CheckBoxList in a Window
        BasicWindow listWindow = new BasicWindow("My Checkbox List");
        listWindow.setComponent(checkBoxList); // Set the list as the window's main content

        // Step 2: Start the screen and show the window
        screen.startScreen();
        try {
            mwtg.addWindowAndWait(listWindow); // Blocks until the window is closed
        } finally {
            screen.stopScreen(); // Clean up the terminal properly
        }
    }
}

Key Details to Remember:

  • Windows are required: Lanterna's MultiWindowTextGUI only accepts Window instances. All UI components (like your checkbox list) need to be nested inside a window to be rendered.
  • Screen lifecycle: Always call screen.startScreen() before displaying your GUI, and screen.stopScreen() in a finally block to reset the terminal when you're done.
  • Window interaction: Use addWindowAndWait if you want your code to pause until the user closes the window. If you need to run other logic while the window is open, use addWindow instead.

This should get your checkbox list showing up in the terminal GUI as expected!

内容的提问来源于stack exchange,提问作者George S.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 15:48:10