2

I'm new to java and I'm trying to create a currency converter right now. I've made this little currency converter that converts Dollars into euros and pounds. When ran, it pops up the java screen and you can enter the amount of dollars you would like to convert, and it will give you the amount of euros/pounds it would be.

Now i would like to display a little Jpannel message first. I can't quite seem to figure out how. Im sure its quite easy so forgive me for my newbieness. Help would be fantastic

Sorry if the layout of my question seems odd. im not familiar with this website yet. I will improve.

Thanks in advance for any help given.

I've made this so far:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.JOptionPane;

class converter extends JFrame {

    private static final double DOLLAR_PER_EURO = 0.77;
    //private static final double   DOLLAR_PER_POUND = 0.66;

    private JTextField _dollarsTF = new JTextField(3);        
    private JTextField _eurosTF   = new JTextField(3);
    private JTextField _poundsTF = new JTextField(3);

    public converter() {

        JButton convertBtn = new JButton("Convert");
        convertBtn.addActionListener(new ConvertBtnListener());

        _dollarsTF.addActionListener((ActionListener) new ConvertBtnListener());
        _eurosTF.setEditable(false);
        _poundsTF.setEditable(false);

        JPanel content = new JPanel();        
        content.setLayout(new FlowLayout());  

        content.add(new JLabel("Enter amounts of dollars to compare: "));  
        content.add(_dollarsTF);              
        content.add(convertBtn);               
        content.add(new JLabel("Euros: "));
        content.add(_eurosTF);
     //   content.add(new JLabel("English Pounds: "));
     //   content.add(_poundsTF);
        setContentPane(content);       
        pack();                        
        setTitle("Currency converter");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);   
    }

    class ConvertBtnListener implements ActionListener {       
        public void actionPerformed(ActionEvent e) {
            String dyStr = _dollarsTF.getText();          
            int Dollars = Integer.parseInt(dyStr);                 
            double Euros = Dollars * DOLLAR_PER_EURO;
            //double Pounds = Dollars * DOLLAR_PER_POUND;
            _eurosTF.setText("" + Euros);
            // _poundsTF.setText("" + Pounds);
        }
    }

    public static void main(String[] args) {                    
        converter window = new converter();
        window.setVisible(true);
    }
}
default locale
  • 13,035
  • 13
  • 56
  • 62
Tristen
  • 31
  • 5

1 Answers1

2

From your comments, I understand what you mean. Firstly, welcome to Stack Overflow and the question formatting was fine! I'm glad to see you're showing that you've made an effort.

Now it seems to me you just want a little message to pop up. It seems pointless, to me, to write all the JPanel code to appear, when Swing has a lovely Class called JOptionPane to do those sorts of things for you. Here is the documentation.

The syntax is quite simple, for what you want.

JOptionPane.showMessageDialog("This is your message", parentObject);

This will make a small dialog box appear with your chosen message in it. The parentObject field is the container that is showing this dialog. In your case, it is the converter class (Should really be Converter). That is because converter extends JFrame. So your code would be:

JOptionPane.showMessageDialog("This is your message", this);
christopher
  • 26,815
  • 5
  • 55
  • 89