《Java课程实习》日志(周三)
2015-07-01 11:30
489 查看
Java程序设计《猜猜看》游戏 进程…
做一个那个GUI那个,有一个按钮(JButton)和一个标签(JLabel),当点击
按钮后,能够显示下一张图片。
![](http://img.blog.csdn.net/20150701113903541?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzg5OTY4MQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](http://img.blog.csdn.net/20150701113934625?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzg5OTY4MQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
增加图像缩放
做一个那个GUI那个,有一个按钮(JButton)和一个标签(JLabel),当点击
按钮后,能够显示下一张图片。
import java.awt.BorderLayout; import java.awt.EventQueue; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JPanel; 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 ImgDemo extends JFrame { private JPanel contentPane; private int idImg = 1; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { ImgDemo frame = new ImgDemo(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public ImgDemo() { setTitle("\u56FE\u7247\u663E\u793A"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 576, 404); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JLabel lblNewLabel = new JLabel("New label"); lblNewLabel.setBackground(Color.YELLOW); lblNewLabel.setForeground(Color.PINK); lblNewLabel.setBounds(48, 86, 467, 248); contentPane.add(lblNewLabel); JButton btnNewButton = new JButton("\u663E\u793A\u4E0B\u4E00\u5F20\u56FE\u7247"); btnNewButton.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))); lblNewLabel.setBounds(48, 86, icon.getIconWidth(), icon.getIconHeight()); lblNewLabel.setIcon(icon); idImg = idImg+1; if(idImg==10) idImg = 1; } catch (IOException e1) { e1.printStackTrace(); } } }); btnNewButton.setBounds(194, 37, 153, 28); contentPane.add(btnNewButton); } }
增加图像缩放
import java.awt.EventQueue; import java.awt.Image; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JPanel; 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 ImgDemo extends JFrame { private JPanel contentPane; private int idImg = 1; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { ImgDemo frame = new ImgDemo(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public ImgDemo() { setTitle("\u56FE\u7247\u663E\u793A"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 576, 404); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JLabel lblNewLabel = new JLabel("New label"); lblNewLabel.setBackground(Color.YELLOW); lblNewLabel.setForeground(Color.PINK); lblNewLabel.setBounds(48, 86, 400, 400); contentPane.add(lblNewLabel); JButton btnNewButton = new JButton("\u663E\u793A\u4E0B\u4E00\u5F20\u56FE\u7247"); btnNewButton.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(400,400,Image.SCALE_FAST); icon = new ImageIcon(smallImage); lblNewLabel.setIcon(icon); idImg = idImg+1; if(idImg==10) idImg = 1; } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }); btnNewButton.setBounds(194, 37, 153, 28); contentPane.add(btnNewButton); } }
相关文章推荐
- java
- Java - How Class is loaded and initialized
- java静态内部类
- Spring2.5学习4.1_AOP
- java 集合(list、set、map)的特点
- java集合总结(转载)
- Java中的Serializable接口和transient关键字
- Spring IOC的实现
- Java - Why String is Immutable or Final in Java
- Fragment中使用出现的java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
- Java - Why multiple inheritances are not supported in Java
- Spring MVC拦截器+注解方式实现防止表单重复提交
- 《Java课程实习》日志(周二)
- JDBC初探
- 一、Spring源码分析——Spring框架
- java.sql.SQLException: Listener refused the connection with the following error:
- java集合框架知识总结
- LeetCode_71---Simplify Path
- 【Java EE 学习 24 上】【注解详解】
- 用java对文件进行分割与合并