2017-05-08 47 views
1

我想要做tabbedPane.setSelectedIndex(0)當我點擊CTRL + 。設置TabbedPane點擊鍵時的索引

你能告訴我該怎麼做嗎?

我已經發現:

KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.CTRL_MASK) 

,但我不知道如何使用它。

回答

0

這裏是例如使用:

import java.awt.BorderLayout; 
import java.awt.Container; 
import java.awt.event.ActionEvent; 
import javax.swing.AbstractAction; 
import javax.swing.Action; 
import javax.swing.ActionMap; 
import javax.swing.InputMap; 
import javax.swing.JButton; 
import javax.swing.JComponent; 
import javax.swing.JFrame; 
import javax.swing.KeyStroke; 

public class KeyTester { 

    static class MyActionListener extends AbstractAction { 

     MyActionListener(String str) { 

      super(str); 

     } 

     @Override 

     public void actionPerformed(ActionEvent event) { 

      System.out.println(getValue(Action.NAME)); 

     } 

    } 

    public static void main(String args[]) { 

     String aKey = "Action"; 

     JFrame jFrame = new JFrame("Test"); 

     JButton jbutton1 = new JButton("<html><center>A<br>Focused/Typed"); 

     JButton jbutton2 = new JButton("<html><center>Ctlr-Z<br>Window/Pressed"); 

     JButton jbutton3 = new JButton("<html><center>Shift-L<br>Ancestor/Released"); 

     Container pane = jFrame.getContentPane(); 

     pane.add(jbutton1, BorderLayout.NORTH); 

     pane.add(jbutton2, BorderLayout.CENTER); 

     pane.add(jbutton3, BorderLayout.SOUTH); 

     KeyStroke keStroke = KeyStroke.getKeyStroke("typed A"); 

     Action act = new MyActionListener("Action occured"); 

     InputMap iMap = jbutton1.getInputMap(); 


     iMap.put(keStroke, aKey); 

     ActionMap actionMap = jbutton1.getActionMap(); 

     actionMap.put(aKey, act); 

     keStroke = KeyStroke.getKeyStroke("ctrl Z"); 

     act = new MyActionListener("No Action"); 

     iMap = jbutton2.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); 

     iMap.put(keStroke, aKey); 

     actionMap = jbutton2.getActionMap(); 

     actionMap.put(aKey, act); 

     keStroke = KeyStroke.getKeyStroke("shift released L"); 

     act = new MyActionListener("What Happened?"); 

     iMap = jbutton3.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); 

     iMap.put(keStroke, aKey); 

     actionMap = jbutton3.getActionMap(); 

     actionMap.put(aKey, act); 

     jFrame.setSize(200, 200); 

     jFrame.show(); 
    } 
} 
+0

謝謝,但點擊時如何使用它:CTRL + 1 – IneedYOURhelp

+0

以及如何與tabbedPane.setSelectedIndex(0)使用它呢? – IneedYOURhelp