用java编写程序,单击按钮后改变按钮上的图标,(两个图标的改变 40
2个回答
展开全部
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.JPanel;
public class ImageButton extends JFrame implements ActionListener{
JButton jb;
ImageIcon img1 = new ImageIcon("D:\\1.jpg");
ImageIcon img2 = new ImageIcon("D:\\2.jpg");
public ImageButton()
{
jb = new JButton();
jb.setIcon(img1);
jb.addActionListener(this);
JPanel jp = new JPanel();
jp.add(jb);
add(jp);
setSize(400,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
if(jb.getIcon() == img1) jb.setIcon(img2);
else jb.setIcon(img1);
}
public static void main(String[] args)
{
new ImageButton().setVisible(true);
}
}
在D盘下放两张名 为1.jpg 和 2.jpg的图片
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ImageButton extends JFrame implements ActionListener{
JButton jb;
ImageIcon img1 = new ImageIcon("D:\\1.jpg");
ImageIcon img2 = new ImageIcon("D:\\2.jpg");
public ImageButton()
{
jb = new JButton();
jb.setIcon(img1);
jb.addActionListener(this);
JPanel jp = new JPanel();
jp.add(jb);
add(jp);
setSize(400,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
if(jb.getIcon() == img1) jb.setIcon(img2);
else jb.setIcon(img1);
}
public static void main(String[] args)
{
new ImageButton().setVisible(true);
}
}
在D盘下放两张名 为1.jpg 和 2.jpg的图片
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询