Skip to content

Instantly share code, notes, and snippets.

@Gzoref
Created October 1, 2018 03:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gzoref/fd2d236d6cdc23190cb4aad7ffa7185d to your computer and use it in GitHub Desktop.
Save Gzoref/fd2d236d6cdc23190cb4aad7ffa7185d to your computer and use it in GitHub Desktop.
First GUI that I made that computes figures.
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/*
* Name: Geoffrey Zoref
* Date: 9/30/2018
* Project: Monthly Sales Tax - GUI
*/
public class MonlySalesTax extends JFrame {
private JPanel panel; // To reference a panel
private JLabel messageLabel; // To reference a label
private JTextField costTextField; // To reference a text field
private JTextField markupTextField;
private JButton calcButton; // To reference a button
private final int WINDOW_WIDTH = 340; // Window width
private final int WINDOW_HEIGHT = 100; // Window height
public MonlySalesTax() {
super("Retail Price Calculator");
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buildPanel();
add(panel);
setVisible(true);
}
private void buildPanel() {
messageLabel = new JLabel("Enter wholesale cost of item.");
// messageLabel = new JLabel("Enter markup by percentage");
costTextField = new JTextField(10);
markupTextField = new JTextField(10);
calcButton = new JButton("Calculate");
calcButton.addActionListener(new CalcButtonListener());
panel = new JPanel();
panel.add(messageLabel);
panel.add(costTextField);
panel.add(markupTextField);
panel.add(calcButton);
}
private class CalcButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String str1, str2;
double cost = 0, markup = 0, retailCost = 0;
str1 = costTextField.getText();
str2 = markupTextField.getText();
retailCost = (Double.parseDouble(str1) * Double.parseDouble(str2)/100) + Double.parseDouble(str1) ;
JOptionPane.showMessageDialog(null, "wholesale cost is $" + str1 + "\nMarkup is " + str2 + "%\nRetail cost is " + retailCost);
}
}
public static void main(String[] args) {
new MonlySalesTax();
}
}
/**
* Create a GUI application where the user enters the wholesale cost of an item and its markup percentage into text
* fields. (For example, if an item’s wholesale cost is $5 and its markup percentage is 100%, then its retail price is
* $10.) The application should have a button that displays the item’s retail price when clicked.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment