Java 中键盘事件的程序怎么样的?希望能标明注释,先谢啦!!!
发布网友
发布时间:2022-04-29 03:38
我来回答
共3个回答
热心网友
时间:2023-10-09 12:19
but.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//这里写按键时要执行的代码
}
}
给你个完整的
import static java.awt.BorderLayout.NORTH;
import java.awt.Button;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
class Demo{
private Frame f;
private TextArea ta;
private Button but;
private TextField tf;
private Dialog d;
private Label lab;//错误操作弹出对话框里面的文字
private Button bt;
private Panel p;
Demo(){
init();
}//endof Demo
public void init(){
lab = new Label();//提示信息,现在还不知道
p = new Panel();
f = new Frame("my window");
f.setBounds(300,100,600,500);
f.setLayout(new FlowLayout());
tf = new TextField(60);
but = new Button("转到");
ta = new TextArea(25,69);
d = new Dialog(f,"提示信息",true);
bt = new Button("确定");
d.setBounds(400,200,300,100);
//d.setLayout(new FlowLayout());
d.add(lab,NORTH);
p.setLayout(new FlowLayout());
p.add(bt);
d.add(p);
f.add(tf);
f.add(but);
f.add(ta);
f.setVisible(true);
myEvent();
}//endof init()
//实现各种监听事件
public void myEvent(){
tf.addActionListener(new ActionListener(){//按回车实现"转到"按钮
public void actionPerformed(ActionEvent e){
String text = tf.getText();
tf.setText("");
File file = new File(text);
if(file.exists() && file.isDirectory()){//--------->两种遍历方法。。。记下来吧都
ta.setText("");
/*String[] names = file.list();
for (String name : names) {
ta.append(name + "\r\n");
}*/
traverse(file);
}
else{
String msg = "您输入的" + text +"有误,请重新输入!";
lab.setText(msg);
d.setVisible(true);
}
}
});
//关闭窗口
f.addWindowListener((new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e){
System.exit(1);
}
}));
//隐藏对话框
d.addWindowListener((new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e){
d.setVisible(false);
}
}));
//按确定按钮隐藏对话框
bt.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
d.setVisible(false);
}
});
//按”转到“按钮实现遍历
but.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String text = tf.getText();
tf.setText("");
File file = new File(text);
if(file.exists() && file.isDirectory()){//--------->两种遍历方法。。。记下来吧都
ta.setText("");
/*String[] names = file.list();
for (String name : names) {
ta.append(name + "\r\n");
}*/
traverse(file);
}
else{
String msg = "您输入的" + text +"有误,请重新输入!";
lab.setText(msg);
d.setVisible(true);
}
}
});
}//endof myEvent()
public void traverse(File file){//遍历文件夹
File[] arr = file.listFiles();//返回一个文件路径数组
for(int i = 0; i < arr.length; i++){
if(arr[i].isFile())
ta.append(arr[i] + "\r\n");
else traverse(arr[i]);
}
}//endof traverse()
}//endof Demo{}
public class Main{
public static void main(String[] args) {
new Demo();
}
}
热心网友
时间:2023-10-09 12:20
使用Scanner 获取键盘输入追问那我要做一个计算器,怎么才能将键盘输入的数值获取了???
追答Scanner sc = new Scanner(System.in);
System.out.println("请输入数据");
int a = sc.nextLine();//获取输入的数据
热心网友
时间:2023-10-09 12:20
j集成自窗口 后添加监听事件 重写
热心网友
时间:2023-10-09 12:19
but.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//这里写按键时要执行的代码
}
}
给你个完整的
import static java.awt.BorderLayout.NORTH;
import java.awt.Button;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
class Demo{
private Frame f;
private TextArea ta;
private Button but;
private TextField tf;
private Dialog d;
private Label lab;//错误操作弹出对话框里面的文字
private Button bt;
private Panel p;
Demo(){
init();
}//endof Demo
public void init(){
lab = new Label();//提示信息,现在还不知道
p = new Panel();
f = new Frame("my window");
f.setBounds(300,100,600,500);
f.setLayout(new FlowLayout());
tf = new TextField(60);
but = new Button("转到");
ta = new TextArea(25,69);
d = new Dialog(f,"提示信息",true);
bt = new Button("确定");
d.setBounds(400,200,300,100);
//d.setLayout(new FlowLayout());
d.add(lab,NORTH);
p.setLayout(new FlowLayout());
p.add(bt);
d.add(p);
f.add(tf);
f.add(but);
f.add(ta);
f.setVisible(true);
myEvent();
}//endof init()
//实现各种监听事件
public void myEvent(){
tf.addActionListener(new ActionListener(){//按回车实现"转到"按钮
public void actionPerformed(ActionEvent e){
String text = tf.getText();
tf.setText("");
File file = new File(text);
if(file.exists() && file.isDirectory()){//--------->两种遍历方法。。。记下来吧都
ta.setText("");
/*String[] names = file.list();
for (String name : names) {
ta.append(name + "\r\n");
}*/
traverse(file);
}
else{
String msg = "您输入的" + text +"有误,请重新输入!";
lab.setText(msg);
d.setVisible(true);
}
}
});
//关闭窗口
f.addWindowListener((new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e){
System.exit(1);
}
}));
//隐藏对话框
d.addWindowListener((new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e){
d.setVisible(false);
}
}));
//按确定按钮隐藏对话框
bt.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
d.setVisible(false);
}
});
//按”转到“按钮实现遍历
but.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String text = tf.getText();
tf.setText("");
File file = new File(text);
if(file.exists() && file.isDirectory()){//--------->两种遍历方法。。。记下来吧都
ta.setText("");
/*String[] names = file.list();
for (String name : names) {
ta.append(name + "\r\n");
}*/
traverse(file);
}
else{
String msg = "您输入的" + text +"有误,请重新输入!";
lab.setText(msg);
d.setVisible(true);
}
}
});
}//endof myEvent()
public void traverse(File file){//遍历文件夹
File[] arr = file.listFiles();//返回一个文件路径数组
for(int i = 0; i < arr.length; i++){
if(arr[i].isFile())
ta.append(arr[i] + "\r\n");
else traverse(arr[i]);
}
}//endof traverse()
}//endof Demo{}
public class Main{
public static void main(String[] args) {
new Demo();
}
}
热心网友
时间:2023-10-09 12:20
使用Scanner 获取键盘输入追问那我要做一个计算器,怎么才能将键盘输入的数值获取了???
追答Scanner sc = new Scanner(System.in);
System.out.println("请输入数据");
int a = sc.nextLine();//获取输入的数据
热心网友
时间:2023-10-09 12:20
j集成自窗口 后添加监听事件 重写
热心网友
时间:2023-10-09 12:19
but.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//这里写按键时要执行的代码
}
}
给你个完整的
import static java.awt.BorderLayout.NORTH;
import java.awt.Button;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
class Demo{
private Frame f;
private TextArea ta;
private Button but;
private TextField tf;
private Dialog d;
private Label lab;//错误操作弹出对话框里面的文字
private Button bt;
private Panel p;
Demo(){
init();
}//endof Demo
public void init(){
lab = new Label();//提示信息,现在还不知道
p = new Panel();
f = new Frame("my window");
f.setBounds(300,100,600,500);
f.setLayout(new FlowLayout());
tf = new TextField(60);
but = new Button("转到");
ta = new TextArea(25,69);
d = new Dialog(f,"提示信息",true);
bt = new Button("确定");
d.setBounds(400,200,300,100);
//d.setLayout(new FlowLayout());
d.add(lab,NORTH);
p.setLayout(new FlowLayout());
p.add(bt);
d.add(p);
f.add(tf);
f.add(but);
f.add(ta);
f.setVisible(true);
myEvent();
}//endof init()
//实现各种监听事件
public void myEvent(){
tf.addActionListener(new ActionListener(){//按回车实现"转到"按钮
public void actionPerformed(ActionEvent e){
String text = tf.getText();
tf.setText("");
File file = new File(text);
if(file.exists() && file.isDirectory()){//--------->两种遍历方法。。。记下来吧都
ta.setText("");
/*String[] names = file.list();
for (String name : names) {
ta.append(name + "\r\n");
}*/
traverse(file);
}
else{
String msg = "您输入的" + text +"有误,请重新输入!";
lab.setText(msg);
d.setVisible(true);
}
}
});
//关闭窗口
f.addWindowListener((new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e){
System.exit(1);
}
}));
//隐藏对话框
d.addWindowListener((new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e){
d.setVisible(false);
}
}));
//按确定按钮隐藏对话框
bt.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
d.setVisible(false);
}
});
//按”转到“按钮实现遍历
but.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String text = tf.getText();
tf.setText("");
File file = new File(text);
if(file.exists() && file.isDirectory()){//--------->两种遍历方法。。。记下来吧都
ta.setText("");
/*String[] names = file.list();
for (String name : names) {
ta.append(name + "\r\n");
}*/
traverse(file);
}
else{
String msg = "您输入的" + text +"有误,请重新输入!";
lab.setText(msg);
d.setVisible(true);
}
}
});
}//endof myEvent()
public void traverse(File file){//遍历文件夹
File[] arr = file.listFiles();//返回一个文件路径数组
for(int i = 0; i < arr.length; i++){
if(arr[i].isFile())
ta.append(arr[i] + "\r\n");
else traverse(arr[i]);
}
}//endof traverse()
}//endof Demo{}
public class Main{
public static void main(String[] args) {
new Demo();
}
}
热心网友
时间:2023-10-09 12:20
使用Scanner 获取键盘输入追问那我要做一个计算器,怎么才能将键盘输入的数值获取了???
追答Scanner sc = new Scanner(System.in);
System.out.println("请输入数据");
int a = sc.nextLine();//获取输入的数据
热心网友
时间:2023-10-09 12:20
j集成自窗口 后添加监听事件 重写