Coding Java Assignment

by - January 06, 2014



Subject : Programming in Java
Aim to provide a good understanding of Object-Oriented concepts in Java, exception handling and GUI programming. it apply object-oriented programming concepts in Java in problem solving.
question :
by click the button change the image



import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
GridLayout, FlowLayout public class may2009 extends JFrame implements ActionListener {
    JButton bt1, bt2, bt3;
    JLabel lb;
    ImageIcon[] image = {
        new ImageIcon("build/classes/elephant.jpg"),
        new ImageIcon("build/classes/mickey.jpg"),
        new ImageIcon("build/classes/butterfly.jpg")
    };
    public static void main(String[] args) {
        may2009 f = new may2009();
        f.setSize(700, 500);
        f.setVisible(true);
        f.setTitle("Button and Icons");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public may2009() {
        bt1 = new JButton("Elephant");
        bt2 = new JButton("Mickey");
        bt3 = new JButton("Butterfly");
        lb = new JLabel();
        JPanel p1 = new JPanel();
        p1.add(lb);
        JPanel p2 = new JPanel();
        p2.add(bt1);
        p2.add(bt2);
        p2.add(bt3);
        setLayout(new BorderLayout());
        add(p1, BorderLayout.NORTH);
        add(p2, BorderLayout.SOUTH);
        bt1.addActionListener(this);
        bt2.addActionListener(this);
        bt3.addActionListener(this);
    }
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == bt1) {
            lb.setIcon(image[0]);
        }
        if (e.getSource() == bt2) {
            lb.setIcon(image[1]);
        }
        if (e.getSource() == bt3) {
            lb.setIcon(image[2]);
        }
    }
} 

insert the amount of RM select the currency, automatic display the amount.




import javax.swing.*; //all button
import java.awt.event.*; //handling event
import java.awt.*; // layout GridLayout, FlowLayout
import java.text.DecimalFormat;
public class sept2012Q2 extends JFrame implements ItemListener {
    JLabel lb1, lb2, lb3;
    JTextField tf;
    JRadioButton rd1, rd2;
    ImageIcon icon1, icon2, icon3;

    public static void main(String[] args) {
        sept2012Q2 f = new sept2012Q2();
        f.setSize(500, 200);
        f.setVisible(true);
        f.setTitle("Currency Exchange Rates");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public sept2012Q2() {
            icon1 = new ImageIcon("build/classes/Malaysia.png");

            lb1 = new JLabel("Amount in Ringgit Malaysia (MYR)", icon1, JLabel.LEFT);
            lb2 = new JLabel("Choose the exchange rate");
            lb3 = new JLabel("the amount in______");
            rd1 = new JRadioButton("Singapora Dollar(1 SGD):2.42");
            rd2 = new JRadioButton("Indonesian Rupiah(1 IDR):0.00033");

            tf = new JTextField(5);
            tf.setHorizontalAlignment(JTextField.RIGHT);

            JPanel p1 = new JPanel();
            p1.add(lb1);
            p1.add(tf);
            p1.add(lb2);

            p1.setLayout(new GridLayout(2, 2));
            JPanel p2 = new JPanel();
            p2.add(rd1);
            p2.add(rd2);
            p2.setLayout(new GridLayout(1, 2));
            JPanel p3 = new JPanel();
            p3.add(lb3);
            p3.setBackground(Color.yellow);

            ButtonGroup bg = new ButtonGroup(); //choose one radio
            bg.add(rd1);
            bg.add(rd2);

            setLayout(new BorderLayout());
            add(p1, BorderLayout.NORTH);
            add(p2, BorderLayout.CENTER);
            add(p3, BorderLayout.SOUTH);

            rd1.addItemListener(this);
            rd2.addItemListener(this);

        } //constructor
    public void itemStateChanged(ItemEvent e) {
        icon2 = new ImageIcon("build/classes/Indonesia.png");
        icon3 = new ImageIcon("build/classes/Singapore.png");
        DecimalFormat df1 = new DecimalFormat("0.00");
        double rate = 0;
        String text = "";
        double rm = Double.parseDouble(tf.getText());
        if (rd1.isSelected()) {
            rate = 2.42;
            text = "SGD";
            lb3.setIcon(icon3);
        }
        if (rd2.isSelected()) {
            rate = 0.00033;
            text = "IDR";
            lb3.setIcon(icon2);
        }
        double answer = rm / rate;
        lb3.setText("The amount in" + text + " is $" + df1.format(answer));
    }
}
 

You May Also Like

0 comments