1 package com.chuangkohua; 2 3 import java.awt.Button; 4 import java.awt.Dialog; 5 import java.awt.FlowLayout; 6 import java.awt.Frame; 7 import java.awt.Label; 8 import java.awt.TextArea; 9 import java.awt.TextField; 10 import java.awt.event.ActionEvent; 11 import java.awt.event.ActionListener; 12 import java.awt.event.KeyAdapter; 13 import java.awt.event.WindowAdapter; 14 import java.awt.event.WindowEvent; 15 import java.io.File; 16 17 public class Test { 18 19 public static void main(String[] args) { 20 // TODO Auto-generated method stub 21 22 Frame frame = new Frame("小毛炉"); 23 frame.setBounds(300, 100, 600, 500); 24 TextField field = new TextField(60); 25 Button button = new Button("转到"); 26 TextArea area = new TextArea(25, 70); 27 frame.add(field); 28 frame.add(button); 29 frame.add(area); 30 frame.setLayout(new FlowLayout()); 31 frame.setVisible(true); 32 33 frame.addWindowListener(new Demo2()); 34 35 /* button.addActionListener(new ActionListener() { 36 37 @Override 38 public void actionPerformed(ActionEvent e) { 39 // TODO Auto-generated method stub 40 String str = field.getText(); 41 area.setText(str); 42 field.setText(""); 43 } 44 });*/ 45 46 47 //弹出窗口 48 Dialog dialog = new Dialog(frame,"提示小窗口",true); 49 Label label = new Label(); 50 Button button2 = new Button("确定"); 51 dialog.add(label); 52 dialog.add(button2); 53 dialog.setBounds(100, 50, 300, 200); 54 dialog.setLayout(new FlowLayout()); 55 //单击关闭按钮关闭对话框 56 dialog.addWindowListener(new WindowAdapter() { 57 public void windowClosing(WindowEvent e){ 58 dialog.setVisible(false); 59 } 60 }); 61 //单击button2按钮关闭对话框 62 button2.addActionListener(new ActionListener() { 63 64 @Override 65 public void actionPerformed(ActionEvent e) { 66 // TODO Auto-generated method stub 67 dialog.setVisible(false); 68 } 69 }); 70 71 //回车键转到文本区域显示 72 /*field.addKeyListener(new KeyAdapter() { 73 public void keyPressed(KeyEvent e){ 74 if(e.getKeyCode()==KeyEvent.VK_ENTER){ 75 String filepath = field.getText(); 76 File dir = new File(filepath); 77 if(dir.exists()&&dir.isDirectory()){ 78 field.setText(""); 79 String[] name = dir.list(); 80 for(String names : name){ 81 area.append(names+"\r\n"); 82 } 83 } 84 else{ 85 String str = " 您输入的信息"+filepath+"有误,请重新输入 "; 86 label.setText(str); 87 dialog.setVisible(true); 88 } 89 } 90 } 91 });*/ 92 93 button.addActionListener(new ActionListener() { 94 @Override 95 public void actionPerformed(ActionEvent e) { 96 97 // TODO Auto-generated method stub 98 String filepath = field.getText(); 99 File dir = new File(filepath); 100 if(dir.exists()&&dir.isDirectory()){ 101 field.setText(""); 102 String[] name = dir.list(); 103 for(String names : name){ 104 area.append(names+"\r\n"); 105 } 106 } 107 else{ 108 String str = " 您输入的信息"+filepath+"有误,请重新输入 "; 109 label.setText(str); 110 dialog.setVisible(true); 111 } 112 113 } 114 }); 115 } 116 117 } 118 119 class Demo2 extends WindowAdapter{ 120 public void windowClosing(WindowEvent e){ 121 System.exit(0); 122 } 123 }
时间: 2024-11-05 14:55:24