import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Question extends JFrame implements ActionListener
{
private JLabel amt,dollar,fifty,title,result,error,creator;
private TextField tb1,tb2,tb3;
private JButton count;
public Question()
{
setSize(400,300);
JPanel contentPane = new JPanel(null);
contentPane.setBorder(BorderFactory.createLineBorder(Color.gray));
setContentPane(contentPane);
count = new JButton("=");
title = new JLabel("Calculate Change");
amt = new JLabel("Enter the amount:");
dollar = new JLabel("1 Dollar Coin:");
fifty = new JLabel("50 Cent Coin:");
error = new JLabel("wrong information.");
creator = new JLabel("Created by ?????");
Font bold = new Font("Arial",Font.BOLD,15);
Font wrong = new Font("Arial",Font.ITALIC,12);
Font smaller = new Font("Arial",Font.ITALIC,10);
error.setForeground(Color.red);
tb1 = new TextField(13);
tb2=new TextField(12);
tb3= new TextField(12);
tb2.setEnabled(false);
tb3.setEnabled(false);
creator.setFont(smaller);
error.setFont(wrong);
title.setFont(bold);
title.setBounds(130,-14,150,40);
amt.setBounds(20, 20, 120, 20);
dollar.setBounds(40, 45, 120, 20);
fifty.setBounds(40, 70, 120, 20);
tb1.setBounds(160, 21, 90, 24);
tb2.setBounds(160, 45, 90, 20);
tb3.setBounds(160, 70, 90, 20);
error.setBounds(260,170,150,20);
count.setBounds(100,200,90,20);
creator.setBounds(240,220,180,20);
contentPane.add(title);
contentPane.add(amt);
contentPane.add(dollar);
contentPane.add(fifty);
contentPane.add(tb1);
contentPane.add(tb2);
contentPane.add(tb3);
contentPane.add(error);
contentPane.add(creator);
contentPane.add(count);
count.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
try
{
//String input = tb1.getText();
double convert = Double.parseDouble(tb1.getText());
if((e.getSource() == count) && (convert >=1))
{
double result;
result = convert /1;
convert = convert % 1;
int result2 = (int)result;
String result3 = String.valueOf(result2);
tb2.setText(result3);
}
if((convert >= 0.5) && (convert < 0.99))
{
tb3.setText("1");
convert = convert % 0.5;
String result4 = String.valueOf(convert);
error.setText(result4);
}
}catch(Exception ex)
{
error.setText("Invalid input.");
}
}
public static void main(String []args)
{
Question frame = new Question();
frame.setVisible(true);
}
}