60 likes | 72 Views
JOptionPane. JOptionPane. Import javax.swing.JOptionPane Use showInputDialog () for input . Only string values can be input. To convert an input value from a string to an integer use the parseInt () method from the Integer class, use appleCount = Integer.parseInt ( appleString );.
E N D
JOptionPane • Import javax.swing.JOptionPane • UseshowInputDialog() for input . • Only string values can be input. • To convert an input value from a string to an integer use the parseInt() method from the Integer class, use appleCount = Integer.parseInt(appleString);
JOptionPane • Output is displayed using the showMessageDialogmethod. JOptionPane.showMessageDialog(null, "The total number of fruits = " + totalFruitCount);
JOptionPane Demo import javax.swing.JOptionPane; public class JOptionPaneDemo { public static void main(String[] args) { String appleString = JOptionPane.showInputDialog("Enter number of apples:"); intappleCount = Integer.parseInt(appleString); String orangeString= JOptionPane.showInputDialog("Enter number of oranges:"); intorangeCount = Integer.parseInt(orangeString); inttotalFruitCount = appleCount + orangeCount; JOptionPane.showMessageDialog(null, "The total number of fruits = " + totalFruitCount); System.exit(0); } }
JOptionPane Sample Screen Output
JOptionPane Cautions • If the input is not in the correct format, the program will crash. • If you omit the last line (System.exit(0)), the program will not end, even when theOK button in the output window is clicked. • Always label any output.