keylistener_带有Java代码的KeyListener示例程序

keylistener

The following Java code shows an example program that implements the KeyListener interface. When executed, the Java code will show a very simple Swing Graphical User Interface.

以下Java代码显示了实现KeyListener接口的示例程序。 执行时,Java代码将显示一个非常简单的Swing图形用户界面

背景 ( Background )

The GUI is made up of a JFrame which contains two JTextAreas. The first, feedbackText JTextArea, is placed inside a JScrollPane and is used to display text generated by the KeyListener events. The JScrollPane allows the user to see all the lines of text generated by the KeyListener events.

GUI由包含两个JTextAreasJFrame组成。 第一个, feedbackText JTextArea ,放置在JScrollPane内部,用于显示KeyListener事件生成的文本。 JScrollPane允许用户查看KeyListener事件生成的所有文本行。

The second is the inputText JTextArea. This JTextArea has the focus and will generate KeyListener events as the user types into it. By default, the inputArea JTextArea will have the focus when the JFrame appears.

第二个是inputText JTextArea 。 此JTextArea具有焦点,并将在用户键入它时生成KeyListener事件。 默认情况下,当JFrame出现时, inputArea JTextArea将具有焦点。

The KeyListener interface could have been implemented as a separate class, or extending the JFrame, but in this instance using an anonymous inner class makes the most sense.

KeyListener接口可以实现为单独的类,也可以扩展JFrame ,但是在这种情况下,使用匿名内部类最有意义。

The keyPressed method is called when a user presses down on a key and the keyReleased method is called when a key is released. The keyTyped method is called when a character key is typed into the inputText JTextArea.

当用户按下某个键时,将调用keyPressed方法,而当释放一个键时,将调用keyReleased方法。 将字符键键入到inputText JTextArea时,将调用keyTyped方法。

Java代码清单 ( Java Code Listing )

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
//Here's a class for a simple GUI that uses a JFrame
//to hold to JTextAreas - one will listen for the key events
//and the other will sit inside a JScrollPane providing feedback
//about the KeyListener events being triggered
public class KeyListenerExample {
JTextArea inputText;
JTextArea feedbackText;
//Note: Typically the main method will be in a
//separate class. As this is a simple one class
//example it's all in the one class.
public static void main(String[] args) {
//Use the event dispatch thread for Swing components
EventQueue.invokeLater(new Runnable()
{
@Override
public void run()
{
new KeyListenerExample();
}
});
}
public KeyListenerExample()
{
JFrame guiFrame = new JFrame();
//make sure the program exits when the frame closes
guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
guiFrame.setTitle("Creating a Table Example");
guiFrame.setSize(700,200);
//This will center the JFrame in the middle of the screen
guiFrame.setLocationRelativeTo(null);
//This JTextArea is used to display information about
//the keylistener events. It's place in a JScrollPane
//to allow the scrolling through all the events triggered
feedbackText = new JTextArea();
JScrollPane scrollText = new JScrollPane(feedbackText);
//This JTextArea will trigger the KeyListener events as
//long as it hold the focus
inputText = new JTextArea();
//The KeyListener interface is implemented as an anonymous
//inner class using the addKeyListener method.
inputText.addKeyListener(new KeyListener()
{
//When any key is pressed and released then the
//keyPressed and keyReleased methods are called respectively.
//The keyTyped method is called when a valid character is typed.
//The getKeyChar returns the character for the key used. If the key
//is a modifier key (e.g., SHIFT, CTRL) or action key (e.g., DELETE, ENTER)
//then the character will be a undefined symbol.
@Override
public void keyPressed(KeyEvent e)
{
feedbackText.append("Key Pressed: " + e.getKeyChar() + "\n");
}
@Override
public void keyReleased(KeyEvent e)
{
feedbackText.append("Key Released: " + e.getKeyChar() + "\n");
}
@Override
public void keyTyped(KeyEvent e)
{
//The getKeyModifiers method is a handy
//way to get a String representing the
//modifier key.
feedbackText.append("Key Typed: " + e.getKeyChar() + " " + KeyEvent.getKeyModifiersText(e.getModifiers()) + "\n");
}
});
guiFrame.add(inputText, BorderLayout.NORTH);
guiFrame.add(scrollText, BorderLayout.CENTER);
guiFrame.setVisible(true);
}
}

翻译自: https://www.thoughtco.com/a-keylistener-example-program-2033900

keylistener

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值