Como crio outro eento para outro botao

5 respostas Resolvido
java
Pantrol

como crio outro evento para o botão sair??

public class Principal implements ActionListener {

    	private JFrame frame1;
    	public JButton btn_ENTRAR;
    	public JButton btn_SAIR;
    	private JFrame frame2;
    	private boolean ligado =true;
    	

    	public Principal() {
    		janelas();

    	}

    	public void janelas() {
    		
    		this.frame1 = new JFrame();
    		this.frame1.setTitle("Janela 1");
    		this.frame1.setPreferredSize(new Dimension(400, 400));
    		this.frame1.pack();
    		this.frame1.setLayout(null);
    		this.frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		this.frame1.setLocationRelativeTo(null);
    		this.frame1.setVisible(true);

    		this.frame2 = new JFrame();
    		this.frame2.setTitle("Janela 2");
    		this.frame2.setPreferredSize(new Dimension(400, 400));
    		this.frame2.pack();
    		this.frame2.setLayout(null);
    		this.frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		this.frame2.setLocationRelativeTo(null);
    		this.frame2.setVisible(false);
    		componentes();
    	}

    	public void componentes() {

    		this.btn_ENTRAR= new JButton("ENTRAR!");
    		this.btn_ENTRAR.setBounds(100,100,100,30);
    		this.btn_ENTRAR.addActionListener(this);
    		this.frame1.add(btn_ENTRAR);
    		//
    		this.btn_SAIR= new JButton("SAIR!");
    		this.btn_SAIR.setBounds(100,100,100,30);
    		this.frame2.add(btn_SAIR);	
    	}

    	public static void main(String[] args) {

    		new Principal();

    	}
    	
    	public void texte() {
    	
    	}

    	@Override
    	public void actionPerformed(ActionEvent ac) {	
    		this.frame1.setVisible(false);
    		this.frame2.setVisible(true);		
    	}
    	@Override
    	public void actionPerformed(ActionEvent ac) {	
    		this.frame1.setVisible(true);
    		this.frame2.setVisible(false);		
    	}

    }

5 Respostas

Lucas_Camara

Ao fazer sua classe implementar ActionListener, vc está dizendo que ela terá um único método responsável por alguma ação.

Seria melhor tu criar as ações de cada botão em classes separadas ou fazer a implementação inline.

Inline seria assim:

this.btn_ENTRAR.addActionListener(new ActionListener() {
	@Override
    public void actionPerformed(ActionEvent ac) {	
    	this.frame1.setVisible(false);
    	this.frame2.setVisible(true);		
   	}
});
Pantrol

hummmmm suspeitei desde o princípios q so criava uma :star_struck: amo esse Fórum, valew brother.

Pantrol

brother fiz assim porem quando clico na segunda janela ela abre a primeira mas nao fecha…
public class Principal implements ActionListener {

private JFrame frame1;
	public JButton btn_ENTRAR;
	public JButton btn_SAIR;
	private JFrame frame2;

	public Principal() {
		janelas();

	}

	public void janelas() {
		
		this.frame1 = new JFrame();
		this.frame1.setTitle("Janela 1");
		this.frame1.setPreferredSize(new Dimension(400, 400));
		this.frame1.pack();
		this.frame1.setLayout(null);
		this.frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.frame1.setLocationRelativeTo(null);
		this.frame1.setVisible(true);

		this.frame2 = new JFrame();
		this.frame2.setTitle("Janela 2");
		this.frame2.setPreferredSize(new Dimension(400, 400));
		this.frame2.pack();
		this.frame2.setLayout(null);
		this.frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.frame2.setLocationRelativeTo(null);
		this.frame2.setVisible(false);
		componentes();
	}

	public void componentes() {
		ClassePrimaria p = new ClassePrimaria();
		this.btn_ENTRAR= new JButton("ENTRAR!");
		this.btn_ENTRAR.setBounds(100,100,100,30);
		this.btn_ENTRAR.addActionListener(this);
		this.frame1.add(btn_ENTRAR);
		//
		this.btn_SAIR= new JButton("SAIR!");
		this.btn_SAIR.setBounds(100,100,100,30);
		this.btn_SAIR.addActionListener(p);
		this.frame2.add(btn_SAIR);	
	}

	public static void main(String[] args) {

		new Principal();

	}
	
	public void texte() {
	
	}

	@Override
	public void actionPerformed(ActionEvent chamaJanela2) {	
		this.frame1.setVisible(false);
		this.frame2.setVisible(true);	
		//
	
	}

	public JFrame getFrame1() {
		return frame1;
	}


	public JFrame getFrame2() {
		return frame2;
	}

}

//////////////////////////////////////
public class ClassePrimaria implements ActionListener {

public ClassePrimaria() {
		
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		Principal peincipal = new Principal();
		peincipal.getFrame1().setVisible(true);
		peincipal.getFrame2().setVisible(false);
	}

}
Lucas_Camara
Solucao aceita

O problema é que vc está criando uma nova instância de Principal na classe ClassePrimaria:

Principal peincipal = new Principal();

Isso irá criar novas referências e não afetará as referências já existentes. Para funcionar, vc teria que passar a instância da classe Principal como parâmetro para a classe ClassePrimaria. Algo assim:

ClassePrimaria p = new ClassePrimaria(this);

e a classe ClassePrimaria ficaria assim:

public class ClassePrimaria implements ActionListener {

	private final Principal principal;	

	public ClassePrimaria(Principal principal) {
		this.principal = principal;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		principal.getFrame1().setVisible(true);
		principal.getFrame2().setVisible(false);
	}
}
Pantrol

humm entendi. Funcionou^^

Criado 24 de junho de 2021
Ultima resposta 25 de jun. de 2021
Respostas 5
Participantes 2