Image: Code: import javax.swing.*; import java.awt.BorderLayout; import java.awt
ID: 3548969 • Letter: I
Question
Image:
Code:
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.*;
public class Exercise2 extends JFrame
{
private JTextField tf1;
private JLabel label1, label2, label3;
private JPanel panel1, panel2, panel3;
private JRadioButton rb1, rb2;
public static void main(String [] args)
{
Exercise2 fr = new Exercise2 ("Currency Exchange Rates");
fr.setSize(900,225);
fr.setVisible(true);
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public Exercise2(String title)
{
tf1 = new JTextField(20);
tf1.setHorizontalAlignment(JTextField.RIGHT);
ImageIcon icon1 = new ImageIcon(getClass().getResource("mala.jpg"));// insert image
ImageIcon icon2 = new ImageIcon(getClass().getResource("sing.jpg"));
ImageIcon icon3 = new ImageIcon(getClass().getResource("indo.jpg"));
label1 = new JLabel("Amount in Ringgit Malaysia (MYR): ",icon1,JLabel.CENTER);
label2 = new JLabel("choose the exchange rate: ");
label3 = new JLabel("The amount in _____ ");
rb1 = new JRadioButton("Singapore Dollar (1 SGD): 2.42");
rb2 = new JRadioButton("Indonesian Rupiah(1 IDR): 0.00033");
panel1= new JPanel();
panel2= new JPanel();
panel3= new JPanel();
tf1.setFont(new Font("Verdana",Font.BOLD,13));
label1.setFont(new Font("Tahoma",Font.BOLD+Font.ITALIC,13));
label2.setFont(new Font("Verdana",Font.BOLD,13));
label3.setFont(new Font("Tahoma",Font.BOLD+Font.ITALIC,13));
rb1.setFont(new Font("Verdana",Font.BOLD,13));
rb1.setForeground(Color.blue);
rb2.setFont(new Font("Verdana",Font.BOLD,13));
rb2.setForeground(Color.red);
panel1.setLayout(new GridLayout(2,2));
add(panel1,BorderLayout.NORTH);
//panel1.add(lblimg1);
panel1.add(label1);
panel1.add(tf1);
panel1.add(label2);
panel2.setLayout(new GridLayout(1,2));
add(panel2,BorderLayout.CENTER);
panel2.add(rb1);
panel2.add(rb2);
add(panel3,BorderLayout.SOUTH);
panel3.setBackground(Color.yellow);
panel3.add(label3);
rb1.addActionListener(new MyAction());
rb2.addActionListener(new MyAction());
}
public class MyAction implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
float converter;
int mala_price = Integer.parseInt(tf1.getText());
float singapore = (float) 2.42;
float indonesia = (float) 0.00033;
if(rb1.isSelected())
{
converter = singapore * mala_price;
label3 = new JLabel("The amount in "+converter);
panel3.add(label3);
}
else if(rb2.isSelected())
{
converter = indonesia* mala_price;
label3= new JLabel("The amount in "+converter);
panel3.add(label3);
}
}
}
}
Please correct my code,
using comment explain my wrong.
Thank you.
Explanation / Answer
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.*;
public class Exercise2 extends JFrame implements ActionListener
{
private JTextField tf1;
private JLabel label1, label2, label3;
private JPanel panel1, panel2, panel3;
private JRadioButton rb1, rb2;
private JButton button;
private ButtonGroup grp; // need to add radio buttons to a group
public Exercise2(String title)
{
grp = new ButtonGroup();
tf1 = new JTextField(20);
tf1.setHorizontalAlignment(JTextField.RIGHT);
button = new JButton("Calculate");
ImageIcon icon1 = new ImageIcon(getClass().getResource("mala.jpg"));// insert image
ImageIcon icon2 = new ImageIcon(getClass().getResource("sing.jpg"));
ImageIcon icon3 = new ImageIcon(getClass().getResource("indo.jpg"));
label1 = new JLabel("Amount in Ringgit Malaysia (MYR): ",icon1,JLabel.CENTER);
label2 = new JLabel("choose the exchange rate: ");
label3 = new JLabel("The amount in _____ ");
rb1 = new JRadioButton("Singapore Dollar (1 SGD): 2.42");
rb2 = new JRadioButton("Indonesian Rupiah(1 IDR): 0.00033");
panel1= new JPanel();
panel2= new JPanel();
panel3= new JPanel();
tf1.setFont(new Font("Verdana",Font.BOLD,13));
label1.setFont(new Font("Tahoma",Font.BOLD+Font.ITALIC,13));
label2.setFont(new Font("Verdana",Font.BOLD,13));
label3.setFont(new Font("Tahoma",Font.BOLD+Font.ITALIC,13));
rb1.setFont(new Font("Verdana",Font.BOLD,13));
rb1.setForeground(Color.blue);
rb2.setFont(new Font("Verdana",Font.BOLD,13));
rb2.setForeground(Color.red);
panel1.setLayout(new GridLayout(2,2));
add(panel1,BorderLayout.NORTH);
//panel1.add(lblimg1);
panel1.add(label1);
panel1.add(tf1);
panel1.add(label2);
panel2.setLayout(new GridLayout(1,2));
add(panel2,BorderLayout.CENTER);
grp.add(rb1);
grp.add(rb2);
panel2.add(rb1);
panel2.add(rb2);
panel2.add(button);
add(panel3,BorderLayout.SOUTH);
panel3.setBackground(Color.yellow);
panel3.add(label3);
button.addActionListener(this); // use a button to trigger action
}
public void actionPerformed(ActionEvent e) // in the same class
{
float converter;
int mala_price = Integer.parseInt(tf1.getText());
float singapore = (float) 2.42;
float indonesia = (float) 0.00033;
String result;
if(rb1.isSelected())
{
converter = singapore * mala_price;
result = "The amount in " + converter;
label3.setText(result); // just need to set text in label
}
else if(rb2.isSelected())
{
converter = indonesia* mala_price;
result = "The amount in " + converter;
System.out.println(result);
label3.setText(result); // just need to set text in label
}
}
public static void main(String [] args)
{
Exercise2 fr = new Exercise2 ("Currency Exchange Rates");
fr.setSize(900,225);
fr.setVisible(true);
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}