Question

In: Computer Science

The purpose of this homework is to test your knowledge of GUI. Consider a fictional park...

The purpose of this homework is to test your knowledge of GUI. Consider a fictional park where the entry price for 1 adult ticket is $50, and for 1 children ticket is $25. Write a simple GUI application that let user to enter the number of tickets and display the total price. The GUI should contain:

● One text field for the user to enter the number of adult tickets

● One text field for the user to enter the number of children tickets

● One button “Calculate total cost”

● One text field to display the total cost When the user clicks the button then the correct cost is displayed in the total price field. If the input text field is empty then it should be treated as 0 tickets.

Solutions

Expert Solution

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class TicketPriceGUI {
   static JFrame frame = new JFrame("JFrame Example");

   public static void main(String s[]) {
       JPanel panel = new JPanel();
       panel.setLayout(null);
       final JLabel label3 = new JLabel("Adult ticket $50.Children ticket $25");
       label3.setBounds(150, 15, 500, 30);
       JLabel label1 = new JLabel("Enter the number of adult ticket: ");
       label1.setBounds(200, 50, 300, 30);

       final JTextField adult = new JTextField(10);
       adult.setBounds(400, 50, 100, 30);

       JLabel label2 = new JLabel("Enter the number of children ticket: ");
       label2.setBounds(200, 100, 250, 30);
       // password field
       final JTextField children = new JTextField(10);
       children.setBounds(400, 100, 100, 30);

       JButton login = new JButton();
       login.setText("Calculate total cost");
       login.setBounds(200, 150, 250, 20);

       JLabel total = new JLabel("Total Price: ");
       total.setBounds(200, 200, 300, 30);

       final JTextField totalPrice = new JTextField(10);
       totalPrice.setBounds(400, 200, 100, 30);

       JButton exit = new JButton();
       exit.setText("Exit");
       exit.setBounds(320, 150, 100, 20);

      

       login.addActionListener(new ActionListener() {

           @Override
           public void actionPerformed(ActionEvent aE) {
               String a = adult.getText();
               String c = children.getText();
               int ad = 0;
               if (a.trim().length() != 0)
                   ad = Integer.parseInt(a);
               int ac = 0;
               if (c.trim().length() != 0)
                   ac = Integer.parseInt(c);
               totalPrice.setText((ac * 25 + ad * 50) + "");

           }
       });

       panel.add(label1);
       panel.add(label2);
       panel.add(label3);
       panel.add(adult);
       panel.add(children);
       panel.add(login);
       panel.add(exit);
       panel.add(totalPrice);
       panel.add(total);
       frame.add(panel);

       frame.setSize(600, 300);
       frame.setLocationRelativeTo(null);
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.setVisible(true);
   }
}

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me


Related Solutions

The purpose of this homework is to test your knowledge of File IO and Collections and...
The purpose of this homework is to test your knowledge of File IO and Collections and the language would be java. Suppose that we have a file Employee.csv which contains employee information of a certain company. The first line of the file contains the following header: employeeId,firstName,lastName,dob,gender,position After the header line, each employee information is written on a separate line which consists of 6 comma-separated fields, for example:1234567, John, Smith,20/12/1989, M, Software Engineer Question 1. Write code to read the...
R.A.T.-Create Your Own Water Park Apply your knowledge of polynomial functions to create a water park,...
R.A.T.-Create Your Own Water Park Apply your knowledge of polynomial functions to create a water park, with 6 waterslides - one for under 6 years old (highest point at least 5m above ground) two for ages 6 to 12 (highest point at least 10m above ground) three for over age 12 (highest point at least 20 m above ground) A Create a polynomial equation for each waterslide. Show all of your work. The waterslide must begin at the y axis...
The purpose of this assignment is to enhance your writing, application of knowledge, and critical thinking...
The purpose of this assignment is to enhance your writing, application of knowledge, and critical thinking skills. In general, this assignment requires you to analyze an organization of your choice and prepare a case study covering a variety of aspects of the organization. The content will include information on the mission, strategy, culture, structure, and environment of the organization. First you need to choose an organization to examine. It can be one you worked for in the past, your present...
PURPOSE: The purpose of this assignment is to enable the students to enhance their knowledge on...
PURPOSE: The purpose of this assignment is to enable the students to enhance their knowledge on the environmental engineering and sustainable development for environmental engineering. TASK 1 (DOCUMENTATION) Development in science and technology has increase the level of human lifestyle which leads to technology advancement. However, everything good comes with a price. One of the greatest problems that the world is facing today is that of environmental pollution, increasing with every passing year and causing grave and irreparable damage to...
Purpose: To strengthen and demonstrate your knowledge of the Immune and Lymphatic System and its systemic...
Purpose: To strengthen and demonstrate your knowledge of the Immune and Lymphatic System and its systemic relationship in the body. The ability to apply this content and think systemically with physiology processes will benefit you as a healthcare student and practitioner. Criteria for Success: To be successful you will make sure you complete diagrams as instructed in the tasks, including proper values (if required) on the x & y-axis as well as labeling those. You also need to make sure...
The purpose of this problem set is to reinforce your knowledge of some basic chemical concepts...
The purpose of this problem set is to reinforce your knowledge of some basic chemical concepts that are important for the origin of the elements. 1. Use the abundances of the stable isotopes of strontium and the masses of these nuclides (found at http://atom.kaeri.re.kr/nuchart/) to calculate the atomic weight of strontium. Compare the value that you get with the value shown in the periodic table found at http://www.rsc.org/periodic-table. Show your work. 2. Consult the chart of the nuclides (http://atom.kaeri.re.kr/nuchart/ )...
Purpose: To strengthen and demonstrate your knowledge of the Immune and Lymphatic System and its systemic...
Purpose: To strengthen and demonstrate your knowledge of the Immune and Lymphatic System and its systemic relationship in the body. The ability to apply this content and think systemically with physiology processes will benefit you as a healthcare student and practitioner. Criteria for Success: To be successful you will make sure you complete diagrams as instructed in the tasks, including proper values (if required) on the x & y-axis as well as labeling those. You also need to make sure...
Write a 800 word essay. The purpose is to demonstrate your knowledge about principles of macroeconomics...
Write a 800 word essay. The purpose is to demonstrate your knowledge about principles of macroeconomics and ability to apply these principles to issues in the real world. It should be as analytical as possible; you should avoid merely describing the issue or simply repeating work by other authors. You have to pick a company and show how macroeconomic developments impact its business. It can be about trade, foreign exchange rates, foreign direct investments, financial criris or something else.
Now, it is time for you to test your knowledge of key economic indicators and the...
Now, it is time for you to test your knowledge of key economic indicators and the effects of economic measurements have on an economy. The purpose of this assignment is to investigate and to gauge your understanding of key economic indicators by filling out the figure one (1) below and to write up your findings and compare and contrast the United States to that of China. Compare and Contrast the United States to that of China Assignment should include the...
Test your basic knowledge about clotting factors and anticoagulants. biol
Test your basic knowledge about clotting factors and anticoagulants. biol
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT