本帖最后由 赵崇友 于 2013-5-5 21:35 编辑
- <p>import java.awt.Button;
- import java.awt.Dialog;
- import java.awt.FlowLayout;
- import java.awt.Frame;
- import java.awt.Label;
- import java.awt.TextArea;
- import java.awt.TextField;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.KeyAdapter;
- import java.awt.event.KeyEvent;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- import java.io.File;</p><p>public class MouseAndKeyAction {</p><p> /**
- * @param args
- */
- @SuppressWarnings("unused")
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- MyFrame mf=new MyFrame();
- }</p><p>}
- class MyFrame {
- private Frame f;
- private Button b,okbutton;
- private TextField tf;
- private TextArea ta;
- private Dialog dia;
- private Label lab;
- MyFrame(){
- inital();
- }
- private void inital(){
- f=new Frame("file");
- b=new Button("转到");
- tf=new TextField(40);
- ta=new TextArea(20,48);
- f.setLayout(new FlowLayout());
- f.setBounds(500, 100, 500, 500);
- dia=new Dialog(f,"提示信息",true);
- okbutton=new Button("确定");
- dia.setBounds(600,350,300,150);
- dia.setLayout(new FlowLayout());
- lab=new Label();
- dia.add(lab);
- dia.add(okbutton);
- f.add(tf);
- f.add(b);
- f.add(ta);
- myEvent();
- f.setVisible(true);
-
- }
- private void myEvent(){
- f.addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent e){
- System.exit(0);
- }
- });
- b.addActionListener(new ActionListener() {
-
- @Override
- public void actionPerformed(ActionEvent e) {
- // TODO Auto-generated method stub
- showDir();
-
- }
- });
- dia.addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent e){
- dia.setVisible(false);
- }
- });
- okbutton.addActionListener(new ActionListener() {
-
- @Override
- public void actionPerformed(ActionEvent e) {
- // TODO Auto-generated method stub
- dia.setVisible(false);
- }
- });
- tf.addKeyListener(new KeyAdapter() {
- @SuppressWarnings("unused")
- public void Keypressed(KeyEvent e){
- if(e.getKeyCode()==KeyEvent.VK_ENTER){
- showDir();
- }
- }
- });
- }
- private void showDir(){
- String path=tf.getText();
- File file=new File(path);
- ta.setText("");
- if (file.exists()&&file.isDirectory()){
- String []names=file.list();
- for(String name:names){
- ta.append(name+"\r\n");
- }
- }
- else
- {
- String erroinfo="找不文件:"+path+":路径不存在"+";请重新输入路径";
- lab.setText(erroinfo);
- dia.setVisible(true);
- }
- }
- }</p>
复制代码 如何实现:按ENTER键列出目录呢?
我添加了按键监听咋按enter键没有反应呢?
|
|