标签:
承认,这次纯属敷衍。被虐了一晚上,我还是什么都没弄出来,离断网时间越来越近,便找了一篇过来发了个博文。明儿加油,起码有个小目标了。唉……import java.awt.BorderLayout; import java.awt.Component; import java.awt.EventQueue; import java.awt.FlowLayout; import java.awt.Image; import javax.imageio.ImageIO; import javax.swing.AbstractButton; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.SwingConstants; import javax.swing.border.EmptyBorder; import javax.swing.JButton; import javax.swing.JLabel; import java.awt.Color; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.io.File; import java.io.IOException; public class TableDatabase extends JFrame { private static final Component JButton = null; private JPanel contentPane; private int idImg = 1; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { TableDatabase frame = new TableDatabase(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public TableDatabase() { setTitle("图片显示"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 500); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); getContentPane().setLayout(new FlowLayout()); final JLabel lblNewLabel = new JLabel("New label"); lblNewLabel.setBackground(Color.YELLOW); lblNewLabel.setForeground(Color. LIGHT_GRAY); lblNewLabel.setBounds(60, 90, 400, 400); contentPane.add(lblNewLabel); /*@1 * */ JButton button_1 = new JButton("上一张"); button_1.setVerticalAlignment(SwingConstants.TOP); button_1.setBounds(145, 10, 69, 23); getContentPane().add(button_1); button_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { ImageIcon icon; try { String strImgFile = "E:/demoPhoto"; // 获取图片的路径 strImgFile = strImgFile + String.valueOf(idImg) + ".jpg";// 得到图片的完整路径 icon = new ImageIcon(ImageIO.read(new File(strImgFile)));// 读取图片 //第一种 Image image = icon.getImage(); // 从图表中获取到图片 Image smallImage = image.getScaledInstance(300,300,Image.SCALE_FAST);// 缩放图像 icon = new ImageIcon(smallImage); //把Image文件转化为ImageIcon /* 第二种方法 Image srcImg = ImageIO.read(new FileInputStream(strImgFile) );//取源图 int width = 200; //假设要缩小到600点像素 int height = srcImg.getHeight(null)*200/srcImg.getWidth(null);//按比例,将高度缩减 Image smallImg =srcImg.getScaledInstance(width, height, Image.SCALE_FAST);//缩小 icon = new ImageIcon(smallImg); */ lblNewLabel.setIcon(icon); // 为下一张图片做准备,一共9张图片,显示完第9张后,重新显示第一张 int idImg=1; idImg--; if(idImg==10) idImg = 1; else //System.out.println("没有可显示的了"); JOptionPane.showMessageDialog(null, "没有可显示的了");// } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }); button_1.setBounds(200, 50, 153, 30); contentPane.add(button_1); /*@2 * */ JButton button = new JButton("下一张"); button.setVerticalAlignment(SwingConstants.TOP); button.setBounds(219, 10, 69, 23); getContentPane().add(button); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { ImageIcon icon; try { String strImgFile = "E:/demoPhoto/"; // 获取图片的路径 strImgFile = strImgFile + String.valueOf(idImg) + ".jpg";// 得到图片的完整路径 icon = new ImageIcon(ImageIO.read(new File(strImgFile)));// 读取图片 //第一种 Image image = icon.getImage(); // 从图表中获取到图片 Image smallImage = image.getScaledInstance(300,300,Image.SCALE_FAST);// 缩放图像 icon = new ImageIcon(smallImage); //把Image文件转化为ImageIcon /* 第二种方法 Image srcImg = ImageIO.read(new FileInputStream(strImgFile) );//取源图 int width = 200; //假设要缩小到600点像素 int height = srcImg.getHeight(null)*200/srcImg.getWidth(null);//按比例,将高度缩减 Image smallImg =srcImg.getScaledInstance(width, height, Image.SCALE_FAST);//缩小 icon = new ImageIcon(smallImg); */ // 设置Jlabel标签的icon(图标) lblNewLabel.setIcon(icon); // 为下一张图片做准备,一共9张图片,显示完第9张后,重新显示第一张 idImg = idImg+1; if(idImg==10) idImg = 1; } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }); button.setBounds(200, 37, 153, 28); contentPane.add(button); } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/u014026824/article/details/46688729