我需要多行的切换按钮,因为其中一些文本很长。我尝试扩展复选框以创建一个spantogglecheckbox,但它不起作用,因为没有显示文本。以下代码中有什么问题?我该如何实现目标?
public class SpanToggleCheckBox extends CheckBox {
private TextArea textArea;
/**
* Constructs a toggle checkbox with the given text
*
* @param text to display
*/
public SpanToggleCheckBox(String text) {
textArea = new TextArea(getUIManager().localize(text, text));
textArea.setActAsLabel(true);
textArea.setColumns(textArea.getText().length() + 1);
textArea.setUIID("Label");
textArea.setEditable(false);
textArea.setFocusable(false);
this.setUIID("CheckBox");
super.setToggle(true);
}
/**
* {@inheritDoc}
*/
@Override
public void paint(Graphics g) {
getUIManager().getLookAndFeel().drawTextArea(g, textArea);
}
/**
* {@inheritDoc}
*/
@Override
protected Dimension calcPreferredSize() {
return getUIManager().getLookAndFeel().getTextAreaSize(textArea, true);
}
/**
* Shorthand for creating the SpanToggleCheckBox
*
* @param text the text for the button
* @return a check box
*/
public static SpanToggleCheckBox createToggle(String text) {
SpanToggleCheckBox cb = new SpanToggleCheckBox(text);
return cb;
}
}
我用不同的批准解决了这个问题。在代码中,我替换为代码行:
Component button = CheckBox.createToggle(label);
使用这些行:
CheckBox myBtn = CheckBox.createToggle("text");
Container container = new Container(BoxLayout.y());
container.setLeadComponent(myBtn);
SpanLabel multiLineText = new SpanLabel(label);
container.add(multiLineText);
以这种方式,我得到了一个像切换按钮一样的容器,并且包含带有多行文本的Spanlabel。