// 不加包,图片跟类文件在一个目录,命令行下编译执行就行了
创新互联建站是专业的江宁网站建设公司,江宁接单;提供网站设计、成都网站设计,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行江宁网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
// 如果建工程,图片放到工程根目录
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Student extends JFrame {
public Student() {
init();
}
private void init() {
final Container c = getContentPane();
final JLabel imgLabel = new JLabel();
JButton btnPic = new JButton("换背景图片");
btnPic.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ImageIcon img = new ImageIcon(System.getProperty("user.dir") + "\\a.png");
imgLabel.setIcon(img);
imgLabel.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());
}
});
((JPanel)getContentPane()).setOpaque(false);
getLayeredPane().add(imgLabel, new Integer(Integer.MIN_VALUE));
setLayout(new FlowLayout());
add(btnPic);
setSize(500, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Student().setVisible(true);
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class KeyCharEx extends JFrame {
JLabel la = new JLabel("Enter键换背景颜色");
KeyCharEx() {
super("请按Enter键");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
this.add(la);
this.addKeyListener(new MyKeyListener());// 整个界面添加键盘敲击事件
setSize(250, 150);
setVisible(true);
}
class MyKeyListener extends KeyAdapter {
public void keyTyped(KeyEvent e) {// 应该是这个方法 keyTyped 键盘按下,然后释放
super.keyTyped(e);
int r = (int) (Math.random() * 256);
int g = (int) (Math.random() * 256);
int b = (int) (Math.random() * 256);
switch (e.getKeyChar()) {
case '\n':
la.setText("r=" + r + ",g=" + g + ",b=" + b);
getContentPane().setBackground(new Color(r, g, b));
break;
case 'q':
System.exit(0);
}
}
}
public static void main(String[] args) {
new KeyCharEx();
}
}
这段代码和你提的问题类似,不过这个是直接自动切换的!你只需要把自动的哪块换个按钮,在换个触发事物的函数就可以了!
script
var count = 0;
var pics = new Array("a.jpg","b.jpg","c.jpg","d.jpg");
function changePic()
{
//得到img
var myPic = document.getElementById('pic');
//根据对象 修改DOM元素的属性
myPic.src = pics[count];
count++;
if(count == pics.length)
{
count = 0;
}
//调用函数
setTimeout('changePic()',2000);
}
/script
/HEAD
BODY onload="changePic()"
img src="#" height="500" width="500" id="pic"/
/BODY