Ola pessoal,
Comecei a estudar Java na faculdade esse semestre (faço 2º semestre de SI), e não aprendi muita coisa ainda.
Tentei aprender em casa mais algumas coisas e até consegui entender um pouquinho.
Sei programar em PHP e em FLASH, mas java realmente estou começando.
Então resolvi tentar fazer uma calculadora em JAVA seguindo o seguinte link e código:
link: http://www.guj.com.br/posts/list/68783.java#361348
código:
[code]import java.awt.;
import java.awt.Event.;
import java.awt.event.ActionEvent;
import javax.swing.*;
public class Calc extends JFrame implements ActionListener, ListenerText //ActionListener e ListenerText estão apontando erro.
{
JLabel L1, L2, L3;
JButton B1, B2, B3, B4, B5;
JTextField T1, T2, T3;
public static void main(String []args)
{
JFrame Janela = new Calc();
Janela.show();
//
[b]WindowListener x = new WindowAdapter()[/b] //Essa linha esta com erro.
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
};
Janela.addWindowListener(x);
}
Calc()
{
setTitle("Calculadora");
setSize(390, 90);
setLocation(50, 50);
getContentPane().setBackground(new Color(150, 150, 150));
getContentPane().setLayout(new GridLayout(3, 4));
//LABELS
L1 = new JLabel("num.1");
L1.setForeground(Color.black);
L1.setFont(new Font("", Font.BOLD, 14));
L2 = new JLabel("num.2");
L2.setForeground(Color.black);
L2.setFont(new Font("", Font.BOLD, 14));
L3 = new JLabel("total");
L3.setFont(new Font("", Font.BOLD, 14));
//BOTÕES
B1 = new JButton("+"); /*FUNÇÂO ---> */ B1.addActionListener(this);//ERRO
B2 = new JButton("-"); B2.addActionListener(this);//ERRO
B3 = new JButton("*"); B3.addActionListener(this);//ERRO
B4 = new JButton("/"); B4.addActionListener(this);//ERRO
B5 = new JButton("Limpar"); B5.addActionListener(this);//ERRO
B5.setBackground(Color.black);
B5.setForeground(Color.white);
T1 = new JTextField();
T2 = new JTextField();
T3 = new JTextField();
T3.setEditable(false);
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)
{
T1.setText("");
T2.setText("");
T3.setText("");
return 0;//ERRO
}
float n1 = 0, n2 = 0, result = 0;
try
{
n1 = Float.parseFloat(T1.getText());
n2 = Float.parseFloat(T2.getText());
}
catch(NumberFormatException error)
{
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);
}
public void textValueChanged(TextEvent e) //TextEvent esta apontando erro
{
T3.setText("Ola!!");
}
}
[/code]
Coloquei um “//” nas linhas que apontaram o erro e não consegui solucionar.
Alguem poderia me indicar uma solução ou o que eu poderia estar fazendo para aprender melhor a linguagem Java?
Estou tentando aprender mas sempre surgem dúvidas, espero que o pessoal do forum possa me ajudar com as dúvidas.
Agradeço a todos
abraços