作业 GUI编程同步
2015-07-01 11:55
225 查看
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()); // 设置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(); } } }); btnNewButton.setBounds(194, 37, 153, 28); contentPane.add(btnNewButton); } }
相关文章推荐
- uialertview 设置输入的位数限制
- 安装win7或win8系统时UEFI和Legacy模式的设置
- 又到毕业季:用“创意”吸引企业
- uiview 永久旋转动画
- 第三次作业GUI文本编辑器
- iOS UINavigationController跳转到指定页面
- [SoapUI] 通过编程的方式设置当前的Environment
- iOS UIScrollView左右滑动手势与返回手势冲突的解决办法
- js的 valueOf & toString
- UILabel 设置不同颜色,不同字体
- uitableview中刷新单个section和row
- Ueditor 单独使用上传图片及上传附件方法
- Java基础——GUI
- Guice 学习(七)常量和属性的注入( Constant and Property Inject)
- Guice 学习(六)使用Provider注入服务( Provider Inject Service)
- 深入理解ServletRequest与ServletResponse
- EazyUI TreeGrid分页、查询
- 【转】Uiautomator Api浅析
- Guice 学习(五)多接口的实现( Many Interface Implementation)
- 《Java程序设计》实训每天一博文之第二天 附:14周作业:GUI初步编程