Bem pessoal,
sou iniciante em Java… estou montando uma calculadora básica,
to me dedicando mais ao swing,
montei a seguinte classe:
[code]import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JTextField;
public class Exemplo0904 extends JFrame implements ActionListener {
JLabel L1, L2, L3;
JButton B1, B2, B3, B4, B5;
JTextField T1,T2,T3;
public static void main(String args[])
{
JFrame Janela = new Exemplo0904();
Janela.show();
//rotina para fechar a janela:
WindowListener x = new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
};
Janela.addWindowListener(x);
}
Exemplo0904()
{
setTitle("Calculadora"); //Texto no título do formulário
setSize(290,290); //dimensoes da janela
setLocation(50,50); //canto esquerdo e topo da janela
getContentPane().setBackground(new Color(150,150,150)); //Definição da cor de fundo
getContentPane().setLayout(new GridLayout(3,4)); //definição do layout
//Declarações de componetes, inicialização...
L1 = new JLabel("Num.1");
L1.setForeground(Color.black);
L1.setFont(new Font("",Font.BOLD,12));
L2 = new JLabel("Num.2");
L2.setForeground(Color.black);
L2.setFont(new Font("", Font.BOLD, 12));
L3 = new JLabel("Total");
L3.setFont(new Font("", Font.BOLD, 12));
B1 = new JButton ("+");
B1.addActionListener(this);
B2 = new JButton ("-");
B2.addActionListener(this);
B3 = new JButton ("x");
B3.addActionListener(this);
B4 = new JButton ("/");
B4.addActionListener(this);
B5 = new JButton ("Limpar");
B5.addActionListener(this);
B5.setBackground(Color.black);
B5.setForeground(Color.white);
T1 = new JTextField();
T2 = new JTextField();
T3 = new JTextField();
T3.setEditable(false); //somente leitura, não é possivel inserir dados
getContentPane().add(L1);
getContentPane().add(T1);
getContentPane().add(B1);
getContentPane().add(B2);
getContentPane().add(L2);
getContentPane().add(T2);
getContentPane().add(B3);
getContentPane().add(B4);
getContentPane().add(L3);
getContentPane().add(T3);
getContentPane().add(B5);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==B5) // evento clique no B5 (botão limpar) atribui valores ""
{ // nas textfields
T1.setText(""); T2.setText(""); T3.setText("");
return;
}
float N1=0,N2=0,result=0;
/* try
{N1 = Float.parseFloat(T1.getText());
N2 = Float.parseFloat(T2.getText());
}
catch (NumberFormatException erro)
{
T3.setText("Erro");
return;
}*/
if (e.getSource()==B1)
result = N1 + N2;
if (e.getSource()==B2)
result = N1 - N2;
if (e.getSource()==B3)
result = N1 * N2;
if (e.getSource()==B4)
result = N1 / N2;
T3.setText ("" + result);
}
}[/code]
porém onde tenho comentado o try/catch, não tinha colocado no meu código, então qualquer operação feita o resultado retornava O.
Vi em alguns tópicos o try/catch e resolvi acrescenta no código, deu certo…
mas não entendi o que significa o Float.parseFloat,
alguém dá uma luz ai??
Vlw