16周作业
来源:互联网 发布:云计算公司排名 编辑:程序博客网 时间:2024/06/05 10:10
import java.awt.Color; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.FileFilter; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; class myFileFilter implements FileFilter{ @Override public boolean accept(File pathname) { String filename = pathname.getName().toLowerCase(); if(filename.contains(".jpg")){ return false; }else{ return true; } } } public class guess01 extends JFrame implements ActionListener{ private static final long serialVersionUID = 1L; JButton open=null; JTextField jtfPath = null; JLabel jlbImg = null; JButton btnNext = null; String strPath = ""; //文件夹路径 String strFileName = ""; //文件名称 File[] fileArray; // 文件夹下所有文件 int NUM_IMG = 0; // 文件总数目 int index = 0; // 当前文件的序号 public static void main(String[] args) { new guess01(); } public guess01(){ this.setTitle("Week16"); // 设置布局方式 this.setLayout(new FlowLayout()); // 按钮初始化 open=new JButton("选择目录"); // 添加监听 open.addActionListener(this); // 把按钮添加到JFrame容器中 this.add(open); // 添加文本框控件 jtfPath = new JTextField("选择的文件",40); jtfPath.setEditable(false); // 不可编辑 jtfPath.setHorizontalAlignment(JTextField.CENTER); // 居中 this.add(jtfPath); // 显示下一张图片 btnNext = new JButton("显示下一张"); this.add(btnNext); btnNext.addActionListener(this); // 添加显示Image的JLabel控件 jlbImg = new JLabel(); jlbImg.setBackground(Color.RED); jlbImg.setBounds(100, 100, 200, 200); this.add(jlbImg); // 设置JFrame的大小,可显示,默认关闭按钮 this.setBounds(400, 200, 700, 500); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } @Override public void actionPerformed(ActionEvent e) { if(e.getSource()==open){ JFileChooser jfc=new JFileChooser(); jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 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()); // 获取文件路径 与文件名 strPath = file.getAbsolutePath(); strFileName = jfc.getSelectedFile().getName(); if(file!=null && file.isDirectory()){ fileArray = file.listFiles(); NUM_IMG = fileArray.length; } } if(e.getSource()==btnNext){ //如果是next按钮 String strTmp = fileArray[index].toString(); index++; if(index==NUM_IMG) index = 0; jlbImg.setIcon(new ImageIcon(strTmp)); } } }
0 0
- 16周作业
- 16周作业(2)
- 16周作业(3)
- 第16周作业一
- 第16周作业二
- 数据库:第16周作业
- [leetcode] 第16周作业
- 4/16 作业
- 算法设计作业16
- 作业10-16
- 16秋第二次作业
- JAVA作业11-16
- [2017.11.16]作业10
- JAVA11.16作业
- java作业16/17
- 第二周作业
- 第二周作业
- 第二周作业
- QWidget: Must construct a QApplication before a QWidget
- [leetcode] 109.Convert Sorted List to Binary Search Tree
- 系统服务(daemons)
- 支持上下拉刷新的RecycleView,基于androidPullToRefresh
- 织梦导航标签调用
- 16周作业
- JSON知识
- 黑马程序员——Java基础---泛型
- window.onresize 事件笔记
- android问题解决
- 初探浅拷贝与深拷贝
- 在Xcode中添加pch文件的操作
- Eclipse4.4 安装java反编译插件Eclipse Class Decompiler
- ASCII码值从小到大的排列顺序