Results 1 to 5 of 5

Thread: Help me

  1. #1
    kader is offline Member
    Join Date
    Sep 2008
    Posts
    3
    Rep Power
    0

    Default Help me

    Hi,i am new in java. I wanna complete my java code project.The situation is Check box 1(cg) and 2(cg1) are difference. How i wanna set the label l2 to add selected item from check box 1 and 2. Let say, if i choose tomato and lamb, the price in l2 will display the price of tomato + price of lamb. I'm really appreciate if anyone could help me. Thanks a lot.


    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;

    public class GUI4 extends Applet implements ItemListener{



    Label l1=new Label("Mark Diner MENU");
    Label l2=new Label("Dinner Price Is RM0");

    CheckboxGroup cg = new CheckboxGroup();
    Checkbox c1=new Checkbox ("Tomato",cg,false);
    Checkbox c2=new Checkbox ("Vegetable",cg,false);
    Checkbox c3=new Checkbox ("Lemon Rice",cg,false);

    Choice ch = new Choice();

    CheckboxGroup cg1 = new CheckboxGroup();
    Checkbox c4=new Checkbox ("Lamb",cg1,false);
    Checkbox c5=new Checkbox ("Eggplant",cg1,false);
    Checkbox c6=new Checkbox ("Beer",cg1,false);

    Choice ch1 = new Choice();

    Panel p=new Panel(new FlowLayout());
    Panel p1=new Panel(new GridLayout(3,1,1,1));
    Panel p2=new Panel(new GridLayout(3,1,1,1));

    Font f1=new Font ("Ariel",Font.BOLD,24);
    Font f2=new Font ("Ariel",Font.BOLD,24);

    public void init(){
    setLayout (new BorderLayout());
    setSize(400,400);

    p1.add(c1); //add radio buttons to panel p1
    p1.add(c2);
    p1.add(c3);
    add(p1,"West");

    p2.add(c4); //add radio buttons to panel p2
    p2.add(c5);
    p2.add(c6);
    add(p2,"East");


    l1.setFont(f1); //font for label 1
    l1.setForeground(Color.MAGENTA);
    l1.setForeground(Color.YELLOW);
    l1.setAlignment(Label.CENTER);
    add(l1,"North");

    l2.setFont(f2);
    l2.setForeground(Color.BLUE);
    l2.setAlignment(Label.CENTER);
    add(l2,"Center");

    c1.addItemListener(this);
    c2.addItemListener(this);
    c3.addItemListener(this);
    c4.addItemListener(this);
    c5.addItemListener(this);
    c6.addItemListener(this);

    }

    public void itemStateChanged(ItemEvent e){

    double amount=0;
    if(e.getSource()==c1)
    l2.setText("$1.0");
    else if(e.getSource()==c2)
    l2.setText("$1.50");
    else if(e.getSource()==c3)
    l2.setText("$2.0");
    else if(e.getSource()==c4)
    l2.setText("$1.50");
    else if(e.getSource()==c5)
    l2.setText("$2.0");
    else if(e.getSource()==c6)
    l2.setText("$1.50");


    }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    Are you getting errors with your program?
    If so copy and post them here.
    Is the output from the program wrong?
    If so copy and post it here with an explanation of what's wrong and what it needs to be.

  3. #3
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default

    Indeed, post what's wrong. Btw, you could try to check if the itemStateChanged method actually called by printing a line.
    I die a little on the inside...
    Every time I get shot.

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    public class GUI4 extends Applet implements ItemListener{
        ...
        double leftPrice = 0, rightPrice = 0;
        ...
        public void itemStateChanged(ItemEvent e){
            if(e.getStateChange() == ItemEvent.SELECTED) {
                Object source = e.getSource();
                if(source==c1)
                    leftPrice = 1.0;
                else if(source==c2)
                    leftPrice = 1.50;
                else if(source==c3)
                    leftPrice = 2.0;
                else if(source==c4)
                    rightPrice = 1.50;
                else if(source==c5)
                    rightPrice = 2.0;
                else if(source==c6)
                    rightPrice = 1.50;
                setPriceLabel();
            }
        }
    
        private void setPriceLabel() {
            double total = leftPrice + rightPrice;
            l2.setText("$" + total);
        }
    }

  5. #5
    kader is offline Member
    Join Date
    Sep 2008
    Posts
    3
    Rep Power
    0

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •