2012-04-22 68 views
3

我有一个在特定设置下不可编辑的JTextArea。但是,在此设置下,用户仍然可以使用空格和退格键。为了容纳这个空间,我有以下代码,Java在不可编辑的JTextArea中插入退格键

if (e.getKeyChar() == KeyEvent.VK_SPACE) { 
    editor.insert(" ", editor.getCaretPosition()); 
} 

虽然我遇到了退格问题。我试过这个,

if (e.getKeyChar() == KeyEvent.VK_BACK_SPACE) { 
    editor.insert("\b", editor.getCaretPosition()); 
} 

这似乎是当按下退格键时添加一个小空间。它不如空间,当按下一次时它几乎不会被察觉。这绝对不是一个退格。更糟糕的情况下,我必须将所有字符复制到插入位置 - 1并将它们附加到插入位置后面的所有字符,但我不喜欢该解决方案。

+0

只是看着我写了前段时间的接口,我用(如你所说)**如果(!s.equals( “”)),S = s.substring (0,s.length() - 1); **(我的界面不需要克拉)另外:我不知道JTextArea是否为您处理它,但您可能需要注意Alt,Tab,Escape ,控制等(没有被** KeyEvent.isActionKey()**捕获的任何东西都会导致插入一个小空格(一个不可打印的字符) – lynks 2012-04-22 00:48:12

回答

3

使用键绑定允许空格键和退格键具有关联的动作,然后如果退格键被按下,则从JTextArea的文档中移除一个字符。

例如,

import java.awt.event.ActionEvent; 
import java.awt.event.KeyEvent; 

import javax.swing.*; 
import javax.swing.text.BadLocationException; 
import javax.swing.text.PlainDocument; 

@SuppressWarnings("serial") 
public class TextAreaFun extends JPanel { 
    public static final String SPACE = "space"; 
    public static final String BACK_SPACE = "back space"; 
    private JTextArea textArea = new JTextArea(15, 50); 

    public TextAreaFun() { 
     // create our key bindings 
     // only allow key presses to initiate an action if the JTextArea has focus 
     int condition = JComponent.WHEN_FOCUSED; 
     InputMap taInputMap = textArea.getInputMap(condition); 
     ActionMap taActionMap = textArea.getActionMap(); 

     taInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), SPACE); 
     taInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, 0), 
      BACK_SPACE); 
     taActionMap.put(SPACE, new KeyAction(textArea, SPACE)); 
     taActionMap.put(BACK_SPACE, new KeyAction(textArea, BACK_SPACE)); 

     // checkbox that stops all editing except for that specified in the 
     // key bindings above 
     JCheckBox chkBox = new JCheckBox(new AbstractAction("Prevent Editing") { 
     { 
      putValue(SELECTED_KEY, Boolean.FALSE); // default to unchecked 
      putValue(MNEMONIC_KEY, KeyEvent.VK_P); 
     } 

     @Override 
     public void actionPerformed(ActionEvent evt) { 
      boolean selection = (Boolean) getValue(SELECTED_KEY); 
      textArea.setEditable(!selection); 
     } 
     }); 
     JPanel bottomPanel = new JPanel(); 
     bottomPanel.add(chkBox); 

     setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS)); 
     add(new JScrollPane(textArea)); 
     add(Box.createVerticalStrut(10)); 
     add(bottomPanel); 
    } 

    private static void createAndShowGui() { 
     TextAreaFun mainPanel = new TextAreaFun(); 

     JFrame frame = new JFrame("TextAreaFun"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(mainPanel); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowGui(); 
     } 
     }); 
    } 
} 

@SuppressWarnings("serial") 
// action to be initiated by key bindings 
class KeyAction extends AbstractAction { 
    private PlainDocument textAreaDocument; 
    private String title; 

    public KeyAction(JTextArea textArea, String title) { 
     this.textAreaDocument = (PlainDocument) textArea.getDocument(); 
     this.title = title; 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     if (title.equals(TextAreaFun.SPACE)) { 
     try { 
      textAreaDocument.insertString(textAreaDocument.getLength(), " ", 
        null); 
     } catch (BadLocationException e1) { 
      e1.printStackTrace(); 
     } 
     } else if (title.equals(TextAreaFun.BACK_SPACE)) { 
     if (textAreaDocument.getLength() == 0) { 
      return; 
     } 
     try { 
      textAreaDocument.remove(textAreaDocument.getLength() - 1, 1); 
     } catch (BadLocationException e1) { 
      e1.printStackTrace(); 
     } 
     } 
    } 
} 
+0

感谢它的工作,不幸的是,由于textarea是不可编辑的,在Windows上恼人的高调声音,告诉你你正试图编辑一些不可编辑的东西,你会不会知道如何摆脱它,是吗? – gsingh2011 2012-04-22 04:43:44

+0

@ gsingh2011:不客气。我上面的代码适用于我,并且我没有听到这个声音,所以我不能提供任何建议,直到我能够以某种方式重现此问题。考虑创建并发布一个[sscce](http://sscce.org),类似于我在上面发布的内容,重现此问题,然后对我发表评论,然后我会查看它。 – 2012-04-22 13:55:41

相关问题