• 16.13


    需要注意的是:image文件夹应该放在工程文件的文件夹处,而不是类文件夹里面

    经过了2天的思考这个题目最终还是做出来了。

    我的思路:由于题目提示是在框架中放置标签,然后将图像图标放置在标签中,于是我最开始想通过标签的初始化进行设计。但是这种方式是无法进行图标的改变的,因为一旦初始化以后,图标就已经固定且无法修改。

    所以这种思路思考了2天也是无果,最终只能在paintComponent()函数里面做文章。而paintComponent()函数就好处理得多,因为对它进行调用,会重绘JLabel,这样只要是使用了paintComponent()的绘图都会重新加载。于是我找到了g.drawImage(im[i], 0, 0, 800, 600,this); 把图标做成数组,然后最后一个this是我瞎填的,毕竟第一次使用这个,也不知道最后一个参数 ImageObserver observer 到底填什么,于是随便填了个this姑且当做就是这个对象。结果居然成功了!

     paintComponent会在初始化之前被swing调用,所以,一般如果初始化一个JComponent组件的话paintComponent会在初始化之前就调用一次

     1 import java.awt.*;
     2 import java.awt.event.ActionEvent;
     3 import java.awt.event.ActionListener;
     4 import java.util.Scanner;
     5 
     6 import javax.swing.*;
     7 
     8 public class Test_16_13 extends JFrame{
     9     Image im[] = {new ImageIcon("image/slide1.jpg").getImage(),new ImageIcon("image/slide2.jpg").getImage(),new ImageIcon("image/slide3.jpg").getImage(),
    10             new ImageIcon("image/slide4.jpg").getImage(),new ImageIcon("image/slide5.jpg").getImage()};
    11     
    12     public Test_16_13(){   //page353                
    13 //        JLabel1 j1 = new JLabel1();        
    14         add(new JLabel1());        
    15     }
    16     
    17     public static void  main(String[] args){    
    18         Test_16_13 frame = new Test_16_13();        
    19         frame.setSize(800, 600);
    20         frame.setTitle("Exercise15_8");
    21         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    22         frame.setLocationRelativeTo(null); // Center the frame
    23         frame.setVisible(true);        
    24     }   
    25     class JLabel1 extends JLabel{
    26         private ImageIcon ic;
    27         int x = 300,y = 300,i=-1;
    28 
    29         public JLabel1(){            
    30             i = 0;
    31             Timer timer = new Timer(30000,new TimerListener());
    32             timer.start();
    33         }
    34         public JLabel1(ImageIcon ic){
    35             super(ic);            
    36         }
    37         public JLabel1(String s){            
    38             super(s);  
    39             i = 0;      
    40             Timer timer = new Timer(3000,new TimerListener());
    41             timer.start();
    42         }
    43         public void setImageIcon(ImageIcon ic)
    44         {
    45             this.ic = ic;
    46         }
    47         protected void paintComponent(Graphics g){
    48             super.paintComponent(g);
    49             g.drawImage(im[i], 0, 0, 800, 600,this);  
    50             if(i>4 || i < 0)
    51                 i = 0;
    52             System.out.println(i);                      
    53             i++;    
    54         }
    55         class TimerListener implements ActionListener{
    56 
    57             @Override
    58             public void actionPerformed(ActionEvent e) {
    59                 // TODO Auto-generated method stub                
    60                 repaint();
    61             }            
    62             }
    63         }
    64 }
    Test_16_13.java
  • 相关阅读:
    Navicat连接mysql1862错误your password has expired.To log in you must change itusing a client that supports expired passwords
    机器学习听课 | 目录 | 00
    Git实战 | 其他 | 04
    练手SQL数据 | 目录 | 00
    练手SQL数据 | 区域自关联 | 02
    蓝桥杯训练 | 数学和简单DP | 03
    Python基础 | linux下Python的安装 | 03
    Java基础 | 目录 | 00
    初级-MySQL经典练习题及答案,常用SQL语句练习50题
    MYSQL必知必会-SQL语句查询
  • 原文地址:https://www.cnblogs.com/wanjiang/p/5645262.html
Copyright © 2020-2023  润新知