要加入keymap。下面的例子我已通过,当你选中"keymap added“时,就可实现ctrl-enter回车了。
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;public class JTextAreaDemo extends JApplet {
private JTextArea textArea = new JTextArea("initial content");
private JCheckBox cbox = new JCheckBox("keymap added");
private Hashtable actionTable = new Hashtable();
private Keymap originalKeymap, newKeymap; public void init() {
Container contentPane = getContentPane(); loadActionTable();
originalKeymap = textArea.getKeymap();
newKeymap = createKeymap(); textArea.setFont(new Font("Dialog", Font.PLAIN, 24)); contentPane.add(new ControlPanel(), BorderLayout.NORTH);
contentPane.add(textArea, BorderLayout.CENTER);
}
private Keymap createKeymap() {
Keymap map = JTextComponent.addKeymap("applet keymap",
textArea.getKeymap()); KeyStroke   returnKeyStroke = 
KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,
       InputEvent.CTRL_MASK);
Action     returnAction =
getAction(DefaultEditorKit.insertBreakAction); map.addActionForKeyStroke(returnKeyStroke,
                          returnAction);
return map;
}
private void loadActionTable() {
Action[] actions = textArea.getActions(); for(int i=0; i < actions.length; ++i) {
actionTable.put(actions[i].getValue(Action.NAME),
actions[i]);
}
}
private Action getAction(String name) {
return (Action)actionTable.get(name);
}
class ControlPanel extends JPanel {
public ControlPanel() {
add(cbox); cbox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textArea.setKeymap(cbox.isSelected() ?
newKeymap : 
originalKeymap); textArea.requestFocus();
}
});
}
}
}

解决方案 »

  1.   

    谢谢楼上的回答,不过你这个是继承JApplet的,我的是Swing继承于JFrame,不一样的啊。我想问在Swing下如何实现呢?
      

  2.   

    可否通过设置重新事件如在原先事件public void keyPressed(KeyEvent e)中,用e.setKeyCode(KeyEvent.VK_ENTER)来作,试一试。
      

  3.   

    OK,重新参照wulemale(wulemale)提供的例子,问题大概解决了,会把分奉上的:)。不过对于原来的Enter键,现在的功能是想让它移到下一个控件上,现在出现的效果是TextArea中他执行一遍回车,然后跑到下一个控件上了,我不想让他在TextArea中执行回车。谁知道如何处理。