我使用lanterna创建了一个简单的终端应用程序,只显示一个自定义按钮。但即使在自定义按钮的类中,我扩展Button类并覆盖Button类的'createDefaultRenderer方法以返回Button类'FlatButtonRenderer类的实例,它正在使用DefaultButtonRenderer类显示按钮。
你能帮我理解如何在lanterna中创建一个使用FlatButtonRenderer显示按钮的自定义按钮吗?
import java.io.IOException;
import java.util.ArrayList;
import com.googlecode.lanterna.terminal.*;
import com.googlecode.lanterna.screen.*;
import com.googlecode.lanterna.gui2.*;
class Test {
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 GUIAppWindow();
textGUI.addWindowAndWait(window);
}
catch (IOException e) {
e.printStackTrace();
}
finally {
if(screen != null) {
try {
screen.stopScreen();
}
catch(IOException e) {
e.printStackTrace();
}
}
}
}
private static class GUIAppWindow extends BasicWindow {
GUIAppWindow() {
ArrayList<Window.Hint> hints = new ArrayList<>();
hints.add(Window.Hint.CENTERED);
setHints(hints);
Panel mainPanel = new Panel(new LinearLayout(Direction.VERTICAL));
XButton b = new XButton(new String("."));
b.addListener(new ButtonHandler("data"));
mainPanel.addComponent(b);
this.setComponent(mainPanel);
}
private class XButton extends Button {
public XButton(String label) {
super(label);
}
@Override
protected ButtonRenderer createDefaultRenderer() {
return new Button.FlatButtonRenderer();
}
}
private class ButtonHandler implements Button.Listener {
final String loc;
ButtonHandler(String loc) {
this.loc = loc;
}
public void onTriggered(Button button) {
button.setLabel(button.getLabel().equals(".") ? "x" : new String("."));
}
}
}
}
答案 0 :(得分:0)
我相信使用FlatButtonRenderer的正确方法是调用setRenderer()。
private class XButton extends Button {
public XButton(String label) {
super(label);
setRenderer(new Button.FlatButtonRenderer());
}
}