我想通过点击通过添加的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