Hey there! Sign in to join this conversationNew here? Join for free
    • Thread Starter
    Offline

    0
    ReputationRep:
    I cant get them to add together, please can someone tell me where Im going wrong.
    thanks in advance


    [code/]
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import java.awt.*;
    import javax.swing.*;

    public class samplle extends JFrame
    {
    // instance variables - replace the example below with your own
    static JPanel panel = new JPanel();
    static JFrame frame = new JFrame();

    // 2. set up centerPanel for central area
    static JPanel centerPanel = new JPanel();

    static JCheckBox chk1;
    static JCheckBox chk2;
    static JCheckBox chk3;
    static double CPU1= 822.49;
    static double CPU2 = 263.32;
    static double HardD1 = 530.97;
    static double price1;
    static double price2;
    static double price3;
    static double TotalPrice;


    JButton addButton = new JButton();
    JLabel subButton = new JLabel();



    public samplle ()
    {
    frame = new JFrame("Desktop Window");
    frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    Dimension minimumSize = new Dimension(550, 450);
    frame.setMinimumSize(minimumSize );

    frame.setLocationRelativeTo(null );
    frame.setDefaultLookAndFeelDecor ated(true);

    frame.setLayout(new BorderLayout());
    //frame.setLayout(new FlowLayout());
    frame.getContentPane().setBackgr ound(Color.blue);
    frame.setLayout(new BorderLayout());
    //second panel in the middle of window

    JPanel centerPanel = new JPanel();//(new GridLayout(5, 1));
    centerPanel.setLayout(new GridLayout(10,1));
    addButton = new JButton("Add to basket");

    chk1 = new JCheckBox("Intel CPU Core i7 965");
    chk2 = new JCheckBox("Intel CPU Core 2 Quad");
    chk3 = new JCheckBox("HP 600GB SAS LFF Hard Drive");
    centerPanel.add(chk1);
    centerPanel.add(chk2);
    centerPanel.add(chk3);

    frame.add(centerPanel,BorderLayo ut.CENTER);
    /******************************** **************************/
    JPanel buttonPanel = new JPanel ();
    buttonPanel.setLayout(new FlowLayout());

    addButton = new JButton("Add to basket");
    subButton = new JLabel("Subtotal: " + TotalPrice);
    buttonPanel.add(subButton);
    buttonPanel.add(addButton);

    addButton.addActionListener(new Action());
    //another panel in the right of window
    frame.add(buttonPanel,BorderLayo ut.EAST);
    /******************************** **************************/
    frame.setVisible(true);


    }
    static class Action implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
    if (chk1.isSelected())
    {
    price1 = CPU1;
    TotalPrice = price1 + price2 + price3 ;
    }
    if (chk2.isSelected()) {
    price2 = CPU2 ;
    TotalPrice = price1 + price2 + price3 ;
    }
    if (chk3.isSelected()) {
    price3 = HardD1 ;
    TotalPrice = price1 + price2 + price3 ;
    }
    }
    }
    }

    [code/]
    Offline

    15
    ReputationRep:
    (Original post by ellishnoo)
    I cant get them to add together, please can someone tell me where Im going wrong.
    thanks in advance


    [code/]
    <snipped>
    [code/]
    You can use the code syntax as follows:

    [code] Java code here [/code]

    With reference to your code, there is no statement anywhere which changes the text. The numbers are adding together fine, but that subButton JLabel you have needs to be updated when the subtotal changes.
 
 
 
Reply
Submit reply
TSR Support Team

We have a brilliant team of more than 60 Support Team members looking after discussions on The Student Room, helping to make it a fun, safe and useful place to hang out.

Updated: December 17, 2010
  • See more of what you like on The Student Room

    You can personalise what you see on TSR. Tell us a little about yourself to get started.

  • Poll
    Did TEF Bronze Award affect your UCAS choices?
    Useful resources
  • See more of what you like on The Student Room

    You can personalise what you see on TSR. Tell us a little about yourself to get started.

  • The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

    Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

    Quick reply
    Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.