当我尝试在其他标签之间插入标签时,遇到JTabbedPane
的奇怪问题。 UI呈现了预期的样子,但是getComponentAt()
返回了错误的组件。
在我的应用程序中,用户应该可以按自己想要的任何顺序重新排列标签。为此,我使用insertTab()
方法插入标签。这样做时,标签会消失,其余标签的内容有误。
import java.awt.Component;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTabbedPane;
public class InsertTabExample extends JFrame {
public InsertTabExample() {
final JTabbedPane pane = new JTabbedPane();
addTabsInReverseOrder(pane);
dumpTabs(pane);
add(pane);
this.setSize(640, 480);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
private void addTabsInReverseOrder(final JTabbedPane pane) {
for(int i = 0; i < 10; i++) {
final String tabLabel = String.format("tab #%d", i);
final String componentLabel = String.format("component #%d", i);
final JLabel component = new JLabel(componentLabel);
component.setName(componentLabel);
pane.insertTab(tabLabel, null, component, null, 0);
}
}
private void dumpTabs(final JTabbedPane pane) {
for(int i = 0; i < pane.getTabCount(); i++) {
final String tabName = pane.getTitleAt(i);
final Component component = pane.getComponent(i);
final int componentIndex = pane.indexOfComponent(component);
System.out.println(String.format("Index %d -> tab '%s' -> component '%s' -> component index %d", i, tabName, component.getName(), componentIndex));
}
}
public static void main(String[] args) {
new InsertTabExample();
}
}
如预期的那样,用户界面以相反的顺序显示选项卡(从“选项卡#9”开始),并且还向每个选项卡显示正确的组件。
控制台上的输出显示不同的内容。我希望得到
Index 0 -> tab 'tab #9' -> component 'component #9' -> component index 9 Index 1 -> tab 'tab #8' -> component 'component #8' -> component index 8 Index 2 -> tab 'tab #7' -> component 'component #7' -> component index 7 Index 3 -> tab 'tab #6' -> component 'component #6' -> component index 6 Index 4 -> tab 'tab #5' -> component 'component #5' -> component index 5 Index 5 -> tab 'tab #4' -> component 'component #4' -> component index 4 Index 6 -> tab 'tab #3' -> component 'component #3' -> component index 3 Index 7 -> tab 'tab #2' -> component 'component #2' -> component index 2 Index 8 -> tab 'tab #1' -> component 'component #1' -> component index 1 Index 9 -> tab 'tab #0' -> component 'component #0' -> component index 0
但是我得到的是
Index 0 -> tab 'tab #9' -> component 'component #0' -> component index 9 Index 1 -> tab 'tab #8' -> component 'component #1' -> component index 8 Index 2 -> tab 'tab #7' -> component 'component #2' -> component index 7 Index 3 -> tab 'tab #6' -> component 'component #3' -> component index 6 Index 4 -> tab 'tab #5' -> component 'component #4' -> component index 5 Index 5 -> tab 'tab #4' -> component 'component #5' -> component index 4 Index 6 -> tab 'tab #3' -> component 'component #6' -> component index 3 Index 7 -> tab 'tab #2' -> component 'component #7' -> component index 2 Index 8 -> tab 'tab #1' -> component 'component #8' -> component index 1 Index 9 -> tab 'tab #0' -> component 'component #9' -> component index 0
按预期顺序返回选项卡标签,但按相反顺序列出组件。
这是怎么回事?如何正确获取特定选项卡的组件?
感谢任何提示!
答案 0 :(得分:1)
TabbedPane
保持索引从0
到getTabCount()
的顺序-因此最后一个标签不能为0
-因此,当您添加标签时,请放置正确的索引:{{1 }}
getTabCount()-i
另一个错误- private void addTabsInReverseOrder(final JTabbedPane pane) {
for(int i = 0; i < 10; i++) {
final String tabLabel = String.format("tab #%d", i);
final String componentLabel = String.format("component #%d", i);
final JLabel component = new JLabel(componentLabel);
component.setName(componentLabel);
pane.insertTab(tabLabel, null, component, null, pane.getTabCount()-i);
}
}
private void dumpTabs(final JTabbedPane pane) {
for(int i = 0; i < pane.getTabCount(); i++) {
final String tabName = pane.getTitleAt(i);
final Component component = pane.getComponentAt(i);
final int componentIndex = pane.indexOfComponent(component);
System.out.println(String.format("Index %d -> tab '%s' -> component '%s' -> component index %d", i, tabName, component.getName(), componentIndex));
}
}
获取此容器中的第n个组件。
并且您需要替换为pane.getComponent(i)
,它会返回特定索引处的组件。
输出将是:
pane.getComponentAt(i)
因此选项卡和组件的索引将不会相反-它的索引从0到Index 0 -> tab 'tab #9' -> component 'component #9' -> component index 0
Index 1 -> tab 'tab #8' -> component 'component #8' -> component index 1
Index 2 -> tab 'tab #7' -> component 'component #7' -> component index 2
Index 3 -> tab 'tab #6' -> component 'component #6' -> component index 3
Index 4 -> tab 'tab #5' -> component 'component #5' -> component index 4
Index 5 -> tab 'tab #4' -> component 'component #4' -> component index 5
Index 6 -> tab 'tab #3' -> component 'component #3' -> component index 6
Index 7 -> tab 'tab #2' -> component 'component #2' -> component index 7
Index 8 -> tab 'tab #1' -> component 'component #1' -> component index 8
Index 9 -> tab 'tab #0' -> component 'component #0' -> component index 9
。