3

カスタムフォントでJTextFieldを実装したいのですが、JTextFieldでは実装できないことがわかりました。現在、JtextPaneを使用していますが、ユーザーがEnterキーを押したときにカラットが次の行に移動しないようにする方法に問題があります。?

4

3 に答える 3

3

DocumentFilterを追加し、そこに「\n」文字が挿入されないようにします。

于 2011-05-19T11:43:00.033 に答える
2
class DocFilter extends DocumentFilter{

        public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {
            fb.insertString(offset, string.replaceAll("\\n", ""), attr); 
        }

       public void replace(FilterBypass fb, int offset, int length, String string, AttributeSet attr) throws BadLocationException {
            fb.insertString(offset, string.replaceAll("\\n", ""), attr); 
        }
    }

このコードはうまく機能します

于 2011-05-19T14:30:47.893 に答える
0

keyListenerをjTextPaneに追加し、EnterキーのkeyPressed(e)メソッドを追跡します。enterのキーコードは10です。


次のようなキーバインディングを試してください。

JTextPane pane = new JTextPane();
// Get KeyStroke for enter key
KeyStroke enterKey = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0);
// Override enter for a pane
String actionKey = "none";
InputMap map = pane.getInputMap();
map.put(enterKey, actionKey);

これにより、textPaneのEnterキーの動作が上書きされ、ユーザーがEnterキーを押しても何も起こりません。

于 2011-05-19T11:32:06.887 に答える