做的一个小程序,分享一下,给初学者一个示例吧。。我就是初学的。功能:显示鼠标的位置。。。。有几个图片,点击后会改变表情用label存文本信息,用几个label存图片,把每个label定位在form上。只要把代码考过去就好了。在根目录下创建一个image文件夹,放两张小表情,happy.gif,unhappy.gif 或者自己修改路径也行我把项目放到网上去

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.event.*;
public class imageshow {
public static void main(String[] args) {
JFrame frame1 = new winform();
frame1.setVisible(true);//显示
}
}
class winform extends JFrame{
public JLabel label1; //label标签
public JLabel label2; //用于显示图片 label2---7
public JLabel label3;
public JLabel label4;
public JLabel label5;
public JLabel label6;
public JLabel label7;
public JLabel label8;//隐藏,因为它会出现在最左边,很讨厌
public String filename1 = "image/happy.gif";//文件路径
public String filename2 = "image/unhappy.gif";
public ImageIcon image1 = null;
public ImageIcon image2 = null;
int x[] = new int[9]; //记录第i个label现在的图片
public winform( ){
for(int i=0;i<9;i++) x[i]= 0;
setTitle("窗口小程序");
setSize(500, 400);
setLocationRelativeTo(null); // 初始在屏幕中央
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 使点击红X可以退出程序
label1 = new JLabel();
label1.setText("鼠标指针的位置: ");
label1.setFont(new java.awt.Font("Dialog",1,18)); //设置文子格式,
//label1.setHorizontalAlignment(JLabel.LEFT);
//label1.setVerticalAlignment(JLabel.TOP);
label1.setBounds(10, 10, 300, 40);//x,y,width,height, 坐标x,y,宽度,高度
//label1.setLocation(0,0);
getContentPane().add(label1); //加入form中
image1 =new ImageIcon(filename1);//获取图片
image2 = new ImageIcon(filename2);
label2 = new JLabel();
label2.setBounds(300, 100, 30, 30);
label2.setIcon(image1);//设置label的图片
//label2.setLocation(100,100);
getContentPane().add(label2);
label3 = new JLabel();
label3.setBounds(250, 150, 30, 30);
label3.setIcon(image1);
//label3.setLocation(100,100);
getContentPane().add(label3);
label4 = new JLabel();
label4.setBounds(350, 150, 30, 30);
label4.setIcon(image1);
//label4.setLocation(100,100);
getContentPane().add(label4);
label5 = new JLabel();
label5.setBounds(200, 200, 30, 30);
label5.setIcon(image1);
//label5.setLocation(100,100);
getContentPane().add(label5);
label6 = new JLabel();
label6.setBounds(300, 200, 30, 30);
label6.setIcon(image1);
//label6.setLocation(100,100);
getContentPane().add(label6);
label7 = new JLabel();
label7.setBounds(400, 200, 30, 30);
label7.setIcon(image1);
//label7.setLocation(100,100);
getContentPane().add(label7);
label8 = new JLabel();
label8.setBounds(0, 0, 0, 0);
//label8.setIcon(image1);
label8.setLocation(000,000);
getContentPane().add(label8);
addMouseListener(
new MouseListener() {
//释放事件
public void mouseReleased(MouseEvent e){
tt( e.getX(), e.getY());
}
//进入事件
public void mouseEntered(MouseEvent e){
tt( e.getX(), e.getY());
}
//离开
public void mouseExited(MouseEvent e){
tt( e.getX(), e.getY());
}
//点击事件
public void mouseClicked(MouseEvent e) {
tt( e.getX(), e.getY());
}
//按下事件
public void mousePressed(MouseEvent e){
tt( e.getX(), e.getY());
}
}
);
}
public void tt(int x,int y)
{
label1.setText("鼠标指针的位置:"+x+":"+y);
if(300+5 < x && x < 300+30 && 100+30<y&&y<100+60)//定位用的
label2.setIcon(changeimage(2));
if(250+5 < x && x < 250+30 && 150+30<y&&y<150+60)
label3.setIcon(changeimage(3));
if(350+5 < x && x < 350+30 && 150+30<y&&y<150+60)
label4.setIcon(changeimage(4));
if(200+5 < x && x < 200+30 && 200+30<y&&y<200+60)
label5.setIcon(changeimage(5));
if(300+5 < x && x < 300+30 && 200+30<y&&y<200+60)
label6.setIcon(changeimage(6));
if(400+5 < x && x < 400+30 && 200+30<y&&y<200+60)
label7.setIcon(changeimage(7));
}
public ImageIcon changeimage(int y){//判断现在的图片,并且修改改变后的情况,并返回要改变成的图片
if(x[y] == 1){
x[y] = 2;
return image2;
}
else {
x[y] = 1;
return image1;
}
}
}