我想通過點擊通過添加的actionPerformed從彈出菜單「重命名」選項更改標籤名稱,並直接在選項卡中輸入新的名稱。
我發現這個jTabbedPane.setTitleAt(count, "string here");
但它不是我想要的東西,這只是設置參數傳遞的字符串。
感謝
我想通過點擊通過添加的actionPerformed從彈出菜單「重命名」選項更改標籤名稱,並直接在選項卡中輸入新的名稱。
我發現這個jTabbedPane.setTitleAt(count, "string here");
但它不是我想要的東西,這只是設置參數傳遞的字符串。
感謝
通過添加JPopupMenu
可以確定index
和getTitleAt()
從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);
}
});
}
}
笏ü想做的事???詳細註明.. – 2012-03-14 10:22:00
我想通過鍵入一個新的名稱每個選項卡上更改名稱:現在每一個標籤已經名稱設置好的爲「工作區+數」; 我的目標:右鍵點擊,彈出「重命名」出現的項目,點擊「重命名」選項卡設置標題(「」),然後鍵入並設置一個新的名稱。 – 2012-03-14 10:28:59
具有u試圖setTitleAt()...如果是,那麼笏是問題烏爾面臨?? – 2012-03-14 10:38:19