English 中文(简体)
如果按下 Esc 时断断环
原标题:break a loop if Esc was pressed

我用JAVA语言写了一个程序, 通过使用 Scanner 类接收控制台输入...

now I want to add this ability to my code to exist a loop (while) when user presses Esc Button. so far I thought Keyboard class can help me but it was just like Scanner...I tried to use events but do not know how to use them correctly....

源代码 :

    package switchCase_v1;

     import cs1.Keyboard;
     import java.util.EventObject;
     import java.awt.AWTEvent;
     import java.awt.event.KeyEvent;
     import java.awt.event.ComponentEvent;
     import java.awt.event.InputEvent;
     import java.util.*;

      public class SwithCase {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        System.out.println("enter the name or number of month: ");
        int monthNumber = input.nextInt();

        while (true) {
            KeyEvent button;
            if (button.getKeyCode() == 27)
                break;
            else if (monthNumber ==  
 ) {
                System.out.println("enter a number");
                monthNumber = input.nextInt();
            时 时 else {
                switch (monthNumber) {
                case 1:
                case 2:
                case 3:
                case 4:
                case 5:
                case 6:
                    System.out.println("it has 31 days");
                    monthNumber = input.nextInt();
                    break;
                case 7:
                case 8:
                case 9:
                case 10:
                case 11:
                case 12:
                    System.out.println("it has 30 days");
                    monthNumber = input.nextInt();
                    break;
                default:
                    System.out.println("it is not a valid number");
                    monthNumber = input.nextInt();
                    break;
                时 时
            时 时

        时 时
    时 时
  时 时

当我想把“Esc”或“Enter”这样的按键考虑进去时,我如何处理这些案例?我认为它也应该通过使用 ASCII 代码来适用。

这是新版本的我的代码:

public static void main(String[] args) {
    // TODO Auto-generated method stub

    Scanner input = new Scanner(System.in);
    System.out.print("Check number of days");
    KeyEvent e;
    if (e.getKeyCode() == KeyEvent.VK_ENTER)
    {
    System.out.println("enter the name or number of month: ");
    int monthNumber=input.nextInt();
    时 时
    else if (Keyboard.getEventKey()==Keyboard.KEY_ESCAPE)
    {
        System.out.println("GoodBye");
    时 时
    时 时   

时 时

但它有一个错误 说e e object 可能尚未初始化...!!!! 我该怎么办?!!!

问题回答

您目前正在做一个命令行应用程序, 它读取从标准输入和打印到标准输出的东西。 如何处理按钮按键完全取决于您运行程序所在的终端, 大多数终端在按下逃跑时不会发送任何信息到您的应用程序 stdin 。

如果您想要抓取关键事件, 您将需要使用 AWT 或 swing 来做一个 GUI 应用程序。 如果您只想在程序运行时终止程序, 请尝试按下 Ctrl+C( 这在大多数终端中有效 ) 。

Take a look to this page, it may help you: http://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html

特别是,这一部分:

Int 获得 KeyCode ()

获取与此事件相关的密钥代码。 密钥代码指定了用户按下或发布的键盘上的特定密钥。 KeyEvent 类定义了常见密钥的许多密钥代码常数。 例如, VK_ A 指定了标有 A 标签的密钥, VK_ESCAPE 指定了 Escape 密钥 。

请注意, Keylisteers 可能并不总是你所想要的。 更完整的答案还将包括添加 KeyBindings 的引用, 这对设置来说比较困难, 但也比较多才多艺。 所以这就是我们如何做到的 。

首先创建一个新类。 如果组合动作需要参数, 请在您的构建器中设置它们。 让类扩展摘要动作, 这样我们就可以覆盖 ActionPaterformat 方法 。

public class UserAction extends AbstractAction {

String optionalParam;

UserAction(String optionalParam){
this.optionalParam = optionalParam;
}
@Override
public void actionPerformed(ActionEvent e) {

    System.out.println("Action Performed!");

    }

}

然后将动作调用如下。 请注意, 获得输入量马普需要一个代表对象焦点状态的参数整数。 三个有效选项是“ Jcomponent. WHEN_ IN_ FOCUSSED_ WINDOW” “ Jcompent. WHEN_ FOCUSSED” (默认) & amp; “ Jcomponent. WHEN_ ESCSERT_ OF_ FOCUSSED_ COMPONENT ” 。

JFrame graphicalUI = new JFrame();

JPanel panelWithKeybind = new JPanel();

Action userAction = new UserAction() //add params as constructor requires

panelWithKeybind.getinputmap(int 
focus_Status_Of_Panel).put(KeyStroke.getKeyStroke("ESCAPE"),"submit"); //Key
                                                                       
panelWithKeybind.getActionMap().put("submit",userAction);




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签