Na faculdade,o professor pediu para que nós implementasse uma calculadora usando o Swing,
então eu criei todos os botoes e paineis, fiz tb a função para qnd apertar o igual, o programa calcular
a string criada, sendo q quando eu subtraiu o n1(numero menor) do n2(numero maior), da um problema…
Ex: “4-5”, // quando for nesse if o programa da erro… e eu ja tentei de tudo
ele procura o sinal de menos, qnd acha ele pega tudo que ta antes do sinal, converte e armazena em n1
e tudo depois, converte e armazena no n2.
olhem o programa:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.security.PublicKey;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class CalcFrame extends JFrame{
private JTextField visor;
private JPanel panel1;
private JPanel panel2;
private JPanel panel3;
private JPanel panel4;
private JPanel panel5;
private JButton botao1;
private JButton botao2;
private JButton botao3;
private JButton botao4;
private JButton botao5;
private JButton botao6;
private JButton botao7;
private JButton botao8;
private JButton botao9;
private JButton botao0;
private JButton botaoMais;
private JButton botaoMenos;
private JButton botaoVez;
private JButton botaoDiv;
private JButton botaoIg;
private JButton botaoC;
private JButton botaoP;
private String s = "";
public CalcFrame() {
super("Calculadora");
setLayout(new FlowLayout());
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
panel4 = new JPanel();
panel5 = new JPanel();
panel1.setBackground(Color.GREEN);
panel2.setBackground(Color.BLUE);
panel3.setBackground(Color.BLUE);
panel4.setBackground(Color.BLUE);
panel5.setBackground(Color.BLUE);
add(panel1);
add(panel2);
add(panel3);
add(panel4);
add(panel5);
panel1.add(visor = new JTextField(30));
visor.setEditable(false);
panel2.add(botaoC = new JButton("C"));
panel3.add(botao1 = new JButton("1"));
panel3.add(botao2 = new JButton("2"));
panel3.add(botao3 = new JButton("3"));
panel3.add(botao4 = new JButton("4"));
panel3.add(botao5 = new JButton("5"));
panel3.add(botao6 = new JButton("6"));
panel4.add(botao7 = new JButton("7"));
panel4.add(botao8 = new JButton("8"));
panel4.add(botao9 = new JButton("9"));
panel4.add(botao0 = new JButton("0"));
panel5.add(botaoMais = new JButton("+"));
panel5.add(botaoMenos = new JButton("-"));
panel5.add(botaoVez = new JButton("x"));
panel5.add(botaoDiv = new JButton("/"));
panel5.add(botaoP = new JButton("."));
panel5.add(botaoIg = new JButton("="));
botao1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
s += "1";
visor.setText(s);
}
});
botao2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
s += "2";
visor.setText(s);
}
});
botao3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
s += "3";
visor.setText(s);
}
});
botao4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
s += "4";
visor.setText(s);
}
});
botao5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
s += "5";
visor.setText(s);
}
});
botao6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
s += "6";
visor.setText(s);
}
});
botao7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
s += "7";
visor.setText(s);
}
});
botao8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
s += "8";
visor.setText(s);
}
});
botao9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
s += "9";
visor.setText(s);
}
});
botao0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
s += "0";
visor.setText(s);
}
});
botaoMais.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
s += "+";
visor.setText(s);
}
});
botaoMenos.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
s += "-";
visor.setText(s);
}
});
botaoVez.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
s += "x";
visor.setText(s);
}
});
botaoDiv.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
s += "/";
visor.setText(s);
}
});
botaoC.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
s = "";
visor.setText(s);
}
});
botaoP.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
s += ".";
visor.setText(s);
}
});
botaoIg.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
for (int i = 0; i < s.length(); i++) {
if(s.charAt(i) == '+'){
double res = Double.parseDouble(s.substring(0, i));
res += Double.parseDouble(s.substring(i+1));
s += " = "+res;
}else if(s.charAt(i) == '-'){
double n1 = Double.parseDouble(s.substring(0, i));
double n2 = Double.parseDouble(s.substring(i+1));
if(n2 > n1){
s += " = -"+(n2-n1);
System.out.println(s);
}else{
s += " = "+(n1-n2);
}
}else if(s.charAt(i) == 'x'){
double res = Double.parseDouble(s.substring(0, i));
res *= Double.parseDouble(s.substring(i+1));
s += " = "+res;
}else if(s.charAt(i) == '/'){
double res = Double.parseDouble(s.substring(0, i));
res /= Double.parseDouble(s.substring(i+1));
s += " = "+res;
}
}
visor.setText(s);
}
});
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 250);
setVisible(true);
}
}
if(n2 > n1){
s += " = -"+(n2-n1);
System.out.println(s);
}else{
s += " = "+(n1-n2);
}
quando chega nessa parte da um erro: se eu tirar o sinal de menos da string o programa compila mas nao exibe o numero negativo: