《Java程序设计》第16周周四:GUI编程及文件对话框的使用
2015-06-25 08:48
513 查看
<pre name="code" class="java">package com.liang; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; public class FileChooser extends JFrame implements ActionListener{ JButton open=null; JTextField jtfPath = null; public static void main(String[] args) { new FileChooser(); } public FileChooser() { this.setLayout(new FlowLayout()); // 按钮初始化 open=new JButton("open"); // 添加监听 open.addActionListener(this); // 把按钮添加到JFrame容器中 this.add(open); // 添加文本框控件 jtfPath = new JTextField("选择的文件",40); jtfPath.setEditable(false); // 不可编辑 jtfPath.setHorizontalAlignment(JTextField.CENTER); // 居中 this.add(jtfPath); // 设置JFrame的大小,可显示,默认关闭按钮 this.setBounds(400, 200, 700, 500); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub JFileChooser jfc=new JFileChooser(); jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES ); jfc.showDialog(new JLabel(), "选择"); File file=jfc.getSelectedFile(); if(file.isDirectory()){ System.out.println("文件夹:"+file.getAbsolutePath()); }else if(file.isFile()){ System.out.println("文件:"+file.getAbsolutePath()); } System.out.println(jfc.getSelectedFile().getName()); // 把文件路径显示在文本框中 jtfPath.setText(file.getAbsolutePath()); } }
相关文章推荐
- Win10最新手机预览版Build 10145要来了
- Java中的String、StringBuilder、StringBuffer对比
- 如何高效的判断数组中是否存在至少两个相同的数值,存在返回true,不存在返回false
- 【java基础】——GUI(图形化用户界面
- IOS 关于取消延迟执行函数的种种。performSelector与cancelPreviousPerformRequestsWithTarget
- UITableView快速入门
- UI基础-九宫格计算-模型|封装
- LeetCode——Implement Stack using Queues
- LeetCode95:Unique Binary Search Trees II
- duilib Ip控件
- duilib 可编辑ComboBox
- Spark Transformations之mapValues
- requireJS官网学习(2)
- android 事件处理机制之requestDisallowInterceptTouchEvent
- Upload files by sending multipart request programmatically
- 在ubuntu上配置apue的运行环境
- ios 在UITableViewController上添加悬浮视图
- 菜鸟福利-----以有道翻译API为例说明mui ajax的用法详解
- mui下拉刷新的ajax实现
- mui.ajax 如何调用webservice