GUI & event driven programing

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Zen25
    New Member
    • Mar 2017
    • 3

    GUI & event driven programing

    This is a program to create an Invoice Calculator application; the user inputs the subtotal and the application will calculate the invoice and, display the discount percent and discount amount.
    - if the subtotal is more than or equal to 200, the discount percent is 20%
    - if the subtotal is less than 200 and more than or equal to 100, discount percent is 10%
    - if the subtotal less than 100, discount percent is 0%
    The problem is I don't know how to get user input (subtotal) to be used in another class (a class called Calculate, at the bottom), it keeps saying 'Javadoc not found' but Javadoc is fine there is nothing wrong, and I think there may be something wrong with my coding.
    Code:
    package pad;
    import java.util.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.GridLayout;
    
    public class Pad extends JFrame{
        JFrame jf;//frame to place the panels
        JPanel jp;//panel to place the components
        JLabel lblSubTotal, lblDiscountPercent, lblDiscount, lblInvoice;//label to display
        JTextField txtSubTotal,txtDiscountPercent, txtDiscount, txtInvoice;
        JButton btnCalc, btnExit;//button
        
        public Pad(){
        //initialise panel
            jp= new JPanel(new GridLayout(0,2));//set rows and columns for grid layout
            //initialise the label
            lblSubTotal = new JLabel ("Subtotal:");
            lblDiscount = new JLabel ("Discount:");
            lblInvoice = new JLabel ("Invoice:");        
            
            jp.add(lblSubTotal);
            txtSubTotal =new JTextField("Enter your subtotal");//initial text box
            jp.add (txtSubTotal);
            
            jp.add (lblDiscountPercent);//add label
            txtDiscountPercent = new JTextField ();
            
            jp.add(lblDiscount);
            txtDiscount = new JTextField();
            
            jp.add(lblInvoice);
            txtInvoice = new JTextField();
            
            //initialise button
            btnCalc = new JButton ("Calculate");
            btnCalc.addActionListener(new ActionHandler());
            btnExit = new JButton ("Exit");
            btnExit.addActionListener(new ActionHandler());//add action listener
            
            //create button's panel
            JPanel btn_panel = new JPanel();
            btn_panel.add(btnCalc);
            btn_panel.add(btnExit);
            btn_panel.add(btnExit);
            jp.add(btn_panel);       
           
            //Frame setting
            jf=new JFrame();
            jf.setTitle("Invoice Total Calculator");
            //jf.setSize(800, 500);// set the size of the frame    
            jf.add(jp);//add panel to frame
            jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
            jf.setLocationRelativeTo(null);//the window is placed in the center of the screen
            jf.pack();//Causes this Window to be sized to fit the preferred size and layouts of its subcomponents.
            jf.setVisible(true); //always set at last line 
    }
    
         public static void main(String[] args){
         new Pad();   
        }
         
        class ActionHandler implements ActionListener {        
            public void actionPerformed(ActionEvent e){
                e.getActionCommand().equals("Exit");
                System.exit(0);
            }
        }
        
        class Calculate{
            double discount, discountPercent, Invoice;
            Pad pad = new Pad();
            
            
            if (pad.txtSubTotal >= 200)
            discountPercent = 0.2;
            else
            if (pad.txtSubTotal < 200 && >= 100)
            discountPercent = 0.1;
            else
            if (pad.txtSubTotal < 100)
            discountPercent = 0;
            
        }
    }
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    You have 2 action handlers (line 37 and 39). If someone clicks on the second, you try to handle that in 65, but incorrectly. So in the same way, if someone clicks on the first, you can handle that there, too. So correct these lines to:
    Code:
           public void actionPerformed(ActionEvent e){
                if (e.getActionCommand().equals("Exit")) System.exit(0);
                if (e.getActionCommand().equals("Calculate")) Calculate.run();
            }
    By the way, your class "Calculate" won't compile.
    Insert in line 74:
    Code:
    public void run() {
    and the closing bracket in line 83.

    Comment

    Working...