6

我有以下代码:

JTabbedPane container;
...
AWindow page = WinUtils.buildWindow();
boolean existing = checkIfExists(page); // in this code, this will always be false
if(!existing)
{
    String tabName = page.getLoadedFileLocation().getName();
    container.addTab(page.getLoadedFileLocation().getName(), page);
}
Component comp = container.getTabComponentAt(0);
int sel = container.getSelectedIndex();
container.setSelectedComponent(page);

事情是 :

container.getTabComponentAt(0)

返回null。另一个奇怪的事情是:

container.getSelectedIndex()

返回0。我认为应该发生的合乎逻辑的事情是引用创建的窗口。为什么我会收到null?我究竟做错了什么?

4

2 回答 2

21

getTabComponentAt()返回您可能添加为选项卡标题的自定义组件。您可能正在寻找getComponentAt()返回选项卡内容的方法。只是返回当前选择了第getSelectedIndex()一个选项卡(如果没有选择选项卡,它将返回 -1)

于 2009-06-12T20:05:55.157 回答
8

您混淆了中的两组方法JTabbedPane:选项卡组件方法和组件方法。

getTabComponentAt(0)正在返回null,因为您尚未设置选项卡组件。您已设置在索引 0 处显示的组件,但选项卡组件是呈现选项卡的组件,而不是显示在窗格中的组件。

(注意Javadocs中的示例:

// In this case the look and feel renders the title for the tab.
tabbedPane.addTab("Tab", myComponent);
// In this case the custom component is responsible for rendering the
// title of the tab.
tabbedPane.addTab(null, myComponent);
tabbedPane.setTabComponentAt(0, new JLabel("Tab"));

后者通常用于需要在选项卡上自定义组件的更复杂的用户交互。例如,您可以提供一个自定义组件,该组件具有动画效果,或者具有用于关闭选项卡的小部件。

通常,您不需要弄乱选项卡组件。)

无论如何,试试吧getComponentAt(0)

于 2009-06-12T20:06:42.643 回答