正在做一个JavaSwing的小程序,房间JButton里面的背景图片,有四种状态,分别是
空闲,占用,停用和预定,如何让实现JButton自动判断,去获取图片啊,劳烦大佬们
给个省事的方法谢谢
可根据图片的颜色和特征去识别。
给你个思路 可以自己定义一个Button 根据传进去的参数不同 设置不同的图片
public class MyButton extends JButton {
ImageIcon icon0 = new ImageIcon("image0.jpg");
ImageIcon icon1 = new ImageIcon("image1.jpg");
ImageIcon icon2 = new ImageIcon("image2.jpg");
ImageIcon icon3 = new ImageIcon("image3.jpg");
public MyButton(int colorCode) {// 可以用0123表示四种颜色
switch (colorCode) {
case 0:
this.setIcon(icon0);
break;
case 1:
this.setIcon(icon1);
break;
case 2:
this.setIcon(icon2);
break;
case 3:
this.setIcon(icon3);
break;
default:
break;
}
}
}
判断鼠标的位置
然后找出在那个范围内的button
接着就换图片自己添加个mouselistener?
下载资料去看看
链接:https://pan.baidu.com/s/17wYCKWYDN1CdlOypxaS3Bg 密码:kckz
public void function(String[] arg){
JButton jb = new JButton();
Icon icon = null;
if(占用){
icon = createIcon("占用图片的路径");
}else if(空闲){
icon = createIcon("空闲图片的路径");
}else if(停用){
icon = createIcon("停用图片的路径");
}else{
icon = createIcon("预定图片的路径");
}
jb.setIcon(icon);
}
private Icon createIcon(String path){
URL url = new File(path).toURI().toURL();
return new Icon(url);
}
希望可以帮到你。
public class MyButton extends JButton {
ImageIcon icon0 = new ImageIcon("image0.jpg");
ImageIcon icon1 = new ImageIcon("image1.jpg");
ImageIcon icon2 = new ImageIcon("image2.jpg");
ImageIcon icon3 = new ImageIcon("image3.jpg");
public MyButton(int colorCode) {// 可以用0123表示四种颜色
switch (colorCode) {
case 0:
this.setIcon(icon0);
break;
case 1:
this.setIcon(icon1);
break;
case 2:
this.setIcon(icon2);
break;
case 3:
this.setIcon(icon3);
break;
default:
break;
}
}
}