Welcome to Dream.In.Code
Become a Java Expert!

Join 150,170 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,302 people online right now. Registration is fast and FREE... Join Now!




calculate price after discount

 
Reply to this topicStart new topic

calculate price after discount, gui applet

sya_lea23
23 Aug, 2008 - 11:07 PM
Post #1

New D.I.C Head
*

Joined: 23 Aug, 2008
Posts: 4


CODE

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

//to allow use of message box
import javax.swing.JOptionPane;
//to allow use of ActionListener
import java.awt.event.*;




class GUIExample1 extends JFrame implements ActionListener{
    
    //define interface components
    JLabel numberLabel, numberLabel2;
    JTextField numberPriceField, numberDiscountField;
    JButton displayButton;
    
    public GUIExample1(){
        
    Container pane = getContentPane();
    pane.setLayout(new GridLayout (3, 2, 2, 2));
    numberLabel = new JLabel ("Price: ");
    numberPriceField = new JTextField (10);
    numberLabel2 = new JLabel("Discount");
    numberDiscountField = new JTextField (10);
    displayButton = new JButton ("Calculate");
    
    pane.add(numberLabel);
    pane.add(numberPriceField);
    pane.add(numberLabel2);
    pane.add(numberDiscountField);
    pane.add(displayButton);
    displayButton.addActionListener(this);
    
    
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setTitle ("CALCULATE PRICE AFTER DISCOUNT");
    pack();
    show();
    
    }
    
    
    public static void main (String[] args){
        new GUIExample1();
    }
    
    public void actionPerformed(ActionEvent event){
        Object source = event.getSource();
        
    if (source == displayButton){
        
        try{
            
        double price = Double.parseDouble(numberPriceField.getText());
        double discount = Double.parseDouble(numberDiscountField.getText());
        double result =price-((discount/100)*price);
        JOptionPane.showMessageDialog(null, "Price after discount is RM" +(result),
                                      "Hello", JOptionPane.PLAIN_MESSAGE);
        System.exit(0);
    }
        catch (Exception e){
        JOptionPane.showMessageDialog(null, "You have entered an invalid value. Please enter number", "ERROR",
        JOptionPane.PLAIN_MESSAGE);
    }
    
    }

}
}



i have created this code to calculate price after discount.
apparently, the output will appear in message box.
im confused..
i should create it as a page.
there will be no message box.
how can i view it with applet viewer as a page, not a message box?
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Calculate Price After Discount
24 Aug, 2008 - 07:47 AM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,660



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Well the reason it is showing in a messagebox is that you use JOptionPane.showMessageDialog which shows a message dialog. Instead of printing your result with this, try adding a JLabel to your GUI, or some other control, and setting its text value to the message instead. That way the result of your calculation will set the value of a control on your form instead of popping up a message box with showMessageDialog.

Lets say I had a JLabel called "output" then you could do something like output.setText("Price after discount is RM" + result.toString());

Same with any error messages which use showMessageDialog. Instead either set a label or other control to the value.

Hope this is what you were looking for.

"At DIC we be label setting and showMessageDialog removing code ninjas... output.setText("We be kewl like dat");" decap.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 03:03AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month