Question

In: Computer Science

Java homework problem: I need the code to be able to have a message if I...

Java homework problem:

I need the code to be able to have a message if I type in a letter instead of a number. For example, " Please input only numbers". Then, I should be able to go back and type a number.

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 LoginGui {
   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);
       totalPrice.setEditable(false);

       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);
   }
}

Solutions

Expert Solution

import java.awt.Color;
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 LoginGui {
static JFrame frame = new JFrame("JFrame Example");
static JLabel error = new JLabel("");
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, 250, 250, 20);

JLabel total = new JLabel("Total Price: ");
total.setBounds(200, 150, 300, 30);
error.setBounds(250, 200, 300, 30);
error.setForeground(Color.red);

final JTextField totalPrice = new JTextField(10);
totalPrice.setBounds(400, 150, 100, 30);
totalPrice.setEditable(false);

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

  

login.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent aE) {
String a = adult.getText();
String c = children.getText();
error.setText("");
int ad = 0;
try {
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) + "");
}catch(Exception e) {
   error.setText("Please enter numbers only: ");
}

}
});

panel.add(label1);
panel.add(error);

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, 400);
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

JAVA JAVA JAVA Hey i need to find a java code for my homework, this is...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is my first java homework so for you i don't think it will be hard for you. (basic stuff) the problem: Write a complete Java program The transport Company in which you are the engineer responsible of operations for the optimization of the autonomous transport of liquid bulk goods, got a design contract for an automated intelligent transport management system that are autonomous trucks which...
JAVA CODE BEGINNERS, I already have the DEMO CLASS(NEED YOU TO USE), I need you to...
JAVA CODE BEGINNERS, I already have the DEMO CLASS(NEED YOU TO USE), I need you to use all methods, also switch statements. Write a Temperature class. The class will have three conversion methods: toCelsius(), toKelvin() and toFahrenheit(). These methods will return a Temperature in those three scales equal to the this temperature. Note that the value of this is not changed in these conversions. In addition to these three conversion methods the class will have methods add(Temperature), subtract(Temperature), multiply(Temperature), and...
I need a full java code. And I need it in GUI With the mathematics you...
I need a full java code. And I need it in GUI With the mathematics you have studied so far in your education you have worked with polynomials. Polynomials are used to describe curves of various types; people use them in the real world to graph curves. For example, roller coaster designers may use polynomials to describe the curves in their rides. Polynomials appear in many areas of mathematics and science. Write a program which finds an approximate solution to...
Intro to java Problem, Please provide code and Pseudocode. Not able to compile correct numbers. Problem...
Intro to java Problem, Please provide code and Pseudocode. Not able to compile correct numbers. Problem 7: Simple Calculator (10 points) (General) Calculators represent the most basic, general-purpose of computing machines. Your task is to reduce your highly capable computer down into a simple calculator. You will have to parse a given mathematical expression, and display its result. Your calculator must support addition (+), subtraction (-), multiplication (*), division (/), modulus(%), and exponentiation (**). Facts ● Mathematical expressions in the...
Programming Java Homework: Find the Highest Score (Just need the code and directions followed exactly and...
Programming Java Homework: Find the Highest Score (Just need the code and directions followed exactly and written in Java code) Write a program that prompts the user to enter the number of students and each student’s name and score, and finally displays the name and score of the student with the highest score. Use the next () method in the Scanner class to read a name, rather than using the nextLine () method.
In Java please. I put down my code and what I was able to achieve so...
In Java please. I put down my code and what I was able to achieve so far: public class Animal {   private String gender; //stores the gender of the animal    private String type; //stores the type of the animal(bear of fish)    private int strength; //stores the strength of the animal    public Animal() {        gender = "none";        type = "none";        strength = 0;    }        public Animal (String g, String...
UML Diagram for this java code //java code import java.util.*; class Message { private String sentence;...
UML Diagram for this java code //java code import java.util.*; class Message { private String sentence; Message() { sentence=""; } Message(String text) { setSentence(text); } void setSentence(String text) { sentence=text; } String getSentence() { return sentence; } int getVowels() { int count=0; for(int i=0;i<sentence.length();i++) { char ch=sentence.charAt(i); if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' || ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U') { count=count+1; } } return count; } int getConsonants() { int count=0; for(int i=0;i<sentence.length();i++)...
I need an idea of Java code that will convert an integer (1 to 3,999) into...
I need an idea of Java code that will convert an integer (1 to 3,999) into roman numerals using if statements; arrays and loops sadly aren't allowed and that's all I can come up with.
I have the following python code. I get the following message when running it using a...
I have the following python code. I get the following message when running it using a test script which I cannot send here: while textstring[iterator].isspace(): # loop until we get other than space character IndexError: string index out of range. Please find out why and correct the code. def createWords(textstrings): createdWords = [] # empty list for textstring in textstrings: # iterate through each string in trxtstrings iterator = 0 begin = iterator # new begin variable while (iterator <...
I need solution of this homework Question: Queues, Deques and Priority Queues. Enter the necessary code
I need solution of this homework Question: Queues, Deques and Priority Queues. Enter the necessary code
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT