package com.GUI;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
// 創建一個具有文本框和按鈕的 界面; 輸入盤符 查到對應的文件;
public class Lianxi1 {
public static void main(String[] args) {
new myLianXi();
}
}
class myLianXi
{
private Frame f;
private Button bt;
private TextField tx;
private TextArea ta;
private Dialog d ; // 創建對話框;
private Label la;
private Button but;
public myLianXi()
{
init();
}
public void init()
{
f = new Frame("Lainxi");
bt = new Button("轉到");
tx = new TextField(30);
ta = new TextArea(15,30);
d = new Dialog(f,"提示信息-", true);
la = new Label();
but = new Button("OK");
d.add(la);
d.add(but);
f.setSize(500, 400);
f.setLocation(600, 300);
f.setLayout(new FlowLayout());
f.add(tx);
f.add(bt);
d.setSize(300, 100);
d.setLocation(300, 400);
d.setLayout(new FlowLayout());
f.add(ta);
myEvent(); // 轉到 事件;
f.setVisible(true);
}
public void myEvent()
{
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
bt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
ShowDie();
}
});
d.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{
d.setVisible(false);
}
});
but.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
d.setVisible(false);
}
});
but.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_ENTER) {
d.setVisible(false);
}
}
});
tx.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_ENTER) {
ShowDie();
}
}
});
}
public void ShowDie()
{
String txt = tx.getText(); // 得到鍵盤輸入的內容;
// ta.setText(txt); // 將內容輸入到文本區域中;
// tx.setText("");
File fe = new File(txt); // 列出指定目錄的全部內容;
if(fe.exists() && fe.isDirectory())
{
ta.setText("");
String[] name = fe.list();
for(String names: name )
{
ta.append(names+"\r\n");
} // 獲取目錄;
}
// 獲取對話框;
else
{
String ss = "您輸入的信息"+"\""+txt+"\""+"有誤";
la.setText(ss);
d.setVisible(true);
}
}
}
java GUI (列出給定目錄下的所有文件名稱)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.