Code for converting peso-dollar written in Java:
(No available screenshots for now. :))
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class PesoDollar extends JFrame{
private JTextField pesoTF, dollarTF;
private JLabel pesoL, dollarL;
private Font fontBold;
private static final int JFRAME_WIDTH = 600;
private static final int JFRAME_LENGTH = 100;
private JButton exitB, clearAllB;
private CalculatePesoTextFieldHandler cpHandler;
private CalculateDollarTextFieldHandler cdHandler;
private ExitButtonHandler ebHandler;
private ClearAllButtonHandler clbHandler;
public PesoDollar(){
super("PESO-DOLLAR CONVERTER");
fontBold = new Font ("Arial Black", Font.BOLD, 12);
pesoL = new JLabel("Peso Value:", SwingConstants.RIGHT);
pesoL.setFont(fontBold);
dollarL = new JLabel("Dollar Value: ", SwingConstants.RIGHT);
dollarL.setFont(fontBold);
pesoTF = new JTextField(15);
dollarTF = new JTextField(15);
pesoTF.setToolTipText("Input amount of Peso and press 'ENTER' key.");
dollarTF.setToolTipText("Input amount of Dollar and press 'ENTER' key.");
//Create and set the clear all Button.
clearAllB = new JButton("Clear All");
clbHandler = new ClearAllButtonHandler();
clearAllB.addActionListener(clbHandler);
clearAllB.setToolTipText("Clears the entire text field.");
//Create and set the exit Button.
exitB = new JButton("Quit");
ebHandler = new ExitButtonHandler();
exitB.addActionListener(ebHandler);
exitB.setToolTipText("Exit the program.");
Container con = getContentPane();
//con.setLayout(new GridLayout(3,2));
con.add(pesoL);
con.add(pesoTF);
con.add(dollarL);
con.add(dollarTF);
con.add(clearAllB);
con.add(exitB);
cpHandler = new CalculatePesoTextFieldHandler();
pesoTF.addActionListener(cpHandler);
cdHandler = new CalculateDollarTextFieldHandler();
dollarTF.addActionListener(cdHandler);
setLayout(new FlowLayout());
setSize(JFRAME_WIDTH, JFRAME_LENGTH);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public class CalculatePesoTextFieldHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
double peso = Double.parseDouble(pesoTF.getText());
double answer= (peso/46);
dollarTF.setText("$" + "" + answer);
}
}
public class CalculateDollarTextFieldHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
double dollar = Double.parseDouble(dollarTF.getText());
double answer= (dollar * 46);
pesoTF.setText("Php " + "" + answer);
}
}
public class ClearAllButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
pesoTF.setText("");
dollarTF.setText("");
}
}
public class ExitButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
int resp = JOptionPane.showConfirmDialog(null, "Are you sure you want to quit?", "QUIT", JOptionPane.YES_NO_OPTION);
if(resp == JOptionPane.YES_OPTION)
System.exit(0);
}
}
public static void main(String[] args){
setDefaultLookAndFeelDecorated(true);
PesoDollar pesodollarObject = new PesoDollar();
}
}
(No available screenshots for now. :))
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class PesoDollar extends JFrame{
private JTextField pesoTF, dollarTF;
private JLabel pesoL, dollarL;
private Font fontBold;
private static final int JFRAME_WIDTH = 600;
private static final int JFRAME_LENGTH = 100;
private JButton exitB, clearAllB;
private CalculatePesoTextFieldHandler cpHandler;
private CalculateDollarTextFieldHandler cdHandler;
private ExitButtonHandler ebHandler;
private ClearAllButtonHandler clbHandler;
public PesoDollar(){
super("PESO-DOLLAR CONVERTER");
fontBold = new Font ("Arial Black", Font.BOLD, 12);
pesoL = new JLabel("Peso Value:", SwingConstants.RIGHT);
pesoL.setFont(fontBold);
dollarL = new JLabel("Dollar Value: ", SwingConstants.RIGHT);
dollarL.setFont(fontBold);
pesoTF = new JTextField(15);
dollarTF = new JTextField(15);
pesoTF.setToolTipText("Input amount of Peso and press 'ENTER' key.");
dollarTF.setToolTipText("Input amount of Dollar and press 'ENTER' key.");
//Create and set the clear all Button.
clearAllB = new JButton("Clear All");
clbHandler = new ClearAllButtonHandler();
clearAllB.addActionListener(clbHandler);
clearAllB.setToolTipText("Clears the entire text field.");
//Create and set the exit Button.
exitB = new JButton("Quit");
ebHandler = new ExitButtonHandler();
exitB.addActionListener(ebHandler);
exitB.setToolTipText("Exit the program.");
Container con = getContentPane();
//con.setLayout(new GridLayout(3,2));
con.add(pesoL);
con.add(pesoTF);
con.add(dollarL);
con.add(dollarTF);
con.add(clearAllB);
con.add(exitB);
cpHandler = new CalculatePesoTextFieldHandler();
pesoTF.addActionListener(cpHandler);
cdHandler = new CalculateDollarTextFieldHandler();
dollarTF.addActionListener(cdHandler);
setLayout(new FlowLayout());
setSize(JFRAME_WIDTH, JFRAME_LENGTH);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public class CalculatePesoTextFieldHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
double peso = Double.parseDouble(pesoTF.getText());
double answer= (peso/46);
dollarTF.setText("$" + "" + answer);
}
}
public class CalculateDollarTextFieldHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
double dollar = Double.parseDouble(dollarTF.getText());
double answer= (dollar * 46);
pesoTF.setText("Php " + "" + answer);
}
}
public class ClearAllButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
pesoTF.setText("");
dollarTF.setText("");
}
}
public class ExitButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
int resp = JOptionPane.showConfirmDialog(null, "Are you sure you want to quit?", "QUIT", JOptionPane.YES_NO_OPTION);
if(resp == JOptionPane.YES_OPTION)
System.exit(0);
}
}
public static void main(String[] args){
setDefaultLookAndFeelDecorated(true);
PesoDollar pesodollarObject = new PesoDollar();
}
}
Your thoughts about this post? Leave a commment now.