import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Practise{
public static void main(String[] args) {
new MyFrame();
}
}
class MyFrame extends JFrame
{
public MyFrame()
{
final JButton btnChangeImage=new JButton("改变图标");
btnChangeImage.setIcon(new ImageIcon(".\\images\\icon1.png"));
btnChangeImage.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
btnChangeImage.setIcon(new ImageIcon(".\\images\\icon2.png"));
}
});
this.getContentPane().add(btnChangeImage);
this.setSize(300,200);
this.setLayout(new FlowLayout());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//用户单击“关闭”按钮时关闭窗口
this.setVisible(true);
}
}
温馨提示:答案为网友推荐,仅供参考