Criei uma calculadora apenas com os botões 1 e 2 e que apenas soma para facilitar o estudo…
Agora quero adicionar um Menu, a princípio apenas com a operação SAIR, mas não está com erro mas não está rodando corretamente…
Se puderem me ajudar… Obrigado…
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
class CalcSOMA extends Frame {
TextField txtResult;
Button btnIgual, btnClear, btn1, btn2, btnSom;
float a = 0, b = 0;
char op;
boolean ativado = false, ativado2 = false;
CalcSOMA() {
JFrame f = new JFrame();
Container quadro = f.getContentPane();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.setBackground(Color.orange);
f.setLocation(450, 250);
f.setSize(310, 320);
f.setLayout(null);
f.setTitle(".:calculadora:.");
JMenuBar barra = new JMenuBar();
f.setJMenuBar(barra);
JMenu guia1 = new JMenu("Arquivo");
barra.add(guia1);
JMenuItem item1 = new JMenuItem("Sair");
guia1.add(item1);
item1.addActionListener(new Ouve0GUI(f));
txtResult = new TextField("0");
txtResult.setBounds(60, 60, 118, 25);
add(txtResult);
btn1 = new Button("1");
btn1.setBounds(60, 150, 35, 25);
add(btn1);
btn2 = new Button("2");
btn2.setBounds(100, 150, 35, 25);
add(btn2);
btnIgual = new Button("=");
btnIgual.setBounds(60, 180, 35, 25);
add(btnIgual);
btnSom = new Button("+");
btnSom.setBounds(10, 90, 35, 25);
add(btnSom);
btnClear = new Button("C");
btnClear.setBounds(10, 60, 35, 25);
add(btnClear);
setVisible(true);
btnClear.addActionListener(new Limpar());
btn1.addActionListener(new Um());
btn2.addActionListener(new Dois());
btnSom.addActionListener(new Somar());
btnIgual.addActionListener(new Resultado());
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
class Ouve0GUI implements ActionListener {
Component parentComponent;
Ouve0GUI(Component parentComponent) {
this.parentComponent = parentComponent;
}
public void actionPerformed(ActionEvent e) {
int resp = JOptionPane.showConfirmDialog(parentComponent,
"Sair do Programa");
if (resp == JOptionPane.OK_OPTION)
System.exit(0);
}
}//
class Somar implements ActionListener {
public void actionPerformed(ActionEvent e) {
op = '+';
a = Float.parseFloat(txtResult.getText());
ativado = true;
}
}
class Resultado implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (ativado2 == false) {
b = Float.parseFloat(txtResult.getText());
}
switch (op) {
case '+':
a = a + b;
txtResult.setText(String.valueOf(a));
ativado = true;
ativado2 = true;
break;
}
}
}
class Limpar implements ActionListener {
public void actionPerformed(ActionEvent e) {
txtResult.setText("0");
ativado = false;
}
}
class Um implements ActionListener {
public void actionPerformed(ActionEvent e) {
ativado2 = false;
if ("0".equals(txtResult.getText()) || (ativado == true)) {
txtResult.setText("1");
ativado = false;
} else
txtResult.setText(txtResult.getText() + "1");
}
}
class Dois implements ActionListener {
public void actionPerformed(ActionEvent e) {
ativado2 = false;
if ("0".equals(txtResult.getText()) || (ativado == true)) {
txtResult.setText("2");
ativado = false;
} else
txtResult.setText(txtResult.getText() + "2");
}
}
public static void main(String[] arg) {
CalcSOMA obj = new CalcSOMA();
}
}