Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of JOptionPane in Swing

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the use of JOptionPane in Swing, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

JOptionPane class is provided in Swing to realize the function similar to MessageBox in Windows platform, and it is also available in Java. Various standard dialogs are generated by using various static methods in JOptionPane class, and functions such as displaying information, asking questions, warning, user input parameters and so on are realized. These dialogs are modal dialogs.

◆ ConfirmDialog--- confirmation dialog box, asking questions, and then by the user to confirm (press "Yes" or "No" button)

◆ InputDialog--- prompts for text

◆ MessageDialog--- display information

◆ OptionDialog-- combines the other three dialog types.

These four dialogs can be displayed with showXXXDialog (), such as showConfirmDialog () display confirmation dialog box, showInputDialog () display input text dialog box, showMessageDialog () display information dialog box, showOptionDialog () display optional dialog box. The parameters they use are described as follows:

1.ParentComponent: indicates the parent window object of the dialog box, usually the current window. You can also use the default Frame as the parent window for null, where the dialog box will be set in the middle of the screen.

2. Message: indicates the descriptive text to be displayed in the dialog box

3.String title: title clause string.

4.Component: the components to be displayed in the dialog box (such as buttons)

5.Icon: the icon to display in the dialog box

6.messageType: generally, the values ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE, QUESTION_MESSAGE, PLAIN_MESSAGE,

7.optionType: it determines the button options to be displayed at the bottom of the dialog. Generally, it can be DEFAULT_OPTION, YES_NO_OPTION, YES_NO_CANCEL_OPTION, OK_CANCEL_OPTION.

Examples of use:

(1) display MessageDialog

JOptionPane.showMessageDialog (null, "descriptive text displayed in dialog box", "title bar text string", JOptionPane.ERROR_MESSAGE)

(2) display ConfirmDialog

JOptionPane.showConfirmDialog (null, "choose one", "choose one", JOptionPane.YES_NO_OPTION)

(3) display OptionDialog: this dialog box allows the user to set the number of buttons and return the sequence number of each button clicked by the user (counting from 0)

Object [] options = {"OK", "cancel", "help"}

Int response=JOptionPane.showOptionDialog (this, "this is an options dialog box

Users can choose their own number of buttons "," options dialog title ", JOptionPane.YES_OPTION

JOptionPane.QUESTION_MESSAGE, null, options, options [0])

If (response==0)

{

This.setTitle ("you pressed the OK button")

}

Else if (response==1)

{

This.setTitle ("you pressed the cancel button")

}

Else if (response==2)

{

This.setTitle ("you pressed the help button")

}

(4) display InputDialog to allow users to enter

String inputValue = JOptionPane.showInputDialog ("Please input a value")

(5) display the InputDialog to allow the user to enter selectively

Object [] possibleValues = {"First", "Second", "Third"}

/ / user's selected items

Object selectedValue = JOptionPane.showInputDialog (null, "Choose one"

"Input", JOptionPane.INFORMATION_MESSAGE, null, possibleValues, possibleValues [0])

SetTitle ("you pressed" + (String) selectedValue+ "Project")

Thank you for reading this article carefully. I hope the article "what is the use of JOptionPane in Swing" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report