2

我对事件监听和 GUI 很陌生,所以很难弄清楚这一点。

我有一个 JTabbedPane,我在其中添加了 3 个组件。这些组件是 JToolBars,它允许我将它们从 JTabbedPane 拖到浮动窗口中。这将从选项卡式窗格中删除选项卡。当我将 JToolBar 拖回窗格时,将重新创建该选项卡。但是,它现在的名称不正确。该名称对应于 ToolBar 停靠在 TabbedPane 的哪一侧;北、南、东或西。

任何人都可以推荐一种检测 JToolBar 已重新停靠然后更新选项卡标题的体面方法吗?到目前为止,我已经在 tabbedPane 上实现了一个更改侦听器,但无法找到合适的事件。

干杯。

已解决: 解决方案是使用 ContainerListener 来检测通过实现的 componentAdded 方法添加的组件。当一个组件被添加到 JTabbedPane 时,我调用了一个方法来使用组件名称更新选项卡名称,通过 .setName() 设置。

public void componentAdded(ContainerEvent added) {
    updateTabs();
}

public void updateTabs() {
    for (int i = 0; i < tabbedPane.getComponents().length; i++) {
        tabbedPane.setTitleAt(i,
                tabbedPane.getComponents()[i].getName());
    }
}
4

1 回答 1

1

解决方案是使用 ContainerListener 来检测通过实现的 componentAdded 方法添加的组件。当一个组件被添加到 JTabbedPane 时,我调用了一个方法来使用组件名称更新选项卡名称,通过 .setName() 设置。

public void componentAdded(ContainerEvent added) {
    updateTabs();
}

public void updateTabs() {
    for (int i = 0; i < tabbedPane.getComponents().length; i++) {
        tabbedPane.setTitleAt(i,
                tabbedPane.getComponents()[i].getName());
    }
}
于 2012-11-18T10:08:32.970 回答