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?