Ecco inserisco tutto completo, spero che si capisca qualcosa.
codice:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class CaricaImmagini extends JFrame {
static boolean i = false ;
ImageIcon img = new ImageIcon("icc.jpg");
ImageIcon ver = new ImageIcon("ros.jpg");
public CaricaImmagini(){
super("Carica Immagine");
setSize(300, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
creaico();
add(new Pannello());
setVisible(true);
}
public void creaico(){
JButton b1, b2, b3 ;
b1 = new JButton(img);
b1.setBounds(50,20,60,50);
b1.setBackground(new Color(0,0,0,0));
b1.addActionListener(new Sposta());
add(b1);
b2 = new JButton(img);
b2.setBounds(50,150,60,50);
b2.setBackground(new Color(0,0,0,0));
add(b2);
if( i == true){
remove(b1);
ImageIcon lol = new ImageIcon("ver.jpg");
b1 = new JButton(lol);
b1.setBounds(50,20,60,50);
b1.setBackground(new Color(0,0,0,0));
b1.addActionListener(new Sposta());
add(b1);
}
}
public class Sposta implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if (i == true ){ i=false ; creaico(); }
if (i == false){ i=true ; creaico(); }
}
}
class Pannello extends JPanel{
public void paint(Graphics g){
g.drawImage(new ImageIcon("ros.jpg").getImage(), 0, 0, null);
g.drawImage(new ImageIcon("ver.jpg").getImage(), 0, 100, null);
}
}
public static void main(String[] args){
try{
SwingUtilities.invokeLater(new Runnable(){
public void run(){ new CaricaImmagini(); }
});
}catch(Exception e){ }
}
}