圖片檢索程式

來源:互聯網
上載者:User

 /*
@此程式的目的是通過檔案過濾器來過濾檔案.
@作者:李洪亮
*/

import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import java.util.*;
import javax.swing.event.*;
import java.awt.*;
import javax.swing.filechooser.FileFilter;

public class CheckImageDemo extends JFrame{
private JMenu menu,helpmenu;
private JMenuItem openFile,exititem,helpitem;
private JLabel label,label1;
private JScrollPane jscrollPane;
private JMenuBar menuBar;
private JFileChooser fileChooser;

public CheckImageDemo(){
super("檢索圖片");
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent ev){
System.exit(0);
}
});
menuBar=new JMenuBar();
menu=new JMenu("檔案");
openFile=new JMenuItem("開啟");
exititem=new JMenuItem("退出");
setJMenuBar(menuBar);
menuBar.add(menu);
menu.add(openFile);
menu.add(exititem);
/*******************************************************************************
  檔案“退出”的監聽事件
*******************************************************************************/
exititem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
helpmenu=new JMenu("關於");
helpitem=new JMenuItem("關於檢索圖片器");
helpmenu.add(helpitem);
menuBar.add(helpmenu);
/*******************************************************************************
  協助“關於”的監聽事件
*******************************************************************************/
helpitem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String str="歡迎你使用本程式!"+"/n"+
"程式著作權歸作者所有@轉載請註明作者身份"+"/n"+
"作者:李洪亮 Update:2008-04-03"+"/n"+
"更多資訊請訪問:http://lhl54861287.qyun.net";
JOptionPane.showMessageDialog(null,str,"關於檢索圖片",JOptionPane.INFORMATION_MESSAGE);
}
});
label=new JLabel("",SwingConstants.CENTER);
label1=new JLabel("",SwingConstants.CENTER);
label1.setText("映像檢索中心");
label.setIcon(new ImageIcon("1.gif"));
jscrollPane=new JScrollPane();
jscrollPane.getViewport().add(label);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(jscrollPane,BorderLayout.CENTER);
getContentPane().add(label1,BorderLayout.SOUTH);
fileChooser=new JFileChooser();
JFileFilter filter=new JFileFilter();
filter.addType("jpg");
filter.addType("gif");
filter.setDescription("IMG archives");
fileChooser.addChoosableFileFilter(filter);
/*******************************************************************************
  檔案“開啟”的監聽事件
*******************************************************************************/
openFile.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int r=fileChooser.showOpenDialog(null);
if(r==JFileChooser.APPROVE_OPTION){
String pathname=fileChooser.getSelectedFile().getPath();
label1.setText("你所開啟圖片的路徑為:"+pathname);
label.setIcon(new ImageIcon(pathname));
}
}
});
}

class JFileFilter extends FileFilter{
protected String description;
protected ArrayList arraylist=new ArrayList();

public void addType(String s){
arraylist.add(s);
}

public void setDescription(String s){
description=s;
}

public String getDescription(){
return description;
}

public boolean accept(File f){
if(f.isDirectory()){
 return true;
}else if(f.isFile()){
Iterator it=arraylist.iterator();
while(it.hasNext()){
if(f.getName().endsWith((String)it.next())) return true;
}
}
return false;
}
}

public static void main(String args[]){
CheckImageDemo frame=new CheckImageDemo();
frame.setSize(800,450);
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize=frame.getSize();
frame.setLocation((screenSize.width-frameSize.width)/2,(screenSize.height-frameSize.height)/2);
frame.setVisible(true);
}
}

 

 

 

 

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.