0
我添加了一個名爲MyEditorPanel的自定義面板,它將JPanel擴展到JTabbedPanel,並且希望每個選項卡上都有一個關閉按鈕,以便我使用以下代碼並使用ButtonTabbedComponent.java從ButtonTabbedComponent.java在調用setComponentAt()後無法將面板添加到jtabbed窗格()
public void addTab(String name, String desc) {
MyEditorPane textEditorPane = new MyEditorPane(this, name, desc);
this.addTab(name, normal, textEditorPane, desc);
int i = this.getComponentCount() - 1;
textEditorPane.setTabCount(i);
this.setTabComponentAt(i, new ButtonTabComponent(this, normal, normalFont));
}
,但我想保存MyEditorPane文本關閉我怎樣才能得到它MyEditorPane正在將關閉在關閉之前單擊
當我得到JTabbedPane的組件的名稱類沒有MyEditorPane類對象
方法@Override
public void remove(int index) {
Component component = this.getTabComponentAt(index);
if (component instanceof ButtonTabComponent) {
ButtonTabComponent tab = (ButtonTabComponent) component;
System.out.println("remove method called if : " + tab.getComponentCount());
component = tab.getComponent(0);
System.out.println("remove method called if : " + component.getClass().getName());
component = tab.getComponent(1);
System.out.println("remove method called if : " + component.getClass().getName());
JPanel pane = (JPanel) component;
Component[] components = pane.getComponents();
for (int i = 0, l = components.length; i < l; i++) {
System.out.println("remove method called for : " + components[i].getClass().getName());
}
super.remove(index);
}
}
輸出刪除
remove method called if : 2
remove method called if : javax.swing.JLabel
remove method called if : javax.swing.JPanel
remove method called for : pkginterface.ButtonTabComponent$1
remove method called for : pkginterface.ButtonTabComponent$TabButton
'ButtonTabComponent'不會包含您的MyEditorPane'的'例如,它會被'JTabbedPane' – MadProgrammer
包含什麼將是包含MyEditorPane –
標籤的指數你應該看看[如何使用分頁窗格](https://docs.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html),它演示了基本概念,但它似乎也是代碼的基礎。就像'int i = pane.indexOfTabComponent(ButtonTabComponent.this);',然後允許你使用'pane.remove(i)' – MadProgrammer