1. Retail Price Calculator : Create a GUI calculator where the user enters a who
ID: 3739016 • Letter: 1
Question
1. Retail Price Calculator :
Create a GUI calculator where the user enters a wholesale cost of an item and its markup percentage into textfields. When a button is pressed, display the item's retail price. For example if the item's wholesale cost is $5 and its markup percentage is 100 percent then its retail price is $10.00
2. Display the retail price with a$ and 2 digits after the decimal
3. Display "Invalid data" and change the color of the textfield when invalid data is entered in the textfields.
Explanation / Answer
Solution:
cdoe:
import javax.swing.JFrame;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class CalculatorPanel extends JFrame implements ActionListener{
JLabel l1,l2,l3,l4,l5;
JTextField f1,f2;
JButton b;
CalculatorPanel()
{
l1=new JLabel("Wholesale Cost");
l2=new JLabel("MarkUp Percentage");
l3=new JLabel("Wholesale Cost");
l4=new JLabel("MarkUp Percentage");
l5=new JLabel("Retail Price is");
f1=new JTextField(25);
f2=new JTextField(25);
b=new JButton("Calculate");
add(l1);
add(f1);
add(l3);
add(l2);
add(f2);
add(l4);
add(b);
add(l5);
b.addActionListener(this);
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
CalculatorPanel cp=new CalculatorPanel();
cp.setSize(650, 300);
cp.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String s1=f1.getText();
String s2=f2.getText();
l3.setText("Wholesale Cost "+s1);
l4.setText("MarkUp Percentage "+s2);
int a=Integer.valueOf(s1);
int b=Integer.valueOf(s2);
int c=a+a*b/100;
l5.setText("Retail Price is "+c);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.