我想知道是否有一种方式或方法可以让我获取当前显示的 JEditorPane。例如,我有一个 JFrame,可以在其中创建多个选项卡。每当创建选项卡时,都会创建一个新的 JEditorPane 对象,并且该 Pane 的内容将显示在选项卡中。我已经实现了一个 ChangeListener,目前每当我打开一个新选项卡、关闭一个选项卡或在选项卡之间导航时,它都会获取当前选项卡的索引。我想要做的是,每当打开或导航到新选项卡时,我都想获取驻留在该选项卡上的当前 JEditorPane 对象。有什么方法可以实现这一目标吗?
抱歉,如果问题有点模糊。
提前致谢。
最佳答案
执行此操作的最佳方法是子类化 JPanel
并将自定义 JPanel
添加到选项卡式 Pane :
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());
然后当您需要使用选项卡式 Pane 访问它时:
Component comp = tabbedPane.getComponentAt(i);
if (comp instanceof EditorPanel) {
JEditorPane editorPane = ((EditorPanel) comp).getEditorPane();
}
这是维护单独列表并尝试将其与选项卡式 Pane 的索引一起维护的更好替代方案。
关于java - 获取当前正在显示的 JEditorPane,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13768818/