JTextPane:KeyBindings在StyledEditorKit上不起作用 [英] JTextPane: KeyBindings are not working on StyledEditorKit

查看:75
本文介绍了JTextPane:KeyBindings在StyledEditorKit上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请看下面的代码

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?请帮忙...

In here, user can enter any text, and when he select a text and click on "Bold" button, the text will be bold. However, I need to do it using CTRL+B also. As you can see, my attempt is not giving any response to that key event. I even tried adding it to a seperate class which extends AbstractAction, but still no good. How can I implement the CTRL+B here? Please help...

推荐答案

当键绑定对我不起作用时,我首先想到的是InputMap -我确定我使用的是正确的吗?好吧,你确定吗?默认值使用JComponent.WHEN_FOCUSED,因此仅在组件具有焦点的情况下有效.

When key bindings don't work for me, the first place I look is the InputMap -- am I sure that I'm using the right one? Well, are you sure? The default one uses JComponent.WHEN_FOCUSED and thus only works if your component has the focus.

如果您希望它在其他时间工作,例如说绑定的组件在焦点窗口中可见且在不一定具有焦点的情况下,也许您应该尝试使用不同的条件参数.尝试使用JComponent.WHEN_IN_FOCUSED_WINDOW开头.

If you want it to work at other times, say when the bound component is visible and in a focused window but doesn't necessarily have the focus itself, perhaps you should try different condition parameters. Try using JComponent.WHEN_IN_FOCUSED_WINDOW to start with.

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

这篇关于JTextPane:KeyBindings在StyledEditorKit上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆