图片打开器

来源:互联网 发布:记事本软件哪个好 编辑:程序博客网 时间:2024/04/30 20:53

       25号考完试后,华信就开课了,从26号开始到30号,要从下午两点半到晚上九点半上课,写代码,这几天我写了图片打开器。

图片打开器能够打开单个图片,也能打开一个文件夹里的所有图片,就是用文件搜索的原理搜索图片文件,然后用卡片布局一个个打开。代码如下:

package 图片查看器;import java.awt.BorderLayout;import java.awt.CardLayout;import java.awt.Color;import java.awt.FlowLayout;import java.awt.Graphics;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.io.File;import java.util.ArrayList;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JPanel;import javax.swing.JTextField;import javax.swing.UIManager;public class SearchP extends JFrame {public JPanel picPanel;// 创建图片面板public JPanel WindowPanel;// 创建窗体面板public JPanel ButtonPanel;// 创建按钮面板public JMenuBar menu;// 创建菜单项public Graphics g;public String path;public CardLayout cl = new CardLayout();;public static ArrayList<String> list = new ArrayList<String>();public static void main(String[] args) {// 设置程序打开界面try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());} catch (Exception e) {e.printStackTrace();}new SearchP().initFrame();}public void initFrame() {setTitle("图片查看器");setSize(800, 600);setDefaultCloseOperation(3);setLocationRelativeTo(null);// 窗体面板设置布局WindowPanel = new JPanel();BorderLayout bd = new BorderLayout();WindowPanel.setLayout(bd);// 创建菜单项menu = new JMenuBar();JMenu jm = new JMenu("文件");JMenuItem jmi = new JMenuItem("打开");JMenuItem jmi2 = new JMenuItem("路径");jm.add(jmi);jm.add(jmi2);menu.add(jm);WindowPanel.add(menu, bd.NORTH);// 创建按钮面板ButtonPanel = new JPanel();ButtonPanel.setBackground(Color.green);ActionListener button_aciton = new ActionListener() {public void actionPerformed(ActionEvent e) {// 监听按钮面板的按钮String s = e.getActionCommand();if("下一张".equals(s)){cl.next(picPanel);}if("上一张".equals(s)){cl.previous(picPanel);}if("第一张".equals(s)){cl.first(picPanel);}if("最后一张".equals(s)){cl.last(picPanel);}}};String[] ButtonName = { "第一张", "上一张", "下一张", "最后一张" };for (int i = 0; i < 4; i++) {JButton jb = new JButton(ButtonName[i]);jb.setActionCommand(ButtonName[i]);ButtonPanel.add(jb);jb.addActionListener(button_aciton);}WindowPanel.add(ButtonPanel, bd.SOUTH);// 创建图片面板picPanel = new JPanel();WindowPanel.add(picPanel);picPanel.setBackground(Color.gray);picPanel.setLayout(cl);this.add(WindowPanel);setVisible(true);ActionListener jmi_action = new ActionListener() {public void actionPerformed(ActionEvent e) {// 监听打开文件的操作openfile();}};ActionListener jmi2_aciton = new ActionListener() {public void actionPerformed(ActionEvent arg0) {// 监听打开路径JTextField jt = new JTextField(30);JButton jb = new JButton("确定");JFrame jf = new JFrame("选择文件目录");jf.setSize(300, 80);jf.setLocationRelativeTo(null);jf.setLayout(new FlowLayout());jf.add(jt);jf.add(jb);jf.setVisible(true);jf.setDefaultCloseOperation(2);ActionListener jbaction = new ActionListener() {public void actionPerformed(ActionEvent e) {path = jt.getText();openDirectynext(path);for(int i=0;i<list.size();i++){JPanel jp = new JPanel();JLabel jl = new JLabel();ImageIcon icon = new ImageIcon(list.get(i));jl.setIcon(icon);jp.add(jl);picPanel.add(jp);}}};jb.addActionListener(jbaction);}};jmi.addActionListener(jmi_action);jmi2.addActionListener(jmi2_aciton);MouseAdapter adapter = new MouseAdapter() {public void mouseClicked(MouseEvent e) {cl.next(picPanel);//cl.previous(picPanel);//cl.first(picPanel);//cl.last(SearchP.this.getContentPane());}};this.addMouseListener(adapter);;}// 实现监听打开文件的操作public void openfile() {JFileChooser jfc = new JFileChooser();jfc.showOpenDialog(null);File file = jfc.getSelectedFile();if (file.getAbsolutePath().substring(file.getAbsolutePath().length() - 4).equals(".gif")) {JPanel newPanel = new JPanel();ImageIcon icon = new ImageIcon(file.getAbsolutePath());JLabel jl = new JLabel(icon);newPanel.add(jl);picPanel.add(newPanel);newPanel.updateUI();}if (file.getAbsolutePath().substring(file.getAbsolutePath().length() - 4).equals(".png")) {JPanel newPanel = new JPanel();ImageIcon icon = new ImageIcon(file.getAbsolutePath());JLabel jl = new JLabel(icon);newPanel.add(jl);picPanel.add(newPanel);newPanel.updateUI();}}// 实现按钮的操作public void buttonAction() {}// 实现路径打开public void openDirecty() {}public void openDirectynext(String path) {File file = new File(path);if (file.exists()) {if (file.isFile()) {if (file.getName().endsWith(".png")) {list.add(file.getAbsolutePath());}} else if (file.isDirectory()) {File[] files = file.listFiles();for (File f : files) {openDirectynext(f.getAbsolutePath());}}}}}
这就是文件打开器

1 0
原创粉丝点击