Skip to content

Instantly share code, notes, and snippets.

@L8RFN
Last active January 3, 2024 08:38
Show Gist options
  • Save L8RFN/443c3a3f3d638c5a6f0c1b1a5fd9def2 to your computer and use it in GitHub Desktop.
Save L8RFN/443c3a3f3d638c5a6f0c1b1a5fd9def2 to your computer and use it in GitHub Desktop.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class GUI extends JFrame implements ActionListener{
JPanel left, topRight, bottomLeft;
JLabel dollar, can;
JTextField dolNum,canNum;
JButton convert, clear;//another way
public GUI() {
left = new JPanel();
topRight = new JPanel();
bottomLeft = new JPanel();
dollar = new JLabel("US Dollars");
can = new JLabel("Canadian Dollars");
dolNum = new JTextField();
canNum = new JTextField();
convert = new JButton("Convert");
clear = new JButton("Clear");
left.setLayout(new GridLayout(3,1));
left.add(dollar);
left.add(can);
topRight.setLayout(left.getLayout());
topRight.add(dolNum);
topRight.add(canNum);
topRight.add(bottomLeft);
bottomLeft.setLayout(new BorderLayout());
convert.addActionListener(this);
bottomLeft.add(convert, BorderLayout.EAST);
add(left, BorderLayout.WEST);
add(topRight);
setTitle("الحمدلله");
setVisible(true);
setSize(480,280);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed (ActionEvent ae)
{
if (ae.getSource() == convert)
canNum.setText(String.valueOf(Double.parseDouble(dolNum.getText()) * 1.5));//value of,, because it only accept String
else if (ae.getSource() == clear)
{
canNum.setText("");
dolNum.setText("");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment