Mudar img com clique

1 resposta
O

Tipo, preciso clicar no no botao resete e mudar a img no button1... segue meu codigo onde nao entendi pq nao funciona:

import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.border.Border;



class teste implements ActionListener {
    public JButton button1, resete;
public JLabel titu;

    public teste() {
    }
 
    public void aparencia(){

String iconex = "iconjogo.gif";
ImageIcon icone1 = new ImageIcon(iconex); 

       JFrame frame = new JFrame();
       Border empty;
       empty = BorderFactory.createEmptyBorder();

       Container cons = frame.getContentPane();
       
       GridBagLayout layout = new GridBagLayout();
       GridBagConstraints c = new GridBagConstraints();
       
       
       frame.setLayout(layout);
       //c.insets = new Insets(5,5,5,5);
       c.anchor = GridBagConstraints.CENTER;


       JButton resete = new JButton("Reset");
       c.weightx=1.0;
       c.gridwidth = 4;
       c.gridx=0;
       c.gridy=3;
       c.fill = GridBagConstraints.BOTH;
      

       cons.add(resete, c);

       JButton botao = new JButton("OK");
       c.gridwidth = 1;
       c.gridx=0;
       c.gridy=2;
       c.fill = GridBagConstraints.NONE;
       cons.add(botao, c);
   
       JButton botao2 = new JButton("Novo");
       c.fill = GridBagConstraints.NONE;
       c.gridwidth = 1;
       c.gridx=3;
       c.gridy=2;
       cons.add(botao2, c);
       
       //icons      
       JButton button1 = new JButton();
       c.fill = GridBagConstraints.NONE;
       c.gridx=0;
       c.gridy=0;
       button1.setIcon(icone1);
       button1.setBorder(empty);
              cons.add(button1, c);
       
       //      
       JButton button2 = new JButton(); 
       
       c.gridx=1;
       c.gridy=0;
       button2.setIcon(icone1);
       button2.setBorder(empty);
       button2.setName("button2");
      //addCompForBorder(button2, "empty border", simpleBorders);

       cons.add(button2, c);
       
       //
              
            
       frame.setSize(600,600);
       frame.setVisible(true);
      
    
       resete.addActionListener(this);
            
       frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  }
    
        public void actionPerformed(ActionEvent e){
       button1.setIcon(new ImageIcon("iconcirculo.gif"));
     
        }
  }
public class corpo {
       public static void main(String args[]) {
       teste a = new teste();
         a.aparencia();

       
      }
}

Se possivel uma soluçao e explicaçao pq o meu nao funfa

1 Resposta

M

Tudo Beleza? Tá quebrando muito a head ai?Será que é isso que você tava querendo?

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.*;

class corpo extends JFrame implements ActionListener {
public JButton button1, resete;

public corpo() {
	super("TESTE");
	ImageIcon icone = new ImageIcon ("Fernando.gif");
	button1 = new JButton("Teste");
	button1.setIcon (icone);
	setSize( 300, 300 );

	resete = new JButton("Reset");
	getContentPane().setLayout( new FlowLayout() );
	getContentPane().add( button1, "North" );
	getContentPane().add( resete, "South" );
	resete.addActionListener(this);
	button1.addActionListener( this );
	setVisible( true );
	setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

}

//fim metodo
public void actionPerformed(ActionEvent e) {

if(e.getSource() == button1)

{

//fiz pra teste o exit

button1.setIcon (new ImageIcon (teste.jpg));
}
       else {
       	   button1.setBackground( java.awt.Color.GREEN );
       }
       
   }
public static void main(String args[]) {

corpo a = new corpo();

a.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

}
}

Criado 10 de julho de 2005
Ultima resposta 10 de jul. de 2005
Respostas 1
Participantes 2