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

    Unhappy Please!!! need help with this code

    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. Problems: background color and the main window


    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); 
        } 
    }

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

    Default

    Poster: there is already an active thread that you've started for this problem.

    Help with this code!!!

    Please don't create a new thread and risk folks duplicating labor that has already been done. Also, you would do well to provide more descriptive titles for your questions. We already know you need help, else you wouldn't post a question here. Thank you for your cooperation.

    This thread is closed.

Similar Threads

  1. Convert java code to midlet code
    By coldvoice05 in forum New To Java
    Replies: 1
    Last Post: 08-12-2009, 12:14 PM
  2. Convert java code to midlet code
    By coldvoice05 in forum Advanced Java
    Replies: 1
    Last Post: 08-09-2009, 02: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, 02: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, 11:12 PM
  5. Replies: 1
    Last Post: 04-26-2007, 04: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
  •