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
MultiWindowTextGUIonly acceptsWindowinstances. 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, andscreen.stopScreen()in afinallyblock to reset the terminal when you're done. - Window interaction: Use
addWindowAndWaitif you want your code to pause until the user closes the window. If you need to run other logic while the window is open, useaddWindowinstead.
This should get your checkbox list showing up in the terminal GUI as expected!
内容的提问来源于stack exchange,提问作者George S.
相关产品推荐
相关产品推荐

