Java小程序,需求对键盘按下回车事件进行相应,但是设成if(event==KeyEvent.VK_ENTER)时却只有按CTRL+ENTE有点卡,没写完就发出去了,只有按CTRL+ENTER才有响应,而只按ENTER就没反应
来源:学生作业帮助网 编辑:六六作业网 时间:2024/11/13 04:34:15
Java小程序,需求对键盘按下回车事件进行相应,但是设成if(event==KeyEvent.VK_ENTER)时却只有按CTRL+ENTE有点卡,没写完就发出去了,只有按CTRL+ENTER才有响应,而只按ENTER就没反应
Java小程序,需求对键盘按下回车事件进行相应,但是设成if(event==KeyEvent.VK_ENTER)时却只有按CTRL+ENTE
有点卡,没写完就发出去了,
只有按CTRL+ENTER才有响应,而只按ENTER就没反应
Java小程序,需求对键盘按下回车事件进行相应,但是设成if(event==KeyEvent.VK_ENTER)时却只有按CTRL+ENTE有点卡,没写完就发出去了,只有按CTRL+ENTER才有响应,而只按ENTER就没反应
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
* @author Hardneedl
*/
class KeyPressDemo extends JFrame {
private static final Dimension minSize = new Dimension(300, 200);
private static final Dimension maxSize = new Dimension(1024, 768);
private static final Dimension preferredSize = new Dimension(600, 400);
public Dimension getMaximumSize() {return maxSize;}
public Dimension getMinimumSize() {return minSize;}
public Dimension getPreferredSize() {return preferredSize;}
public String getTitle() {return "Key Press Demo";}
KeyPressDemo() throws HeadlessException {
doLay();
attachListeners();
}
private void doLay() {
Container container = getContentPane();
final JTextArea area = new JTextArea(){
public boolean isEditable() {return false;}
public Color getBackground() {return Color.BLACK;}
public Color getForeground() {return Color.YELLOW;}
};
area.addKeyListener(new KeyAdapter(){
public void keyTyped(KeyEvent e) {
if (e.getModifiers() == KeyEvent.CTRL_MASK && e.getKeyChar() == KeyEvent.VK_ENTER)
area.append( e.toString()+"\r\n");
}
});
container.add(new JScrollPane(area),BorderLayout.CENTER);
pack();
}
private void attachListeners() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {new KeyPressDemo().setVisible(true);}
}