Monday, 1 July 2013

GUI New Home Application

GUI New Home Application

Java Code:


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class JMyNewHome extends JFrame
{

       public static void main(String[] arg)
    {
             
       JMyNewHome window = new JMyNewHome();
       window.setVisible(true);
    }

       JCheckBox aspen = new JCheckBox("Aspen-$100,000");
       JCheckBox brittany = new JCheckBox("Brittany-$120,000");
       JCheckBox colonial = new JCheckBox("Colonial-$180,00");
       JCheckBox dartmoor = new JCheckBox("Dartmoor-$250,000");
       JCheckBox twobed = new JCheckBox("2 Bed-$21,000");
       JCheckBox threebed = new JCheckBox("3 Bed-$31,500");
       JCheckBox fourbed = new JCheckBox("4 Bed-$42,000");
       JCheckBox carzero = new JCheckBox("0 Car-$0");
       JCheckBox carone = new JCheckBox("1 Car-$7,775");
       JCheckBox cartwo = new JCheckBox("2 Car-$15,550");
       JCheckBox carthree = new JCheckBox("3 Car-$23,325");
       int bprice;
       int hprice;
       int cprice;
       int a;
      
       JTextField price = new JTextField(10);
       Container contentPane = getContentPane();
      
    public JMyNewHome()
    {
       setTitle("My New Home");
        setLayout(new FlowLayout());
       
        contentPane.add(aspen);
       contentPane.add(brittany);
       contentPane.add(colonial);
       contentPane.add(dartmoor);
       contentPane.add(twobed);
       contentPane.add(threebed);
       contentPane.add(fourbed);
       contentPane.add(carzero);
       contentPane.add(carone);
       contentPane.add(cartwo);
       contentPane.add(carthree);
       contentPane.add(price);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(500,150);
        ButtonGroup house = new ButtonGroup();
        house.add(aspen);
        house.add(brittany);
        house.add(colonial);
        house.add(dartmoor);
        ButtonGroup beds = new ButtonGroup();
        beds.add(twobed);
        beds.add(threebed);
        beds.add(fourbed);
        ButtonGroup cars = new ButtonGroup();
        cars.add(carzero);
        cars.add(carone);
        cars.add(cartwo);
        cars.add(carthree);
              ClickButtonListener clickListener = new ClickButtonListener();
       aspen.addActionListener(clickListener);
       brittany.addActionListener(clickListener);
       colonial.addActionListener(clickListener);
       dartmoor.addActionListener(clickListener);
       twobed.addActionListener(clickListener);
       threebed.addActionListener(clickListener);
       fourbed.addActionListener(clickListener);
       carzero.addActionListener(clickListener);
       carone.addActionListener(clickListener);
       cartwo.addActionListener(clickListener);
       carthree.addActionListener(clickListener);
    }

        public class ClickButtonListener implements ActionListener
        {
              public void actionPerformed(ActionEvent e)
       {
              Object o = e.getSource();
              if(o==aspen)
              {
                     if(aspen.isSelected())
                     {
                     hprice = 100000;    
                     }
              }
              else if(o==brittany)
              {
                     if(brittany.isSelected())
                     {
                     hprice = 120000;          
                     }
              }
              else if(o==colonial)
              {
                     if(colonial.isSelected())
                     {
                     hprice = 180000;
                     }     
              }
              else if(o==dartmoor)
              {
                     if(dartmoor.isSelected())
                     {
                     hprice = 250000;    
                     }
              }
              else if(o==twobed)
              {
                     if(twobed.isSelected())
                     {
                     bprice = 21000;     
                     }
              }
              else if(o==threebed)
              {
                     if(threebed.isSelected())
                     {
                     bprice = 31500;
                     }     
              }
              else if(o==fourbed)
              {
                     if(fourbed.isSelected())
                     {
                     bprice = 42000;     
                     }
              }
              else if(o==carzero)
              {
                     if(carzero.isSelected())
                     {
                           cprice = 0;
                     }     
              }
              else if(o==carone)
              {
                     if(carone.isSelected())
                     {
                     cprice = 7775;      
                     }
              }
              else if(o==cartwo)
              {
                     if(cartwo.isSelected())
                     {
                     cprice = 15550;     
                     }
              }
              else if(o==carthree)
              {
                     if(carthree.isSelected())
                     {
                     cprice = 23325;     
                     }
              }
             
              a=hprice+bprice+cprice;
                     System.out.println(a);
                     price.setText(Integer.toString(a));
                    
              }
        }

}

Screenshots: 


 

No comments:

Post a Comment