Feliz o Triste
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class felizotriste extends JFrame implements ActionListener {
private JButton botonfeliz, botontriste;
private JPanel panel;
private ImageIcon imagenfeliz, imagentriste;
public static void main(String[] args) {
felizotriste demo=new felizotriste();
demo.setSize(180,180);
demo.setTitle("imagen");
demo.crearGUI();
demo.setVisible(true); }
private void crearGUI() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
Container ventana=getContentPane();
ventana.setLayout(new FlowLayout());
panel=new JPanel();
panel.setPreferredSize(new Dimension(100,100));
panel.setBackground(Color.white);
ventana.add(panel);
botonfeliz=new JButton("feliz");
ventana.add(botonfeliz);
botonfeliz.addActionListener(this);
botontriste=new JButton("triste");
ventana.add(botontriste);
botontriste.addActionListener(this);
imagenfeliz=new ImageIcon("feliz.jpg");
imagentriste=new ImageIcon("triste.jpg"); }
@Override
public void actionPerformed(ActionEvent event) {
Graphics papel=panel.getGraphics();
Object origen=event.getSource();
if (origen==botonfeliz) {imagenfeliz.paintIcon(this, papel, 0, 0); }
else {imagentriste.paintIcon(this, papel, 0, 0); }
}
}