Created
October 1, 2018 03:24
-
-
Save Gzoref/fd2d236d6cdc23190cb4aad7ffa7185d to your computer and use it in GitHub Desktop.
First GUI that I made that computes figures.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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