First of all congratulations on your initiative to the rest of the class and, not least, welcome to the Stack Overflow community!
Now let’s get down to business...
I realized that your problem is not in creating the buttons and the interface, but in adding a function to them, what we call events.
How events work out:
Every operating system that supports graphical interface monitors
events (clicks, mouse movements, keystrokes, etc.).
When an event happens, the operating system informs the
program.
The program decides what to do as an event according to what
was programmed.
Are the events that control all user interactions with the
program (clicks, typed text, mouse movements, etc.).
To work with events in Java from components
SWING we can use package features java.awt.event
.
Each component of the graphical interface (window, button, text field,
etc.) has listening interfaces (Listen) when an event is
generated.
Each interface has associated methods to treat the different
events.
Now that we know a little better how events work, let’s get to the practical part!
I wrote this example that is not very useful, but it’s great to understand how events work in practice (note the comments):
import javax.swing.*;
import java.awt.event.*;
public class MeuJFrame extends JFrame {
private JButton botao;
private JTextField campo;
public MeuJFrame() {
this.setTitle("Exemplo");
this.setBounds(0, 0, 200, 200);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.getContentPane().setLayout(null);
botao = new JButton();
botao.setText("botão");
botao.setBounds(40, 100, 100, 50);
this.add(botao);
campo = new JTextField();
campo.setBounds(40, 50, 100, 30);
this.add(campo);
// Adicionando um evento action ao botão
botao.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
// Aqui você escreve qual será a ação do botão ao ser clicado!
botao.setText(campo.getText());
}
});
} // construtor
public static void main(String[] args) {
MeuJFrame exemplo = new MeuJFrame();
exemplo.setVisible(true);
} // método main
} // classe
If you run the above example, you will notice that the action of the button I added captures the text inserted in JTextField
and assigns it to the button text.
A print of the execution:
Completion:
With the information cited above, you can enter any action at any button in your program.
I didn’t want to make your challenge easier by writing what a button would look like!
Any questions ask me in the comments, I hope to have helped.
EDIT:
As there are still some doubts, I decided to write a prototype of a calculator for you to have something to base:
import javax.swing.*;
import java.awt.event.*;
public class Calculadora extends JFrame {
private JButton numero1;
private JButton numero2;
private JButton somar;
private JButton igual;
private JTextField display;
private int leitura;
private int memoria;
private char operacao;
public Calculadora() {
this.setTitle("Exemplo Botão Somar");
this.setBounds(0, 0, 267, 235);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.getContentPane().setLayout(null);
leitura = 0;
memoria = 0;
somar = new JButton();
somar.setText("+");
somar.setBounds(145, 70, 78, 45);
this.add(somar);
igual = new JButton();
igual.setText("=");
igual.setBounds(25, 130, 200, 45);
this.add(igual);
numero1 = new JButton();
numero1.setText("1");
numero1.setBounds(25, 70, 45, 45);
this.add(numero1);
numero2 = new JButton();
numero2.setText("2");
numero2.setBounds(85, 70, 45, 45);
this.add(numero2);
display = new JTextField();
display.setBounds(25, 25, 200, 30);
this.add(display);
numero1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
leitura *= 10;
leitura += 1;
display.setText(display.getText() + "1");
}
});
numero2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
leitura *= 10;
leitura += 2;
display.setText(display.getText() + "2");
}
});
somar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
operacao = '+';
memoria += leitura;
leitura = 0;
display.setText("");
}
});
igual.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
switch (operacao) {
case '+': {
memoria += leitura;
break;
}
}
leitura = 0;
display.setText("" + memoria);
}
});
} // construtor
public static void main(String[] args) {
Calculadora exemplo = new Calculadora();
exemplo.setVisible(true);
}
} // classe
A print of the execution:
Note: It is not perfect, there are several purposeful bugs that you will have to solve to improve your programming logic... Use this code as a starting point!
I believe you are walking well, thank you very much man ! However, my only question now is: As in your example, the action of the button was to capture the text, how do I for the action of a button be sum, or subtraction for my calculator ? See, I’ve come this far: field = new Jtextfield(); field.setBounds(40,50,100,30); this.add(field); botaoMais.addActionListener(new Actionlistener(){ public void actionPerformed(Actionevent evt){ Now all I need to know is how to make the function sum or any other element of a calculator on each button !
– Gabriel Ozzy
Explaining here by the comments is complicated... I added another example to my answer, more like a calculator. Any doubt warns me!
– AndersonBS
I’ll try my best ! Thank you very much, friend ! You have no idea how much you helped !
– Gabriel Ozzy
That’s it! I bet you’ll get it!!
– AndersonBS