我已經reccently遇到索引我的選項卡問題,雖然我會給它一些具體的使用setComponentAt
方法的順序。這裏是我的代碼:給JTabbedPane訂單
public ContainerPane() {
this.setLayout(new BorderLayout());
myPlayerManagerPane = new PlayerManagerPane();
myGameManagerPane = new GameManagerPane();
myCharacterManagerPane = new CharacterManagerPane();
myPaneTab = new JTabbedPane(JTabbedPane.TOP);
myPaneTab.addTab("Character",myCharacterManagerPane);
myPaneTab.addTab("Player",myPlayerManagerPane);
myPaneTab.addTab("Games",myGameManagerPane);
System.out.println(myPaneTab.getTabCount());
//myPaneTab.setEnabledAt(1, false);
//myPaneTab.setEnabledAt(2, false);
myPaneTab.setComponentAt(0, myPlayerManagerPane);
myPaneTab.setMnemonicAt(0, KeyEvent.VK_1);
myPaneTab.setComponentAt(1, myCharacterManagerPane);
myPaneTab.setMnemonicAt(1, KeyEvent.VK_2);
myPaneTab.setComponentAt(2, myGameManagerPane);<---outOfBoundsException
myPaneTab.setMnemonicAt(2, KeyEvent.VK_3);
add(myPaneTab);
}
所以對我有計,3個標籤(根據我,getTabCount()
),而我從0開始計數(正確嗎?)。我將最後一個索引設置爲我擁有的最後一個組件。但我仍然有這種印刷屏幕:
3 <---from tabCount
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 2 >= 2
我在哪裏跳了起來,有沒有更容易的方法來訂購我的窗格?
編輯:註釋掉setComponent
方法,並把在for循環中,我得到這樣的輸出:
有3個標籤!在0
Tab是性格
選項卡1是玩家
在2 Tab是遊戲
,每次uncommmenting一個對方法,我只得到2,一個我所不被覆蓋,以及我現在設置的其中一個。
是setComponentAt
刪除重複?我的設置應該少於3個標籤嗎? JTabbedPanel對重複窗格有奇怪的行爲嗎?
這很好,但我有問題不確定標籤索引是什麼我的選項卡......這就是爲什麼我要給*索引。 – Pureferret 2012-07-31 09:43:07
只有當您像上面那樣添加選項卡並在代碼的其餘部分中修改順序時,請問標籤窗格中的索引:'int indexOfGame = tabbedPane.indexOfTabComponent(myGameManagerPane);'。這將允許只在一個地方重新安排標籤。其餘的代碼將保持不變。 – 2012-07-31 09:53:51