Erro no meu código EXPECTED!

5 respostas
J
Queria que alguám me ajudasse nisso.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Calc extends JFrame implements ActionListener
{
  JButton b1,b2,b3,b4,b5;
  ImageIcon icone = new ImageIcon("C:/Documents and Settings/Jhonatas/Desktop/icon_up.gif");

   public Calc()
   {
   setTitle("Calculadora");
   setSize(350,100);
   setLocation(50,50);
   getContentPane().setBackground( new Color(180,180,180));

   b1 = new JButton("ADIÇÃO",icone);
   b1.setHorizontalTextPosition(AbstractButton.LEFT);
   b1.setBackground( new Color(100,180,180));
   b1.setForeground(Color.black);
   b1.setFont( new Font("ScriptS", Font.BOLD,20));
   b1.setEnabled(true);
   b1.addActionListener(this);
   b1.setToolTipText("Pressione para realizar uma adição");
   b1.setMnemonic(KeyEvent.VK_A);

   b2 = new JButton("SUBTRAÇÃO");
   b2.setBackground( new Color(100,180,180));
   b2.setForeground(Color.black);
   b2.setFont( new Font("ScriptS", Font.BOLD,20));
   b2.setEnabled(true);
   b2.addActionListener(this);
   b2.setToolTipText("Pressione para realizar uma subtração");
   b2.setMnemonic(KeyEvent.VK_S);

   b3 = new JButton("DIVISÃO");
   b3.setBackground( new Color(100,180,180));
   b3.setForeground(Color.black);
   b3.setFont( new Font("ScriptS", Font.BOLD,20));
   b3.setEnabled(true);
   b3.addActionListener(this);
   b3.setToolTipText("Pressione para realizar uma divisão");
   b3.setMnemonic(KeyEvent.VK_D);

   b4 = new JButton("MULTIPLICAÇÃO");
   b4.setBackground( new Color(100,180,180));
   b4.setForeground(Color.black);
   b4.setFont( new Font("ScriptS", Font.BOLD,20));
   b4.setEnabled(true);
   b4.addActionListener(this);
   b4.setToolTipText("Pressione para realizar uma multiplicação");
   b4.setMnemonic(KeyEvent.VK_M);

   b5 = new JButton("Cancelar");
   b5.addActionListener(this);
   b5.setMnemonic(KeyEvent.VK_C);
   b5.setToolTipText("Pressione para cancelar");
   getContentPane().setLayout( new FlowLayout());
   getContentPane().add(b1);
   getContentPane().add(b2);
   getContentPane().add(b3);
   getContentPane().add(b4);
   getContentPane().add(b5);
   }

   public void actionPerformed( ActionEvent e)
   {
     if (e.getSource()==b1)
        eventoButton1();

     if (e.getSource()==b2)
        eventoButton2();

     if (e.getSource()==b3)
        eventoButton3();

     if (e.getSource()==b4)
        eventoButton4();

     if (e.getSource()==b5)
        eventoButton5();
   }


 public static void main(String args[]) { 
   JFrame Janela = new Exemplo0903(); 
   Janela.show(); 
   Janela.addWindowListener( new WindowAdapter() { 
      public void windowClosing(WindowEvent e) { 
         System.exit(0); 
      }
   });    
} 


private void eventoButton1() {

   int num,num2,res;
   String n1,n2;

   n1 = JOptionPane.showInputDialog("Entre com primeiro número");

   n2 = JOptionPane.showInputDialog("Entre com segundo número");

   try{
      num = Integer.parseInt(n1);
      num2 = Integer.parseInt(n2);
   }
   catch (NumberFormatException e){
      JOptionPane.showMessageDialog(
         null, "ERROR","Inválido",
         JOptionPane.WARNING_MESSAGE);
   }
   res = num + num2;

   JOptionPane.showMessageDialog(
      null, "A soma é " + res,
      "Soma",
      JOptionPane.INFORMATION_MESSAGE);

} //   Aqui ta dando erro   expected, alguém pode me ajudar nisso?
Esse código é de uma suposta e mal feita calculadora, no qual cada botao chama uma determinda função!

5 Respostas

F

Cara tinha uma série de erros no seu código…fiz ele rodar , mas ainda sim não apareceu nada…reveja a parte do metodo main
1-> vc não fechou as aspas {} fico faltando 1
2-> vc não implementou os metodos
eventoButton2();
3,4,5
3->Vc declarou variaveis dentro do try onde não pode ser enchergado dentro do metodo

Agora copila, mas reveja porque n/ao aparec3e os componentes…Beleza??? Qualquer coisa fale

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Calc extends JFrame implements ActionListener{


  JButton b1,b2,b3,b4,b5;
  
  ImageIcon icone = new ImageIcon("C:/Documents and Settings/Jhonatas/Desktop/icon_up.gif");

   public Calc()
   {
   setTitle("Calculadora");
   setSize(350,100);
   setLocation(50,50);
   getContentPane().setBackground( new Color(180,180,180));

   b1 = new JButton("ADIÇÃO",icone);
   b1.setHorizontalTextPosition(AbstractButton.LEFT);
   b1.setBackground( new Color(100,180,180));
   b1.setForeground(Color.black);
   b1.setFont( new Font("ScriptS", Font.BOLD,20));
   b1.setEnabled(true);
   b1.addActionListener(this);
   b1.setToolTipText("Pressione para realizar uma adição");
   b1.setMnemonic(KeyEvent.VK_A);

   b2 = new JButton("SUBTRAÇÃO");
   b2.setBackground( new Color(100,180,180));
   b2.setForeground(Color.black);
   b2.setFont( new Font("ScriptS", Font.BOLD,20));
   b2.setEnabled(true);
   b2.addActionListener(this);
   b2.setToolTipText("Pressione para realizar uma subtração");
   b2.setMnemonic(KeyEvent.VK_S);

   b3 = new JButton("DIVISÃO");
   b3.setBackground( new Color(100,180,180));
   b3.setForeground(Color.black);
   b3.setFont( new Font("ScriptS", Font.BOLD,20));
   b3.setEnabled(true);
   b3.addActionListener(this);
   b3.setToolTipText("Pressione para realizar uma divisão");
   b3.setMnemonic(KeyEvent.VK_D);

   b4 = new JButton("MULTIPLICAÇÃO");
   b4.setBackground( new Color(100,180,180));
   b4.setForeground(Color.black);
   b4.setFont( new Font("ScriptS", Font.BOLD,20));
   b4.setEnabled(true);
   b4.addActionListener(this);
   b4.setToolTipText("Pressione para realizar uma multiplicação");
   b4.setMnemonic(KeyEvent.VK_M);

   b5 = new JButton("Cancelar");
   b5.addActionListener(this);
   b5.setMnemonic(KeyEvent.VK_C);
   b5.setToolTipText("Pressione para cancelar");
   getContentPane().setLayout( new FlowLayout());
   getContentPane().add(b1);
   getContentPane().add(b2);
   getContentPane().add(b3);
   getContentPane().add(b4);
   getContentPane().add(b5);
   }

   public void actionPerformed( ActionEvent e)
   {
     if (e.getSource()==b1){
     	eventoButton1();
     }
        

     if (e.getSource()==b2){
     	 eventoButton1();
     }
       

     if (e.getSource()==b3){
     	 eventoButton1();
     }
       

     if (e.getSource()==b4){
     	eventoButton1();
     }
        

     if (e.getSource()==b5){
     	 eventoButton1();
     }
       
   }


 public static void main(String args[]) {
   JFrame Janela = new JFrame();
   Janela.show();
   Janela.addWindowListener( new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
         System.exit(0);
      }
   });   
}


private void eventoButton1() {

   int num,num2,res;
   String n1,n2;

   n1 = JOptionPane.showInputDialog("Entre com primeiro número");

   n2 = JOptionPane.showInputDialog("Entre com segundo número");
   num = Integer.parseInt(n1);
      num2 = Integer.parseInt(n2);

   try{
      
   }
   catch (NumberFormatException e){
      JOptionPane.showMessageDialog(
         null, "ERROR","Inválido",
         JOptionPane.WARNING_MESSAGE);
   }
   res = num + num2;

   JOptionPane.showMessageDialog(
      null, "A soma é " + res,
      "Soma",
      JOptionPane.INFORMATION_MESSAGE);

}
} 
//   Aqui ta dando erro   expected, alguém pode me ajudar nisso?
L

O metodo eventoButton1() esta fora da classe Calc, vc tem q colocar ele dentro dela junto com os outros metodos q vc chama como eventoButton2() eventoButton3() eventoButton4() e eventoButton5() :razz: :razz: :razz:

J

Daria pra botar um exemplo ou fazer alguma mudança no meu código pra min entender como se faz isso? Valeu desde já!!![color=“darkred”][/color][/quote]

S
ImageIcon icone = new ImageIcon("C:/Documents and Settings/Jhonatas/Desktop/icon_up.gif");

Outro erro.

Troque cada “/” por “\”.

S

public static void main(String args[]) { JFrame Janela = new JFrame(); Janela.show(); Janela.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); }

Troque “JFrame” por “Calc”. Deve ser por isso que não aparece nada.

Criado 26 de maio de 2005
Ultima resposta 27 de mai. de 2005
Respostas 5
Participantes 4