Boa tarde, pessoal.
Estou fazendo um exercício do livro Java Como Programar DEITEL que é construir uma calculadora com Swing. Eu já consegui fazer a interface gráfica da calculadora e gostaria que vocês avaliassem meu código, se possível. Além disso preciso de ajuda em como fazer para executar as ações dos botões.
Por exemplo: como eu faço para quando o usuário clicar no botão de um número que ele deseja junto com o botão de somar + e clicar em um segundo valor que ele deseja somar, o programa pegar o valor que está no LCD (TextField), somar e quando o usuário clicar no botão de igualdade = ele inserir o resultado no LCD (TextField)?
Vou simplificar caso tenha ficado muito confuso:
O usuário clicou no botão 2 em seguida no botão + e por fim no botão 3 e quando ele apertou o botão = apareceu o resultado 5 no LCD.
O código da calculadora:
package projeto.calculadora;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame{
public final JTextField txtfLCD;
public final JButton btPI, btLog, btDivision, btSquareRoot, btPercentage,
btParenthesesOpen, btParenthesesClose, btPlus, btMinus,
btEquals, btPoint, btCleaner, btTimes, bt00, bt01, bt02, bt03,
bt04, bt05, bt06, bt07, bt08, bt09;
public final JPanel panel1, panel2, panel3, panel4, panel5;
public final JMenuBar menuBar;
public final JMenu menuOption;
public final JMenuItem menuItemAbout, menuItemExit, menuItemNew;
Container container;
public Calculator(){
this.setTitle("Calculator");
this.setLayout(new FlowLayout(FlowLayout.LEFT));
this.setSize(308, 265);
this.setVisible(true);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
menuBar = new JMenuBar();
menuOption = new JMenu("Option");
menuItemAbout = new JMenuItem("About");
menuItemNew = new JMenuItem("New");
menuItemExit = new JMenuItem("Exit");
menuBar.add(menuOption);
menuOption.add(menuItemNew);
menuItemNew.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent ae){
txtfLCD.setText(" ");
}
});
menuOption.add(menuItemAbout);
menuOption.add(menuItemExit);
menuItemExit.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent ae){
System.exit(0);
}
});
this.setJMenuBar(menuBar);
panel1 = new JPanel();
txtfLCD = new JTextField(25);
txtfLCD.setEditable(false);
panel1.add(txtfLCD);
panel2 = new JPanel();
btPI = new JButton("π");
btPI.setToolTipText("calculate PI");
btDivision = new JButton("÷");
btSquareRoot = new JButton("√");
bt00 = new JButton("0");
bt00.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent ae){
String txt = txtfLCD.getText();
if(txt == null || txt.isEmpty()){
txtfLCD.setText("0");
}else{
txtfLCD.setText(txt + "0");
}
}
});
bt01 = new JButton("1");
bt01.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent ae){
String txt = txtfLCD.getText();
if(txt == null || txt.isEmpty()){
txtfLCD.setText("1");
}else{
txtfLCD.setText(txt + "1");
}
}
});
bt02 = new JButton("2");
bt02.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent ae){
String txt = txtfLCD.getText();
if(txt == null || txt.isEmpty()){
txtfLCD.setText("2");
}else{
txtfLCD.setText(txt + "2");
}
}
});
panel2.add(btPI);
panel2.add(btDivision);
panel2.add(btSquareRoot);
panel2.add(bt00);
panel2.add(bt01);
panel2.add(bt02);
panel3 = new JPanel();
btPlus = new JButton("+");
btPlus.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent ae){
String txt = txtfLCD.getText();
if(txt == null || txt.isEmpty()){
txtfLCD.setText(" ");
}else{
txtfLCD.setText(txt+" + ");
}
}
});
btPoint = new JButton("•");
btMinus = new JButton("−");
bt03 = new JButton("3");
bt03.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent ae){
String txt = txtfLCD.getText();
if(txt == null || txt.isEmpty()){
txtfLCD.setText("3");
}else{
txtfLCD.setText(txt + "3");
}
}
});
bt04 = new JButton("4");
bt04.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent ae){
String txt = txtfLCD.getText();
if(txt == null || txt.isEmpty()){
txtfLCD.setText("4");
}else{
txtfLCD.setText(txt + "4");
}
}
});
bt05 = new JButton("5");
bt05.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent ae){
String txt = txtfLCD.getText();
if(txt == null || txt.isEmpty()){
txtfLCD.setText("5");
}else{
txtfLCD.setText(txt + "5");
}
}
});
panel3.add(btPlus);
panel3.add(btPoint);
panel3.add(btMinus);
panel3.add(bt03);
panel3.add(bt04);
panel3.add(bt05);
panel4 = new JPanel();
btTimes = new JButton("x");
btEquals = new JButton("= ");
btEquals.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent ae){
}
});
btPercentage = new JButton("%");
bt06 = new JButton("6");
bt06.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent ae){
String txt = txtfLCD.getText();
if(txt == null || txt.isEmpty()){
txtfLCD.setText("6");
}else{
txtfLCD.setText(txt + "6");
}
}
});
bt07 = new JButton("7");
bt07.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent ae){
String txt = txtfLCD.getText();
if(txt == null || txt.isEmpty()){
txtfLCD.setText("7");
}else{
txtfLCD.setText(txt + "7");
}
}
});
bt08 = new JButton("8");
bt08.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent ae){
String txt = txtfLCD.getText();
if(txt == null || txt.isEmpty()){
txtfLCD.setText("8");
}else{
txtfLCD.setText(txt + "8");
}
}
});
panel4.add(btTimes);
panel4.add(btEquals);
panel4.add(btPercentage);
panel4.add(bt06);
panel4.add(bt07);
panel4.add(bt08);
panel5 = new JPanel();
btLog = new JButton("log");
btParenthesesOpen = new JButton("(");
btParenthesesOpen.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent ae){
String txt = txtfLCD.getText();
if(txt == null || txt.isEmpty()){
txtfLCD.setText(" ");
}else{
txtfLCD.setText(txt+" ( ");
}
}
});
btParenthesesClose = new JButton(")");
btParenthesesClose.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent ae){
String txt = txtfLCD.getText();
if(txt == null || txt.isEmpty()){
txtfLCD.setText(" ");
}else{
txtfLCD.setText(txt+" ) ");
}
}
});
bt09 = new JButton("9");
bt09.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent ae){
String txt = txtfLCD.getText();
if(txt == null || txt.isEmpty()){
txtfLCD.setText("9");
}else{
txtfLCD.setText(txt + "9");
}
}
});
btCleaner = new JButton("CLEANER");
btCleaner.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent ae){
txtfLCD.setText(" ");
}
});
btCleaner.setToolTipText("Cleaner button");
panel5.add(btLog);
panel5.add(btParenthesesOpen);
panel5.add(btParenthesesClose);
panel5.add(bt09);
panel5.add(btCleaner);
container = this.getContentPane();
container.add(panel1, BorderLayout.NORTH);
container.add(panel2, BorderLayout.WEST);
container.add(panel3, BorderLayout.CENTER);
container.add(panel4, BorderLayout.EAST);
container.add(panel5, BorderLayout.EAST);
} }
Desde já, muito obrigado