2012-03-14 180 views
2

enter image description here更改標籤名稱

我想通過點擊通過添加的actionPerformed從彈出菜單「重命名」選項更改標籤名稱,並直接在選項卡中輸入新的名稱。

我發現這個jTabbedPane.setTitleAt(count, "string here");

但它不是我想要的東西,這只是設置參數傳遞的字符串。

感謝

+0

笏ü想做的事???詳細註明.. – 2012-03-14 10:22:00

+0

我想通過鍵入一個新的名稱每個選項卡上更改名稱:現在每一個標籤已經名稱設置好的爲「工作區+數」; 我的目標:右鍵點擊,彈出「重命名」出現的項目,點擊「重命名」選項卡設置標題(「」),然後鍵入並設置一個新的名稱。 – 2012-03-14 10:28:59

+0

具有u試圖setTitleAt()...如果是,那麼笏是問題烏爾面臨?? – 2012-03-14 10:38:19

回答

4

通過添加JPopupMenu可以確定indexgetTitleAt()JTabbedPane

import java.awt.*; 
import javax.swing.*; 
import javax.swing.event.*; 

public class TabPopupDemo extends JFrame { 

    private static final long serialVersionUID = 1L; 
    private JLabel jLabel1; 
    private JLabel jLabel2; 
    private JMenuItem jMenuItem1; 
    private JPopupMenu jPopupMenu1; 
    private JTabbedPane jTabbedPane1; 

    public TabPopupDemo() { 
     setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
     setSize(400, 300); 
     setLocationRelativeTo(null); 
     jPopupMenu1 = new JPopupMenu(); 
     jMenuItem1 = new JMenuItem("jMenuItem1"); 
     jTabbedPane1 = new JTabbedPane(); 
     jLabel1 = new JLabel("jLabel1"); 
     jLabel2 = new JLabel("jLabel2"); 
     jPopupMenu1.add(jMenuItem1); 
     jTabbedPane1.addTab(null, jLabel1); 
     jTabbedPane1.addTab(null, jLabel2); 
     getContentPane().add(jTabbedPane1, BorderLayout.CENTER); 
     int tabCount = jTabbedPane1.getTabCount(); 
     for (int i = 0; i < tabCount; i++) { 
      JLabel jLabel = new JLabel("Testing the tab" + (i + 1)); 
      jTabbedPane1.setTabComponentAt(i, jLabel); 
      jLabel.setName(String.valueOf(i)); 
      jLabel.setComponentPopupMenu(jPopupMenu1); 
     } 
     jPopupMenu1.addPopupMenuListener(new PopupMenuListener() { 

      @Override 
      public void popupMenuCanceled(final PopupMenuEvent evt) { 
      } 

      @Override 
      public void popupMenuWillBecomeInvisible(final PopupMenuEvent evt) { 
      } 

      @Override 
      public void popupMenuWillBecomeVisible(final PopupMenuEvent evt) { 
       JPopupMenu source = (JPopupMenu) evt.getSource(); 
       JLabel invoker = (JLabel) source.getInvoker(); 
       JLabel component = (JLabel) jTabbedPane1.getComponentAt(Integer.parseInt(invoker.getName())); 
       jMenuItem1.setText(invoker.getText() + ": " + component.getText()); 
      } 
     }); 
    } 

    public static void main(final String args[]) { 
     EventQueue.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       new TabPopupDemo().setVisible(true); 
      } 
     }); 
    } 
} 
+0

敘述自定義選項卡組成部分?典型的標籤是不是這樣的啄:-) – kleopatra 2012-03-14 10:51:36

+0

@kleopatra你是對的,謝謝你足夠長的時間,真的是....在大多數情況下,我使用的命名不存在於這個詞 – mKorbel 2012-03-14 11:08:09