控制台界面导航按钮侦听器 - java
Console Interface Navigation Button Listener - java
我正在为我正在做的一个小程序编写一个控制台界面。我在控制台上显示这样的东西:
Please select:
1)Add user
2)Delete user
3)Edit user
它自己应该监听按钮按下的方法,如果按钮按下是数字 1,2 或 3 之一,它应该调用其他方法并清除控制台中的所有文本。像这样:
display the above info
if(button is not pushed)
do nothing
else if ( button == 1)
call method addUser and clear everything on the console, so that addUser
can display it's info
我知道这个问题不包含任何代码,但我不知道该怎么做。我知道应该有某种按钮侦听器,但不知道使用什么以及如何使用。欢迎任何帮助:)
您必须向按钮添加一个事件侦听器,当单击该特定按钮时,您将调用所需的方法。
如果您不熟悉事件侦听器,我强烈建议您查看 https://docs.oracle.com/javase/tutorial/uiswing/events/intro.html 和官方文档门户上的相关页面。
希望对您有所帮助。
此代码可以指导您找到解决方案。使用 BufferedReader
从控制台读取并检查按下按钮的代码,并根据它们调用适当的方法。
public static void main (String[] args) throws IOException {
BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Hit 1, 2 or 3");
int buttonCode = bufferRead.read();
System.out.println("Code of button hit is: "+buttonCode);
//Button Codes for 1, 2 and 3 keys are 49, 50 and 51 respectively
if (buttonCode==49) {
//DO insert user
} else if(buttonCode==50) {
//Do delete
} else if (buttonCode==51) {
//Do Edit
} else {
System.out.println("Wrong button pressed");
}
}
我正在为我正在做的一个小程序编写一个控制台界面。我在控制台上显示这样的东西:
Please select:
1)Add user
2)Delete user
3)Edit user
它自己应该监听按钮按下的方法,如果按钮按下是数字 1,2 或 3 之一,它应该调用其他方法并清除控制台中的所有文本。像这样:
display the above info
if(button is not pushed)
do nothing
else if ( button == 1)
call method addUser and clear everything on the console, so that addUser
can display it's info
我知道这个问题不包含任何代码,但我不知道该怎么做。我知道应该有某种按钮侦听器,但不知道使用什么以及如何使用。欢迎任何帮助:)
您必须向按钮添加一个事件侦听器,当单击该特定按钮时,您将调用所需的方法。
如果您不熟悉事件侦听器,我强烈建议您查看 https://docs.oracle.com/javase/tutorial/uiswing/events/intro.html 和官方文档门户上的相关页面。
希望对您有所帮助。
此代码可以指导您找到解决方案。使用 BufferedReader
从控制台读取并检查按下按钮的代码,并根据它们调用适当的方法。
public static void main (String[] args) throws IOException {
BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Hit 1, 2 or 3");
int buttonCode = bufferRead.read();
System.out.println("Code of button hit is: "+buttonCode);
//Button Codes for 1, 2 and 3 keys are 49, 50 and 51 respectively
if (buttonCode==49) {
//DO insert user
} else if(buttonCode==50) {
//Do delete
} else if (buttonCode==51) {
//Do Edit
} else {
System.out.println("Wrong button pressed");
}
}