Results 1 to 2 of 2
  1. #1
    ergne is offline Member
    Join Date
    Mar 2013
    Posts
    1
    Rep Power
    0

    Default Help with a current convers euro-pts (and inverse)

    [COLOR="#0000CD"]Hi, I am new in Java, and I want to make a current conversion euros-pts, and its inverse, using 2 buttons, and a background image;

    This is what I am looking for:
    ImageShack® - Online Photo and Video Hosting
    This means, clicking Button1 (convertir), convert from Euros to ptas. And by clicking on the second (invertir Conversion), must give us the inverse conversion by clicking again in button1.
    The main problem is in the loop, I can make the first change, but I don´t how to close the loop If for the second change.
    How can I do?
    Thanks a lot!

    This is the code :
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.math.*;
    
    public class Cuadros extends JFrame implements ActionListener {
    
    
    	public  JLabel cantidad, result;
    	public JTextField cantMoneda, campoTotal;
    	public JButton boton1, boton2;
    	private final double euro=166.386;
    	private Image img1, img2;
    
    
      public static void main(String[] args) {
        Cuadros e = new Cuadros();
        e.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        e.setSize(310, 200);
        e.setVisible(true);
      }
      public Cuadros() {
        Container pane = getContentPane();
        pane.setLayout(new BorderLayout());
    
        pane.add(getButtonPanel(), BorderLayout.SOUTH);
      }
      
    
      protected JComponent getButtonPanel() {
        JPanel panel = new JPanel();
        panel.setFont(new Font("Arial", Font.BOLD, 18));
        panel.setLayout(new GridLayout(3, 2, 0, 0));
        
        cantidad=new JLabel("Cantidad a Convertir");
        cantidad.setOpaque(true);
        cantMoneda=new JTextField("");
       	panel.add(cantidad);
       	panel.add(cantMoneda);
       	cantidad.setBackground((Color.orange));
       	cantMoneda.setBackground(Color.orange);
        boton1= new JButton("Convertir");
        panel.add(boton1);
        boton1.addActionListener(this);
       	boton1.setBackground(Color.LIGHT_GRAY);
       	boton2= new JButton("Invertir Conversión");
        panel.add(boton2);
        boton2.addActionListener(this);
       	boton2.setBackground(Color.LIGHT_GRAY);
        result=new JLabel("Resultado");
        result.setOpaque(true);
       	campoTotal=new JTextField("", 18);
       	campoTotal.setEditable(false);
       	campoTotal.setBackground(Color.PINK);
       	result.setBackground(Color.PINK);
        panel.add(result);
        panel.add(campoTotal);
        
        return panel;
      }
    
    	 public void actionPerformed(ActionEvent e){
        	Object source=e.getSource();
        	while (e.getSource()==boton2){
        		invertirConversion();
        		if (e.getSource()==boton1)
          		convertiraEuros();
        		break;
        		}		
        	if (e.getSource()==boton1)	
               	convertiraPesetas();
           	}   
           
    
      public void convertiraPesetas(){
           	String cad1=cantMoneda.getText();
        	double x1=Integer.parseInt(cad1);
       		double x2=x1*euro;
        	String total=String.valueOf(x2);
        	BigDecimal numero1 = new BigDecimal(total);
        	campoTotal.setText(numero1.setScale(3, RoundingMode.HALF_UP)+" ptas.");
        	
         }
         public void invertirConversion(){
         	cantMoneda.setText("");
         	campoTotal.setText("");
         	
         }
              
         	public void convertiraEuros(){
           		String cad1=cantMoneda.getText();
         		double x1=Integer.parseInt(cad1);
       			double x2=x1/euro;
        		String total=String.valueOf(x2);
        		BigDecimal numero2 = new BigDecimal(total);
        		campoTotal.setText(numero2.setScale(2, RoundingMode.HALF_UP)+" euros.");
           	}
       		 public void paint(Graphics g) {
        	Graphics2D g2 = (Graphics2D) g;
    
        	img1 = Toolkit.getDefaultToolkit().getImage("EurAPst.jpg");
        	g2.drawImage(img1, 0, 0, this);
        	g2.finalize();
      }
    }
    Last edited by JosAH; 03-15-2013 at 12:52 PM. Reason: added [code] ... [/code] tags

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,730
    Blog Entries
    7
    Rep Power
    21

    Default Re: Help with a current convers euro-pts (and inverse)

    Quote Originally Posted by ergne View Post
    [COLOR="#0000CD"]This means, clicking Button1 (convertir), convert from Euros to ptas. And by clicking on the second (invertir Conversion), must give us the inverse conversion by clicking again in button1.
    The main problem is in the loop, I can make the first change, but I don´t how to close the loop If for the second change.
    How can I do?
    Thanks a lot!
    You don't want a loop in your ActionListener; think of it: the user types a value in a text field and either presses the 'convert' button or 'inverse convert' button. Whatever button is pressed you want to do one thing only: a conversion or a reverse conversion and that's all. The user should press one of the buttons again to make something happen again.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. euro converter to dollars
    By JMAsterson in forum New To Java
    Replies: 12
    Last Post: 04-05-2012, 08:36 PM
  2. Matrix Inverse
    By TryingMybestInJava in forum New To Java
    Replies: 4
    Last Post: 12-01-2010, 12:59 AM
  3. Inverse Cos
    By dilpreet28 in forum Java Applets
    Replies: 2
    Last Post: 06-11-2010, 01:23 AM
  4. Error euro sign on oracle database
    By ct2marer in forum JDBC
    Replies: 0
    Last Post: 10-26-2009, 01:07 PM
  5. inverse engineering
    By Ed in forum JDBC
    Replies: 2
    Last Post: 07-02-2007, 08:57 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
  •