java GUI类制作的资源管理器小程序

 

package cn.itcast.day_19.exercise;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Frame;
import java.awt.List;
import java.awt.Panel;
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;
import java.io.IOException;

 

小程序;资源管理器 :点击下载

//使用cmd命令,在里面输入 java -jar 加上文件的路径名 ,就可以打开和运用了,有些机器可以直接运行


public class Explorer {
 
 private Frame frame;
 private TextField textField;
 private List list;
 private Button turnButton;
 private Button upButton;
 
 public Explorer(){
  generateUI();
  handleEvent();
 }
 
 private void handleEvent() {                         //给窗体 按钮 文本域 list列表 添加监视器
  frame.addWindowListener(new WindowAdapter(){
   public void windowClosing(WindowEvent e) {
    System.exit(0);
   }
  });
  turnButton.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e) {
    turn();
   }
  });
  textField.addKeyListener(new KeyAdapter(){
   public void keyPressed(KeyEvent e) {
    if(e.getKeyCode() == KeyEvent.VK_ENTER)
     turn();
   }
  });
  list.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e) {
    clickList();
   }
  });
  upButton.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e) {
    up();
   }
  });
 }

 private void up() {
  File file = new File(textField.getText());  // 获取原有路径
  textField.setText(file.getParent());   // 将路径设置为父级路径
  turn();           // 跳转
 }

 private void clickList() {
  File file = new File(textField.getText(), list.getSelectedItem());  // 获取原有路径和选中路径, 拼在一起
  textField.setText(file.getAbsolutePath());  // 设置回TextField
  turn();           // 跳转
 }
 
 private void turn() {
  String path = textField.getText();
  File file = new File(path);
  if(file.isFile()){
   up();
   if(file.getName().endsWith(".java") || file.getName().endsWith(".txt"))  //如果是.java和.txt文件.则用自己编写的记事本打开
    new NotePad().loadFile(file);
   else
    try {
     Runtime.getRuntime().exec("cmd /c \"" + file.getAbsolutePath() + "\"");  //使用默认程序打开文件
    } catch (IOException e) {
     new RuntimeException(e);
    }
  }
  if(file.isDirectory()){
   list.removeAll();     // 删除原有条目
   String[] names = file.list();  // 获取文件夹下所有子文件的名字
   for (String name : names)   // 循环遍历
    list.add(name);     // 将每一个名字添加到list中
  }
 }

 private void generateUI() {
  frame = new Frame("资源管理器");          //设置软件界面
  frame.setSize(600,400);
  frame.setLocation(500, 100);
  
  Panel panel = new Panel();    // 定义Panel用来把textField, turnButton, upButton一起放在北边
  
  textField = new TextField(50);
  turnButton = new Button("跳转");
  upButton = new Button("向上");
  
  panel.add(textField);
  panel.add(turnButton);
  panel.add(upButton);
  
  frame.add(panel, BorderLayout.NORTH);
  
  list = new List();
  frame.add(list);
  
  frame.setVisible(true);
 }

 public static void main(String[] args) {   //创建对象,执行方法
  new Explorer();
 }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值