Results 1 to 9 of 9
  1. #1
    ximenaacucr is offline Member
    Join Date
    Sep 2009
    Posts
    6
    Rep Power
    0

    Unhappy Help with this code!!!

    I really need help with this java code because when I try to compile, it sends me 9 errors, please I need someone who can improve this code. Thx

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

    public class Calculadora4 extends JFrame implements ActionListener {

    private JTextField Valor1;
    private JTextField Valor2;
    private JTextField Resultado;
    private JButton boton1;
    private JButton boton2;
    private JButton boton3;
    private JButton boton4;
    private JButton boton5;
    private JTextArea resultado;

    public Calculadora4 (String titulo){
    super(titulo);

    this.setSize(700,600);
    this.setTitle("Calculadora");
    this.setVisible(true);
    this.setBackground(Color.pink);

    JLabel instrucciones = new JLabel("Digite los numeros en los espacios grises y luego pulse el boton de la operacion que desea realizar");
    instrucciones.setBounds(20,5,700,50);

    JLabel valor1 = new JLabel("Valor 1");
    valor1.setBounds(20,60,100,50);
    JLabel valor2 = new JLabel("Valor 2");
    valor2.setBounds(20,130,100,50);
    JLabel valor3 = new JLabel("Resultado");
    valor3.setBounds(20,215,100,50);

    Valor1 = new JTextField(20);
    Valor1.setBounds(100,80,200,30);
    Valor1.setBackground(Color.lightGray);
    Valor2 = new JTextField(20);
    Valor2.setBounds(100,150,200,30);
    Valor2.setBackground(Color.lightGray);
    Resultado = new JTextField(40);
    Resultado.setBounds(100,235,200,80);


    boton1 = new JButton("+");
    boton1.setBounds(100,450,100,30);
    boton1.addActionListener(this);

    boton2 = new JButton("-");
    boton2.setBounds(200,450,100,30);
    boton2.addActionListener(this);

    boton3 = new JButton("/");
    boton3.setBounds(300,450,100,30);
    boton3.addActionListener(this);

    boton4 = new JButton("*");
    boton4.setBounds(400,450,100,30);
    boton4.addActionListener(this);

    boton5 = new JButton("Salir");
    boton5.setBounds(500,450,100,30);
    boton5.addActionListener(this);

    this.add(instrucciones);
    this.add(valor1);
    this.add(Valor1);
    this.add(valor2);
    this.add(Valor2);

    this.add(valor3);

    this.add(boton1);
    this.add(boton2);
    this.add(boton3);
    this.add(boton4);
    this.add(boton5);

    JTextArea resultado = new JTextArea(10,20);
    JScrollPane resultado2 = new JScrollPane();

    resultado2.setBounds(100,235,350,80);
    resultado2.setViewportView(resultado);

    this.add(resultado2);
    }

    public void actionPerformed(ActionEvent arg0) {
    double v1, v2;
    v1= Double.parseDouble(Valor1.getText());
    v2= Double.parseDouble(Valor2.getText());

    public double sumar(){
    if (boton1 != null){
    resultado.setText("");
    resultado.append("El resultado es: " + (v1 + v2));
    }
    }

    public double resta(){
    if (boton1 != null){
    resultado.setText("");
    resultado.append("El resultado es: " + (v1 - v2));
    }
    }

    public double producto(){
    if (boton1 != null){
    resultado.setText("");
    resultado.append("El resultado es: " + (v1 * v2));
    }
    }

    public double division(){
    if (boton1 != null){
    resultado.setText("");
    resultado.append("El resultado es: " + (v1 / v2));
    }
    }
    }
    }

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Well, first off, post code using [code] [/code] tags. Your code is almost impossible to read because of the lack of indentation. Secondly, what is this all about?

    Java Code:
    public void actionPerformed(ActionEvent arg0) {
            double v1, v2;
            v1 = Double.parseDouble(Valor1.getText());
            v2 = Double.parseDouble(Valor2.getText());
    
        public double sumar() {
            if (boton1 != null) {
                resultado.setText("");
                resultado.append("El resultado es: " + (v1 + v2));
            }
        }
    
        public double resta() {
            if (boton1 != null) {
                resultado.setText("");
                resultado.append("El resultado es: " + (v1 - v2));
            }
        }
    ....
    You cannot declare a method inside a method, and even if you could, none of the inner methods have return statements. Have you read anything on the java tutorial pages at sun.com about code structure?
    Last edited by quad64bit; 09-09-2009 at 04:37 PM.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default

    On top of the formatting thing, "it sends 9 errors" isn't much help. You shouldn't expect us to have to compile the code to find out what those errors are. You should post them in full, that way we can probably simply point out the problems without having to cut and paste your code into our IDEs.

  4. #4
    ximenaacucr is offline Member
    Join Date
    Sep 2009
    Posts
    6
    Rep Power
    0

    Unhappy Help with this code!!! please!!!

    This is my new code but I still have problems to compile it. Im really new at this java stuff; please if someone can help me with this code, I will really appreciate it.


    Java Code:
     import java.awt.*; 
    import java.awt.event.*; 
    import javax.swing.*; 
    
    public class Calculadora4 extends JFrame implements ActionListener { 
    
        private JTextField Valor1; 
        private JTextField Valor2; 
        private JTextField Resultado; 
        private JButton boton1; 
        private JButton boton2; 
        private JButton boton3; 
        private JButton boton4; 
        private JButton boton5; 
        private JTextArea resultado; 
    
        public Calculadora4 (String titulo){ 
            super(titulo); 
    
            this.setSize(700,600); 
            this.setTitle("Calculadora"); 
            this.setVisible(true); 
            this.setBackground(Color.pink); 
    
            JLabel instrucciones = new JLabel("Digite los numeros en los espacios grises y luego pulse el boton de la operacion que desea realizar"); 
            instrucciones.setBounds(20,5,700,50); 
    
            JLabel valor1 = new JLabel("Valor 1"); 
            valor1.setBounds(20,60,100,50); 
            JLabel valor2 = new JLabel("Valor 2"); 
            valor2.setBounds(20,130,100,50); 
            JLabel valor3 = new JLabel("Resultado"); 
            valor3.setBounds(20,215,100,50); 
    
            Valor1 = new JTextField(20); 
            Valor1.setBounds(100,80,200,30); 
            Valor1.setBackground(Color.lightGray); 
            Valor2 = new JTextField(20); 
            Valor2.setBounds(100,150,200,30); 
            Valor2.setBackground(Color.lightGray); 
            Resultado = new JTextField(40); 
            Resultado.setBounds(100,235,200,80); 
    
    
            boton1 = new JButton("+"); 
            boton1.setBounds(100,450,100,30); 
            boton1.addActionListener(this); 
    
            boton2 = new JButton("-"); 
            boton2.setBounds(200,450,100,30); 
            boton2.addActionListener(this); 
    
            boton3 = new JButton("/"); 
            boton3.setBounds(300,450,100,30); 
            boton3.addActionListener(this); 
    
            boton4 = new JButton("*"); 
            boton4.setBounds(400,450,100,30); 
            boton4.addActionListener(this); 
    
            boton5 = new JButton("Salir"); 
            boton5.setBounds(500,450,60,30); 
            boton5.addActionListener(this); 
    
            this.add(instrucciones); 
            this.add(valor1); 
            this.add(Valor1); 
            this.add(valor2); 
            this.add(Valor2); 
    
            this.add(valor3); 
    
            this.add(boton1); 
            this.add(boton2); 
            this.add(boton3); 
            this.add(boton4); 
            this.add(boton5); 
    
            resultado = new JTextArea(10,20); 
            JScrollPane resultado2 = new JScrollPane(); 
    
            resultado2.setBounds(100,235,350,80); 
            resultado2.setViewportView(resultado); 
    
            this.add(resultado2); 
        } 
    
        public void actionPerformed(ActionEvent e) { 
            double v1 = 0; 
            double v2 = 0; 
            try{ 
                v1 = Double.parseDouble(Valor1.getText()); 
            }catch(NumberFormatException x){ 
                v1 = 0; 
            } 
            try{ 
                v2 = Double.parseDouble(Valor2.getText()); 
            }catch(NumberFormatException x){ 
                v2 = 0; 
            } 
            if(e.getSource() == boton1) 
                sumar(v1, v2); 
            else if(e.getSource() == boton2) 
                resta(v1, v2); 
            else if(e.getSource() == boton3) 
                division(v1, v2); 
            else if(e.getSource() == boton4) 
                producto(v1, v2); 
            else{ 
                javax.swing.JOptionPane.showMessageDialog(this, "Adios", "Calculadora", javax.swing.JOptionPane.INFORMATION_MESSAGE); 
                System.exit(0); 
            } 
        } 
    
        public void sumar(double v1, double v2){ 
            if (boton1 != null){ 
                resultado.setText(""); 
                resultado.append("El resultado es: " + (v1 + v2)); 
            } 
        } 
    
        public void resta(double v1, double v2){ 
            if (boton2 != null){ 
                resultado.setText(""); 
                resultado.append("El resultado es: " + (v1 - v2)); 
            } 
        } 
    
        public void producto(double v1, double v2){ 
            if (boton3 != null){ 
                resultado.setText(""); 
                resultado.append("El resultado es: " + (v1 * v2)); 
            } 
        } 
    
        public void division(double v1, double v2){ 
            if (boton4 != null){ 
                resultado.setText(""); 
                resultado.append("El resultado es: " + (v1 / v2)); 
            } 
        } 
        public static void main(String args[]){ 
            Calculadora4 ventana = new Calculadora4("Calculadora"); 
            ventana.setDefaultCloseOperation(ventana.EXIT_ON_CLOSE); 
            ventana.setVisible(true); 
        } 
    }

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    -- op, please don't cross-post the same question in multiple locations. I've closed your cross-post thread.

    but I still have problems to compile it
    You still have not told us any specifics here. What errors are you seeing? Which lines are causing the errors? The error messages will tell you (and us) what's wrong. Without your showing us the error messages and indicating the line numbers all we can do is tell you that we hope you get it fixed.
    Last edited by Fubarable; 09-09-2009 at 07:11 PM.

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    And again you fail to provide the error messages that you are getting? Classic.
    I would suggest shelving that code and going back to reading Sun's tutorial to understand the basics first. There is no point in trying to write Swing programs before you lean the structure of a Java method.

  7. #7
    ximenaacucr is offline Member
    Join Date
    Sep 2009
    Posts
    6
    Rep Power
    0

    Unhappy Problems

    When I compile the new code It doesnt show any errors at all, the real problem is when I try to run it, the window doesnt open completely (it is like the program, or some parts of it are not responding), and also the window doesnt show the background color that should be pink.

    Thx

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    1) Don't set the color of the JFrame, but rather set the color of the JFrame's contentPane. i.e.,
    Java Code:
          //this.setBackground(Color.pink);  // not this
          getContentPane().setBackground(Color.pink);  // but this!
    2) Don't call setVisible(true) on the JFrame before you've added all your components to the JFrame. Otherwise you won't see all the components.

    3) If you're going to use a null layout (also known as absolute positioning), then declare it with:
    Java Code:
    getContentPane().setLayout(null);
    Personally, I recommend avoiding null layouts but instead using one or several of the layout managers.

  9. #9
    ximenaacucr is offline Member
    Join Date
    Sep 2009
    Posts
    6
    Rep Power
    0

    Thumbs up Thank you very much!!!!!!

    You re the best!!!! thank you very much, you really help me!!! thx :)

Similar Threads

  1. Convert java code to midlet code
    By coldvoice05 in forum New To Java
    Replies: 1
    Last Post: 08-12-2009, 11:14 AM
  2. Convert java code to midlet code
    By coldvoice05 in forum Advanced Java
    Replies: 1
    Last Post: 08-09-2009, 01:21 PM
  3. Why doesn't this code accept my code?
    By PeterFeng in forum New To Java
    Replies: 5
    Last Post: 02-03-2009, 01:39 PM
  4. I need help fixing my code.. or non code?
    By MrHuggykins in forum New To Java
    Replies: 1
    Last Post: 03-19-2008, 10:12 PM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •