簡單的 java圖片瀏覽器示例
import java.awt.;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.awt.image.ImageProducer;
import java.io.File;
import javax.swing.;
public class vicePic implements ActionListener {
JLabel imgLable;
JFrame mainJframe;
Container con;
JTextField fileFiled;
JButton openBtn,cutBtn;
JPanel pane;
JScrollPane spanel;
ImageIcon img;
public vicePic(){
mainJframe=new JFrame("圖形顯示比例");
con=mainJframe.getContentPane();
pane=new JPanel();
pane.setLayout(new FlowLayout());
openBtn=new JButton("打開文件");
openBtn.addActionListener(this);
cutBtn=new JButton("剪切圖片");
cutBtn.addActionListener(this);
fileFiled=new JTextField();
fileFiled.setColumns(20);
pane.add(fileFiled);
pane.add(openBtn);
pane.add(cutBtn);
imgLable =new JLabel();
spanel=new JScrollPane(imgLable);
con.add(pane,BorderLayout.NORTH);
con.add(spanel,BorderLayout.CENTER);
mainJframe.setSize(800, 800);
mainJframe.setVisible(true);
mainJframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
String cmd=e.getActionCommand();
ImageFilter cropFilter;
Image croppedImage;
ImageProducer produer;
if(cmd.equals("打開文件")){
try{
JFileChooser chooser=new JFileChooser();
if(chooser.showOpenDialog(mainJframe)==JFileChooser.APPROVE_OPTION){
File tempfile=chooser.getSelectedFile();
fileFiled.setText(tempfile.toString());
img=new ImageIcon(fileFiled.getText());
imgLable.setIcon(img);
}http://www.huiyi8.com/jiaoben/
}catch(Exception el){
JOptionPane.showMessageDialog(mainJframe, "圖品無法顯示");
}
}
if(cmd.equals("剪切圖片")){
cropFilter=new CropImageFilter(100, 100, 200, 200);
produer=new FilteredImageSource(img.getImage().getSource(), cropFilter);
croppedImage=Toolkit.getDefaultToolkit().createImage(produer);
imgLable.setIcon(new ImageIcon(croppedImage));
}
}
public static void main(String[] args){
new vicePic();
}
}</pre>
本文由用戶 6x7d 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!