Trabalhando com EVENTOS!

1 resposta
L

Estou tentando mudar a cor do meu botão 1 quando ele sofre um evento, porem não está funcionando!!!

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.Object;
import javax.swing.border.EmptyBorder;
import org.eclipse.wb.swing.FocusTraversalOnArray;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;

public class Layout extends JFrame implements ActionListener{ 
	 Container c;
	 JPanel identificador, menu, conteudo;
	 JLabel  ln, lt;
	 JButton m1, m2, m3, m4, m5;
	 
	public Layout(){
		super  ("Clinica Odontológica");
		setBackground(new Color(0, 175, 239));
		 setSize(1000, 700);
		 setVisible(true);
		 setLocation(10, 30); 
		 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		 c = getContentPane();
		 c.setBackground(new Color(230, 231, 232));
		 
		 menu = new JPanel();
		 menu.setBackground(new Color(255, 255, 255));
		 menu.setSize(new Dimension (1000, 200));
		 menu.setLocation(0, 20);
		 menu.setLayout(new GridLayout(1,2));
		 menu.setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[]{m1, m2, m3, m4, m5}));
		 
		 menu.add(m1 = new JButton("1"));
		 m1.setBackground(new Color(255, 255, 255));
		 
		 menu.add(m2 = new JButton("2"));
		 m2.setBackground(new Color(255, 255, 255));
		 
		 conteudo = new JPanel();
		 conteudo.setBackground(new Color(255, 255, 255));
				 GroupLayout groupLayout = new GroupLayout(c);
		 groupLayout.setHorizontalGroup(
		 	groupLayout.createParallelGroup(Alignment.LEADING)
		 		.addComponent(identificador, GroupLayout.DEFAULT_SIZE, 992, Short.MAX_VALUE)
		 		.addComponent(menu, GroupLayout.DEFAULT_SIZE, 992, Short.MAX_VALUE)
		 		.addComponent(conteudo, GroupLayout.DEFAULT_SIZE, 992, Short.MAX_VALUE)
		 );
		 
		 groupLayout.setVerticalGroup(
		 	groupLayout.createParallelGroup(Alignment.LEADING)
		 		.addGroup(groupLayout.createSequentialGroup()
		 			.addComponent(identificador, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
		 			.addPreferredGap(ComponentPlacement.RELATED)
		 			.addComponent(menu, GroupLayout.PREFERRED_SIZE, 79, GroupLayout.PREFERRED_SIZE)
		 			.addPreferredGap(ComponentPlacement.RELATED)
		 			.addComponent(conteudo, GroupLayout.DEFAULT_SIZE, 537, Short.MAX_VALUE))
		 );
		 
		 JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		 
		 GroupLayout gl_conteudo = new GroupLayout(conteudo);
		 gl_conteudo.setHorizontalGroup(
		 	gl_conteudo.createParallelGroup(Alignment.LEADING)
		 		.addGroup(gl_conteudo.createSequentialGroup()
		 			.addContainerGap()
		 			.addComponent(tabbedPane, GroupLayout.DEFAULT_SIZE, 964, Short.MAX_VALUE)
		 			.addContainerGap())
		 );
		 gl_conteudo.setVerticalGroup(
		 	gl_conteudo.createParallelGroup(Alignment.LEADING)
		 		.addGroup(gl_conteudo.createSequentialGroup()
		 			.addContainerGap()
		 			.addComponent(tabbedPane, GroupLayout.DEFAULT_SIZE, 546, Short.MAX_VALUE))
		 );
		 
		 c.setLayout(groupLayout);
		 conteudo.setLayout(gl_conteudo);
		 
		 
		
	}
	
	 public void actionPerformed(ActionEvent e) {
		  if (e.getSource() == m1){
			  m1.setBackground(new Color(230, 231, 232));
			  conteudo.setBackground(new Color(0, 175, 239));
		  }
	  }
	
	public static void main(String[] args) {
		
        JFrame Janela= new Layout();
       Janela.show();
       WindowListener x=new WindowAdapter(){
           public void windowClosing(WindowEvent e){
               System.exit(0);
           }
       };
    Janela.addWindowListener(x);
	
}
	}

1 Resposta

M

tente usar

m1.addActionListener(new java.awt.event.ActionListener() {  
		    public void actionPerformed(java.awt.event.ActionEvent evt) {  
		    
              m1.setBackground(new Color(230, 231, 232));  
              conteudo.setBackground(new Color(0, 175, 239));

}});
Criado 18 de setembro de 2012
Ultima resposta 18 de set. de 2012
Respostas 1
Participantes 2