java上機 第五周 任務5 封裝一個矩陣類
[java] view plaincopy
- /*
- * 程序頭部注釋開始
- * 程序的版權和版本聲明部分
- * Copyright (c) 2011, 煙臺大學計算機學院學生
- * All rights reserved.
- * 文件名稱:GUI(練習-列出指定目錄內容 有對話框Dialog)
- * 作 者:薛廣晨
- * 完成日期:2011 年 09 月 20 日
- * 版 本號:x1.0
- * 對任務及求解方法的描述部分
- * 輸入描述:
- * 問題描述:
- * 程序輸出:
- * 程序頭部的注釋結束
- */
- package xue;
- import java.awt.*;
- import java.awt.event.*;
- import java.io.File;
- public class MyDiaglog {
- /**
- * @param args
- */
- private Frame f;
- private TextField tf;
- private Button but;
- private TextArea ta;
- private Dialog d;
- private Label lab;
- private Button okBut;
- MyDiaglog()
- {
- init();
- }
- public void init()
- {
- 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, 70);
- d = new Dialog(f, "提示信息——self", true);
- d.setBounds(400, 200, 240,150);
- d.setLayout(new FlowLayout());
- lab = new Label();
- okBut = new Button("確定");
- d.add(lab);
- d.add(okBut);
- f.add(tf);
- f.add(but);
- f.add(ta);
- myEvent();
- f.setVisible(true);
- }
- private void myEvent() {
- // TODO Auto-generated method stub
- d.addWindowListener(new WindowAdapter()
- {
- public void windowClosing(WindowEvent e)
- {
- d.setVisible(false);
- }
- });
- okBut.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- d.setVisible(false);
- }
- });
- tf.addKeyListener(new KeyAdapter()
- {
- public void keyPressed(KeyEvent e)
- {
- if(e.getKeyCode() == KeyEvent.VK_ENTER)
- {
- showDir();
- }
- }
- });
- but.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- showDir();
- }
- });
- f.addWindowListener(new WindowAdapter()
- {
- public void windowClosing(WindowEvent e)
- {
- System.exit(0);
- }
- });
- }
- private void showDir()
- {
- // TODO Auto-generated method stub
- String dirPath = tf.getText();
- File dir = new File(dirPath);
- if(dir.exists() && dir.isDirectory())
- {
- ta.setText("");
- String[] names = dir.list();
- for(String name : names)
- {
- ta.append(name + "\r\n");
- }
- }
- else
- {
- String info = "您輸入的信息:"+dirPath+"是錯誤的。請重輸";
- lab.setText(info);
- d.setVisible(true);
- }
- }
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- new MyDiaglog();
- }
- }
運行結果:
個人網站 www.software8.co