1

我需要多行的切换按钮,因为其中一些的文本很长。我试图扩展 CheckBox 以创建一个 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;
    }

}
4

1 回答 1

1

我用不同的方法解决了这个问题。在代码中,我用代码行替换:

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);

通过这种方式,我得到了一个类似于 Toggle Button 的 Container,其中包含一个带有多行文本的 SpanLabel。

于 2018-04-27T14:40:17.047 回答