0

我想知道是否有一种方法或方法可以让我显示当前的 JEditorPane。例如,我有一个 JFrame,可以在其中创建多个选项卡。每当创建选项卡时,都会创建一个新的 JEditorPane 对象,并且该窗格的内容会显示在选项卡中。我已经实现了一个 ChangeListener,当我打开一个新选项卡、关闭一个选项卡或在选项卡之间导航时,它目前只会获取当前选项卡的索引。我想做的是每当打开或导航到新选项卡时,我都想获取驻留在此选项卡上的当前 JEditorPane 对象。有什么方法可以实现吗?

对不起,如果问题有点模糊。

提前致谢。

4

1 回答 1

1

做到这一点的最好方法是子类化JPanel并将您的自定义添加JPanel到选项卡式窗格中:

public class EditorPanel extends JPanel {

    private JEditorPane editorPane;

    // ...

    public EditorPanel() {
        // ...
        editorPane = new JEditorPane( ... );
        super.add(editorPane);
        // ...
    }

    // ...

    public JEditorPane getEditorPane() {
        return editorPane;
    }
}

添加新选项卡:

JTabbedPane tabbedPane = ... ;
tabbedPane.addTab(name, icon, new EditorPanel());

然后当您需要使用选项卡式窗格访问它时:

Component comp = tabbedPane.getComponentAt(i);
if (comp instanceof EditorPanel) {
    JEditorPane editorPane = ((EditorPanel) comp).getEditorPane();
}

这是维护单独列表并尝试将其与选项卡式窗格的索引一起维护的更好选择。

于 2012-12-07T18:50:00.927 回答