📄 picture.java
字号:
/**
* <p>Title:Picture</p>
* <p>Discription:实现图片的点击切换</p>
* <p>Disadvantage:</p>
* @author DELL
* @version 1.0
*/
package Project1;
import java.awt.*;
import java.util.*;
import java.text.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class Picture extends JFrame{
private JButton button = new JButton();
private ImageIcon[] myPicture ={
new ImageIcon("D:\\workspace\\pictures\\0.gif"),
new ImageIcon("D:\\workspace\\pictures\\1.gif"),
new ImageIcon("D:\\workspace\\pictures\\2.gif"),
new ImageIcon("D:\\workspace\\pictures\\3.gif"),
new ImageIcon("D:\\workspace\\pictures\\4.gif"),
new ImageIcon("D:\\workspace\\pictures\\5.gif"),
new ImageIcon("D:\\workspace\\pictures\\6.gif"),
new ImageIcon("D:\\workspace\\pictures\\7.gif"),
new ImageIcon("D:\\workspace\\pictures\\8.gif"),
new ImageIcon("D:\\workspace\\pictures\\9.gif"),
};
public JButton run(){
button.setIcon(myPicture[0]);
button.addActionListener(new buttonListener());
return button;
}
class buttonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Random r = new Random();
int i = r.nextInt(9);
button.setIcon(myPicture[i]);
}
}
public static void main(String[] args){
JFrame frame = new JFrame();
frame.setSize(500, 500);
frame.setVisible(true);
Picture p = new Picture();
p.run();
frame.getContentPane().add(p.run());
p.button.setSize(300, 233);
p.button.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -