swing and joptionpane

18
Swing and JOptionPane Produced by: Department of Computing and Mathematics http://www.wit.ie/ Using Graphical User Interface (GUI) Components Dr. Siobhán Drohan Mr. Colm Dunphy Mr. Diarmuid O’Connor

Upload: others

Post on 16-Jul-2022

10 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Swing and JOptionPane

SwingandJOptionPane

Producedby:

DepartmentofComputingandMathematicshttp://www.wit.ie/

UsingGraphicalUserInterface(GUI)Components

Dr.Siobhán DrohanMr.ColmDunphyMr.DiarmuidO’Connor

Page 2: Swing and JOptionPane

GraphicalUser

Interface(GUI)

Page 3: Swing and JOptionPane

GraphicalUser

Interface(GUI)

Page 4: Swing and JOptionPane

GraphicalUser

Interface(GUI)

Page 5: Swing and JOptionPane

Topicslist

Swing• JOptionPane– JOptionPane methods• showMessageDialog()• showInputDialog()• showConfirmDialog()

Page 6: Swing and JOptionPane

WhatisSwing?

• Swing isasetofprogramcomponentsfor Java programmersthatallowyoutocreategraphicaluserinterface(GUI )components

Page 7: Swing and JOptionPane

WhatisSwing?

• Swing isasetofprogramcomponentsfor Java programmersthatallowyoutocreategraphicaluserinterface(GUI )components

Thismodule:

dialog/messageboxesonly

Page 8: Swing and JOptionPane

UsingSwing– import thelibrary

• WemakeSwingcomponentsavailabletousbyimportingtheSwingcomponentsatthestartoftheprogram.

import javax.swing.*;

However,* importsall theSwingcomponents(andtherearealot!)

Page 9: Swing and JOptionPane

UsingSwing– import specifics

• WemakeSwingcomponentsavailabletousbyimportingtheSwingcomponentsatthestartoftheprogram.

import javax.swing.JOptionPane;

AsweonlyplanonusingJOptionPane,wecanjustimportthatspecificSwingcomponentinsteadoftheentirelibrary.

Page 10: Swing and JOptionPane

UsingJOptionPane withSwing

• HavingimportedJOptionPane fromSwing,wecanusethesemethods:

showMessageDialog()• Simplemessageoutput

showInputDialog()• Allowsusertotypein(string)input

showConfirmDialog()• Allowtheusertochooseanoption

Page 11: Swing and JOptionPane

JOptionPane.showMessageDialog (null, "Welcome to the 12 days of Christmas”

);

showMessageDialog - SimpleMessageoutput

TextinDialogbox

ParentComponent– forourpurposes,null willworkasfirstparameter.

https://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html

Page 12: Swing and JOptionPane

JOptionPane.showMessageDialog (null, "Welcome to the first day of Christmas”,“Christmas Title”,JOptionPane.PLAIN_MESSAGE);

showMessageDialog - Messageoutputwithlabel

Thismeans‘noicon’https://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html

Textfortitle ofbox

Page 13: Swing and JOptionPane

String day = JOptionPane.showInputDialog("Welcome to Christmas week\n\n " + "Please enter the day: ","Monday");

showInputDialog - Messageinput

Prompttext

https://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html

Defaultvalue

Page 14: Swing and JOptionPane

ReadinginnumbersviaJOptionPane

• Problem:– showInputDialog() returnsaString– Soifyoutype22,itisthestring“22”,thiscan’tbeusedasnumber

• Solution– Useapredefinedmethodtoconverttoanumber.

int number = Integer.parseInt ("22");println (number + 3);

printsthenumber25.

Page 15: Swing and JOptionPane

UsingparseInt withinput

ThisconvertstheinputStringtoanIntegerandstoresitinnum.https://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html

int num = Integer.parseInt ( JOptionPane.showInputDialog(

"Enter a number between 0 and 10", "3" ));

Page 16: Swing and JOptionPane

showConfirmDialog - UsingtheYes/No option

int reply = JOptionPane.showConfirmDialog(null, “Did you watch the Late Late Toy Show on Saturday night?","Christmas Question", JOptionPane.YES_NO_OPTION );

https://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html

JOptionPane.YES_OPTION isreturnedifyoupress‘Yes’.JOptionPane.NO_OPTION isreturnedotherwise.(replywillbeassignedthisvalue)

Page 17: Swing and JOptionPane

Summary

Swing• JOptionPane

– import javax.swing.JOptionPane;

– JOptionPane methods• showMessageDialog()• showInputDialog()

– parseInt()

• showConfirmDialog()– JOptionPane.YES_NO_OPTION– JOptionPane.YES_OPTION– JOptionPane.NO_OPTION

Page 18: Swing and JOptionPane

Questions?