Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JTextPane:键绑定在StyledEditorKit上不起作用_Java_Swing_Jtextpane_Key Bindings_Keyevent - Fatal编程技术网

Java JTextPane:键绑定在StyledEditorKit上不起作用

Java JTextPane:键绑定在StyledEditorKit上不起作用,java,swing,jtextpane,key-bindings,keyevent,Java,Swing,Jtextpane,Key Bindings,Keyevent,请看一下下面的代码 import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.util.ArrayList; impor

请看一下下面的代码

import java.awt.Color;    
import java.awt.Dimension;    
import java.awt.FlowLayout;    
import java.awt.event.ActionEvent;    
import java.awt.event.ActionListener;    
import java.awt.event.KeyEvent;
import java.util.ArrayList;    
import java.util.List;    
import java.util.logging.Level;    
import java.util.logging.Logger;    
import javax.swing.*;    
import javax.swing.text.*;    

    public class Form1 extends JFrame      
    {      
        private JTextPane textPane;      
        private JPanel south;    
        private JScrollPane scroll;      

        private String  content;      
        public String documentType;                


        private DefaultStyledDocument document;          
        int start, end, offset1,length1;         
        private JButton button;             
        JFrame frame;    


        public Form1()      
        {      

            //Declaring the instance variables      
            textPane = new JTextPane();      
            textPane.setMinimumSize(new Dimension(100,100));      

            button = new JButton("Bold");      
            button.addActionListener(new StyledEditorKit.BoldAction());     
             button.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_B,KeyEvent.CTRL_MASK),"key");
        button.getActionMap().put("key", new StyledEditorKit.BoldAction());

            document = (DefaultStyledDocument) textPane.getDocument();        




            //Creating the main window     
            south = new JPanel();      
            south.setLayout(new FlowLayout());      
            south.add(button);                          
            scroll = new JScrollPane(textPane);      

            getContentPane().add(scroll,"Center");      
            getContentPane().add(south,"South");                  

            setSize(800,600);    
            validate();    
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);                          
        }     


        private class Action extends AbstractAction    
        {      
            public void actionPerformed(ActionEvent ae)      
            {              
                new StyledEditorKit.BoldAction();
            }
        }      

       public static void main(String[] args) throws Exception {       
            SwingUtilities.invokeLater(new Runnable()       
            {        
              @Override        
              public void run() {        
               Form1 f = new Form1();  
               f.setVisible(true);
              }        
            });        
          }       
    }  

在这里,用户可以输入任何文本,当他选择一个文本并点击“粗体”按钮时,文本将是粗体的。但是,我也需要使用CTRL+B来完成。正如你所看到的,我的尝试并没有对那个关键事件做出任何回应。我甚至尝试将它添加到一个扩展AbstractAction的单独类中,但仍然没有效果。我如何在这里实现CTRL+B?请帮助…

当键绑定对我不起作用时,我首先查看的是InputMap——我确定我使用的是正确的吗?嗯,你确定吗?默认使用
JComponent.WHEN_FOCUSED
,因此仅当组件具有焦点时才起作用

如果您希望它在其他时间工作,例如当绑定组件可见且在聚焦窗口中但不一定具有聚焦本身时,也许您应该尝试不同的条件参数。尝试使用
JComponent.WHEN_IN_FOCUSED_WINDOW
开始

i、 e

InputMap inputMap = myComponent.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);