I need help please JOptionPane class One of the easiest ways to provide a simple
ID: 3688482 • Letter: I
Question
I need help please
JOptionPane class One of the easiest ways to provide a simple GUI-based input and output is by using the JoptionPane class. For example, when we execute the statement JoptionPane.shosMessageDialog (null, I Love Java") : the following dialog appears on the center of the screen as follows: ILove Java oK Ul environment, there are basically two types of windows: a general-purpose frame In a G and a special-purpose dialog. In Java, we use a JFrame object for a frame window and a JDialog object for a dialog. The first argument to the showMessageDialog method is a frame object that controls this dialog, and the second argument is the text to display. If we pass n argument, the dialog appears on the center of the screen. If we pass a frame object, then the dialog is positioned at the center of the frame. 1. Run the following class. What is the difference betwee recalls ull as the first the following class. What is the difference between the two showMessageDialog eca import javax.swing.*; public class TP5 f public static void main(Stringll args) ( JFrame jFrame jFrame new JFramel): jFrame.setšize(400,300); jFrame.setvisiblel(true); JOptionPane.showMessageDialogUFrame, "How are you?"); JOptionPane.showMessageDialoginul, "one In two In three"; ): jFrame.setSize(400,300); jFr We can also use the JOptionPane class for input by using its showinputDialog method. For example, when we execute:Explanation / Answer
What I understoof from the above description is that you want to write a program in java that shows a Dialog box and prompts user to enter age.The user enters the age and then dialog box shows the age.
The problem is this we are scanner class to take input from the user .If its an integer we write command
int age = Integer.parseInt(str);
But it truncates the decimal values in case user enters a decimal value.In order to solve it,we need to change Integer by Float.Thta's it!!!
Modified Code:
public static void main(String[] args) {
JFrame jFrame; // declared the Frame
jFrame = new JFrame() ; //allocated memory to frame
jFrame.setSize(400,300); // set size for the frame
jFrame.setVisible(true); // set its visibility true
String str = JOptionPane.showInputDialog(jFrame,"Enter age:"); // Call showInputDialog to prompt user to enter the //age
float age = Float.parseFloat(str); // Scan the age given by user; It can be in decimal too
if(age.intValue() == age) JOptionPane.showMessageDialog(jFrame,age.intValue()); // if age is in integer show it as //an integer otherwise show it in decimal format
else JOptionPane.showMessageDialog(jFrame,age); }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.