import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class RetailPrice{
public static void main(String[]args){
SwingUtilities.invokeLater(createGUI());
}
private static Runnable createGUI(){
return new Runnable(){
@Override
public void run(){
JFrame f = new JFrame("Retail Price Calculator");
Container pane = f.getContentPane();
pane.setLayout(new FlowLayout());
JTextField wholesaleTF = new JTextField(10);
pane.add(new JLabel("Wholesale $"));
pane.add(wholesaleTF);
JTextField markupTF = new JTextField(2);
pane.add(new JLabel("Markup %"));
pane.add(markupTF);
JTextField retailTF = new JTextField(10);
pane.add(new JLabel("Retail $"));
pane.add(retailTF);
retailTF.setEditable(false);
JButton btn = new JButton("Go");
pane.add(btn);
f.pack();
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
btn.addActionListener(clickHandler(wholesaleTF, markupTF, retailTF));
}
};
}
private static ActionListener clickHandler(JTextField wholesaleTF, JTextField markupTF, JTextField retailTF){
return new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
retailTF.setText("");
try{
double wholesale = new Double(wholesaleTF.getText());
double markup = new Double(markupTF.getText());
double retail = ((wholesale*(markup/100.0))+wholesale);
retailTF.setText(String.format("%.2f",retail));
}catch(NumberFormatException ex){
retailTF.setText(ex.getMessage());
}
}
};
}
}
Download
Comments
Post a Comment