Question:
I just started learning and reading about GUI's in JAVA. I just have a question.?
Jordan
2011-06-13 03:11:15 UTC
Okay, so I just started reading about it and trying things. I'm just a little confused here.
In this example:
String firstNumber =
JOptionPane.showInputDialog("Enter first integer: ");
String secondNumber =
JOptionPane.showInputDialog("Enter second integer: ");

int number1 = Integer.parseInt(firstNumber);
int number2 = Integer.parseInt(secondNumber);

int sum = number1 + number2;
JOptionPane.showMessageDialogue(null, "The sum is " +sum, "Sum of Two Integers", JOptionPane.PLAIN_MESSAGE)


My questions are:

Why are the variables "firstNumber" and "secondNumber" declared as String? if the expected input is an integer.

What is the use of null?

What is the use of JOptionPane.PLAIN_MESSAGE?

pLEASE explain and provide practical examples.

Please explain it well because I'm new to GUI's


Thank you =(
Three answers:
deonejuan
2011-06-13 07:20:20 UTC
JOptionPanel.showMessage has null as its first argument and it will come up in the center of your monitor. When your program quits, the dialog dies with the program.



Suppose tho', you had a second window to your main window. You want to center the JOptionPane dialog in that 2nd window. Instead of null, you pass the reference of the 2nd window. Passing the reference of any window will make the dialog center in that window, and furthermore, require the user to click OK or Cancel to dismiss the dialog and return to the main program.



Any time you see .CAPITALS that is a Swing Constant. eg, JOptionPane.PLAIN_MESSAGE will fetch a specific icon for your dialog (I think it is a ! inside of a box). Swing has an extensive chart of the statics. For instance, JFrame.EXIT_ON_CLOSE is the same as int value '3'.



Don't spend too much time on the JOption. They are daunting at first because of all the pre-sets. But JOption and JDialog are easy.



The power in Swing is the setLayout(). Learn the different Layouts to make gui inside the JFrame.
AnalProgrammer
2011-06-13 10:32:16 UTC
The documentation for the method showInputDialogue is here

http://download.oracle.com/javase/7/docs/api/javax/swing/JOptionPane.html#showInputDialog(java.lang.Object)

The return value from this method is always a String.



The documentation for the method showMessageDialogue is here

http://download.oracle.com/javase/7/docs/api/javax/swing/JOptionPane.html

(sorry there is a space in the link I want to use so it will not work. Instead you have the whole documentation for JOptionPane. You will have to scroll down to the method showMessageDialogue and then click on the link. You can also use Find on this page in edit.)

The value null is used to link the message to a dummy frame. Usually placing the message in the centre of the screen.



PLAIN_MESSAGE is in capitals so it is a class variable. This sets the message type. Other options are ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE or QUESTION_MESSAGE.



Have fun.
2011-06-13 10:11:51 UTC
Start learning english first boiiii


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...