我不记得在How to Use Tabbed Panes中讨论过的TabComponentsDemo中遇到过这样的问题.您可以将代码与该示例进行比较,以作为参考.
附录:将ButtonTabComponent重构为包含getLabel(),此版本的TabComponentsDemo中的runTest()会添加一个按钮,以表明所需的行为.特别是,每次按下按钮时,将重新绘制选项卡以显示放大的标题.
更新:在pane.remove()之后修改正确的选项卡组件.
public void runTest() {
pane.removeAll();
for (int i = 0; i < tabNumber; i++) {
final int titleIndex = i;
String title = "Tab " + titleIndex;
final JButton button = new JButton("Relabel tab");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int index = pane.indexOfComponent(button);
ButtonTabComponent btc = (ButtonTabComponent)
pane.getTabComponentAt(index);
JLabel label = btc.getLabel();
pane.setTitleAt(index, label.getText() + titleIndex);
label.invalidate();
pane.repaint();
}
});
pane.add(title, button);
initTabComponent(i);
}
tabComponentsItem.setSelected(true);
pane.setTabLayoutPolicy(JTabbedPane.WRAP_TAB_LAYOUT);
scrollLayoutItem.setSelected(false);
this.setPreferredSize(new Dimension(500, 200));
this.pack();
this.setLocationRelativeTo(null);
this.setVisible(true);
}