Add functions to graphic application buttons

0

I'm having trouble putting functions for buttons in java. Can anyone help?

Follow the code:

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Insets;


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class Painel extends JPanel{

    private static final Color COR_FUNDO = Color.RED;
    private int largura;
    private int altura;

    JFrame frame = new JFrame();


    public Painel(int altura, int largura){
        this.altura = altura;
        this.largura = largura;
        botaoAdicionar();
        botaoRemover();
        botaoConsultar();
        botaoPesquisar();

    }


    public void botaoAdicionar(){
        Botao adicionar = new Botao();
        adicionar.setText("Adicionar ator");
        adicionar.setForeground(Color.BLUE);
        adicionar.setMargin(new Insets(2, 10, 2, 14));
        adicionar.setFont(new Font("Linux libertine G", Font.BOLD, 14));
        add(adicionar);


    }

    public void botaoRemover(){
        Botao remover = new Botao();
        remover.setText("Remover Ator");
        remover.setForeground(Color.BLUE);
        remover.setFont(new Font("Linux libertine G", Font.BOLD, 14));
        remover.setLocation(200, 200);
        remover.setEnabled(true);
        add(remover);

    }

    public void botaoConsultar(){
        Botao consultar = new Botao();
        consultar.setText("Consultar elenco");
        consultar.setForeground(Color.BLUE);
        consultar.setFont(new Font("Linux libertine G", Font.BOLD, 14));
        add(consultar);
    }

    public void botaoPesquisar(){
        JButton pesquisar = new JButton("Pesquisar Ator");
        pesquisar.setForeground(Color.BLUE);
        pesquisar.setFont(new Font("Linux libertine G", Font.BOLD, 14));
        add(pesquisar);
    }

    @Override
    public void paint (Graphics g){
        super.paint(g);
        g.setColor(COR_FUNDO);
        g.fillRect(0, 0, largura, altura);

    }
    
asked by anonymous 02.10.2016 / 22:14

1 answer

2

To add actions to buttons (assuming they are JButtons by your code), simply add a ActionListener " as shown below:

jButton1.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
        //aqui você adiciona o código com o a ação
       //que será executada quando o botão for clicado
    }
});

For cases where the action snippet is a little more complex, you can create a method apart and just call it within # (passing ActionEvent as a parameter is optional for if you are to make use of some information of the component clicked):

jButton1.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
        //chama o método com a ação do botão
        ButtonHandler(evt);
    }
});

//...

private void ButtonHandler(java.awt.event.ActionEvent evt) {
   //ação do botão em um método desmembrado
}

References:

How to Write an Action Listener

Working with ActionListener and KeyListener in Java

    
02.10.2016 / 22:28