Add functions in graphical application buttons

Asked

Viewed 854 times

0

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

Follows 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);

    }
  • Amanda the answer below answered you?

1 answer

2

To add actions on buttons (assuming they are Jbuttons by their code), just add one ActionListener as an example 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 stretch of action is a little more complex, you can create a separate method and just call it inside the anonymous class(pass the ActionEvent as parameter is optional in case you are going to make use of some information of the clicked component):

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

  • It would be interesting to explain the reason for the downvote, so that you can correct what is supposedly not legal in the answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.