15 April 2016

Swing: The dialog boxes (2) Hiển thi thông báo dialog trong java swing GUI

Swing: The dialog boxes (2)

The basics of dialogues

Display Message

You use the method showMessageDialog class JOptionPane .
This method has several signatures.

example 1

JOptionPane.showMessageDialog (mainFrame, "message dialog");

example 2

JOptionPane.showMessageDialog (mainFrame, 
                               "error", 
                               "title", 
                               JOptionPane.ERROR_MESSAGE);
view source

Boxes confirmation

Method showConfirmDialog class JOptionPane

example 1

int answer = JOptionPane.showConfirmDialog (mainFrame, "message");

example 2

  
int answer = JOptionPane.showConfirmDialog (mainFrame, 
                                             "message", 
                                             "title", 
                                             JOptionPane.YES_NO_OPTION);
view source

Transmission input

Method showInputDialog class JOptionPane .
This class allows you to enter an option in edit fields or select from a list provided.

example 1

String input = JOptionPane.showInputDialog (mainFrame, "message");
Note: If the user clicks the button Cancel , the method return an empty string regardless seizure

example 2

String [] = {days "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}; 
String input = (String) JOptionPane.showInputDialog (mainFrame, 
                                                       "choose a day ", 
                                                       " title ", 
                                                       JOptionPane.QUESTION_MESSAGE, 
                                                       null, 
                                                       days, days [0]);  
  
view source

Button selection box

Method showOptionDialog class JOptionPane

Example

String [] = {days "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};   
int choice = JOptionPane.showOptionDialog (mainFrame, 
                                          "Choose a day" 
                                          "Title", 
                                          JOptionPane.OK_CANCEL_OPTION, 
                                          JOptionPane.QUESTION_MESSAGE, 
                                          null 
                                          day, 
                                          "Saturday");
  
Remaque: If the user closes the dialog without clicking a button, the reference method -1.

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang