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(); } }
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/10805.html