Lớp JOptionPane trong Java Swing



Lớp JOptionPane là một thành phần cung cấp các phương thức chuẩn để popup một hộp thoại dialog chuẩn cho một giá trị hoặc thông báo người dùng về một cái gì đó. Dưới đây là cú pháp khai báo của lớp javax.swing.JOptionPane:

public class JOptionPane
   extends JComponent
      implements Accessible

Lớp này kế thừa các phương thức từ các lớp sau:

  • javax.swing.JComponent

  • java.awt.Container

  • java.awt.Component

  • java.lang.Object

Các constructor của lớp JOptionPane trong Java Swing

1. JOptionPane(): Tạo một JOptionPane với một thông điệp kiểm tra (test message).

2. JOptionPane(Object message): Tạo một instance của JOptionPane để hiển thị một message bởi sử dụng kiểu thông điệp thuần message và các tùy chọn option mặc định được phân phối bởi UI.

3. JOptionPane(Object message, int messageType): Tạo một instance của JOptionPane để hiển thị một thông điệp với kiểu thông điệp đã cho và các tùy chọn mặc định.

4. JOptionPane(Object message, int messageType, int optionType): Tạo một instance của JOptionPane để hiển thị một thông điệp với kiểu thông điệp đã cho và các tùy chọn mặc định.

5. JOptionPane(Object message, int messageType, int optionType, Icon icon): Tạo một instance của JOptionPane để hiển thị một thông điệp với kiểu thông điệp, tùy chọn và icon đã cho.

6. JOptionPane(Object message, int messageType, int optionType, Icon icon, Object[] options): Tạo một instance của JOptionPane để hiển thị một thông điệp với kiểu thông điệp, tùy chọn và icon đã cho.

7. JOptionPane(Object message, int messageType, int optionType, Icon icon, Object[] options, Object initialValue): Tạo một instance của JOptionPane để hiển thị một thông điệp với kiểu thông điệp, tùy chọn và icon đã cho với giá trị tùy chọn được lựa chọn ban đầu đã được xác định.

Quảng cáo

Các phương thức được sử dụng phổ biến của lớp JOptionPane

STT Phương thức & Miêu tả
1 void selectInitialValue()

Yêu cầu giá trị khởi tạo ban đầu để được lựa chọn, mà sẽ thiết lập focus tới giá trị đó

2 void setIcon(Icon newIcon)

Thiết lập icon để hiển thị

3 void setInitialSelectionValue(Object newValue)

Thiết lập giá trị input mà được hiển thị ban đầu như là selected tới người dùng

4 void setInitialValue(Object newInitialValue)

Thiết lập giá trị ban đầu để được kích hoạt. Đây là thành phần mà có focus khi pane được hiển thị ban đầu

5 void setInputValue(Object newValue)

Thiết lập giá trị input mà là selected hoặc input bởi người dùng

6 void setMessage(Object newMessage)

Thiết lập đối tượng message của option pane

7 void setMessageType(int newType)

Thiết lập kiểu thông điệp của option pane

8 void setOptions(Object[] newOptions)

Thiết lập các tùy chọn mà pane này hiển thị

9 void setOptionType(int newType)

Thiết lập các tùy chọn để hiển thị

10 static voidsetRootFrame(Frame newRootFrame)

Thiết lập frame để sử dụng cho các phương thức lớp mà chưa được cung cấp frame nào

11 void setSelectionValues(Object[] newValues)

Thiết lập các giá trị selection cho một pane mà cung cấp cho người dùng một danh sách item để lựa chọn từ đó

12 void setUI(OptionPaneUI ui)

Thiết lập đối tượng UI mà triển khai L&F cho thành phần này

13 void setValue(Object newValue)

Thiết lập giá trị mà người dùng đã lựa chọn

14 void setWantsInput(boolean newValue)

Thiết lập thuộc tính wantsInput

15 static int showConfirmDialog(Component parentComponent, Object message)

Hiển thị một hộp thoại với các tùy chọn Yes, No và Cancel với title là Select an Option

16 static int showConfirmDialog(Component parentComponent, Object message, String title, int optionType)

Hiển thị một hộp thoại, với số tùy chọn được xác định bởi tham số optionType

17 static int showConfirmDialog(Component parentComponent, Object message, String title, int optionType, int messageType)

Hiển thị một hộp thoại, với số tùy chọn được xác định bởi tham số optionType, và tham số messageType xác định icon để hiển thị

18 static int showConfirmDialog(Component parentComponent, Object message, String title, int optionType, int messageType, Icon icon)

Hiển thị một hộp thoại với icon đã cho, với số tùy chọn được xác định bởi tham số optionType

19 static String showInputDialog(Component parentComponent, Object message, Object initialSelectionValue)

Hiển thị một hộp thoại dạng question-message yêu cầu input từ người dùng được tạo ra từ parentComponent

20 static String showInputDialog(Component parentComponent, Object message, String title, int essageType)

Hiển thị một hộp thoại dạng question-message yêu cầu input từ người dùng được tạo ra từ parentComponent với hộp thoại có title và messageType

21 static Object showInternalInputDialog(Component parentComponent, Object message, String title, int messageType, Icon icon, Object[] selectionValues, Object initialSelectionValue)

Gợi ý người dùng nhập input trong một hộp thoại nội tại, ở đây sự lựa chọn ban đầu, sự lựa chọn có thể có, và tất cả tùy chọn khác có thể được xác định

22 JDialog createDialog(Component parentComponent, String title)

Tạo và trả về một JDialog mới mà bao quanh optionpane này được căn chỉnh vào giữa parentComponent trong frame của parentComponent

23 JDialog createDialog(String title)

Tạo và trả về một JDialog mới (không phải là cha) với title đã cho

24 JInternalFrame createInternalFrame(Component parentComponent, String title)

Tạo và trả về một instance của JInternalFrame

Quảng cáo

Chương trình ví dụ lớp JOptionPane

package com.vietjack.gui;
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
public class SwingControlDemo {
    
   private JFrame mainFrame;
   private JLabel headerLabel;
   private JLabel statusLabel;
   private JPanel controlPanel;

   public SwingControlDemo(){
      prepareGUI();
   }

   public static void main(String[] args){
      SwingControlDemo  swingControlDemo = new SwingControlDemo();      
      swingControlDemo.showDialogDemo();
   }

   private void prepareGUI(){
      mainFrame = new JFrame("Vi du Java Swing");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      });    
      headerLabel = new JLabel("", JLabel.CENTER);        
      statusLabel = new JLabel("",JLabel.CENTER);    

      statusLabel.setSize(350,100);

      controlPanel = new JPanel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);  
   }

   private void showDialogDemo(){                                       
      headerLabel.setText("Control in action: JOptionPane"); 

      JButton okButton = new JButton("OK");        
      JButton javaButton = new JButton("Yes/No");
      JButton cancelButton = new JButton("Yes/No/Cancel");

      okButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(
            mainFrame, "Welcome to TutorialsPoint.com");
         }          
      });

      javaButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            int output = JOptionPane.showConfirmDialog(mainFrame
               , "Click any button"
               ,"VietJack.com"
               ,JOptionPane.YES_NO_OPTION);

            if(output == JOptionPane.YES_OPTION){
               statusLabel.setText("Yes selected.");
            }else if(output == JOptionPane.NO_OPTION){
               statusLabel.setText("No selected.");
            }
         }
      });

      cancelButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {                
            int output = JOptionPane.showConfirmDialog(mainFrame
               , "Click any button"
               ,"VietJack.com"
               ,JOptionPane.YES_NO_CANCEL_OPTION,
               JOptionPane.INFORMATION_MESSAGE);

            if(output == JOptionPane.YES_OPTION){
               statusLabel.setText("Yes selected.");
            }else if(output == JOptionPane.NO_OPTION){
               statusLabel.setText("No selected.");
            }else if(output == JOptionPane.CANCEL_OPTION){
               statusLabel.setText("Cancel selected.");
            }
         }
      });

      controlPanel.add(okButton);
      controlPanel.add(javaButton);
      controlPanel.add(cancelButton);       
      mainFrame.setVisible(true);  
   }
}

Đã có app VietJack trên điện thoại, giải bài tập SGK, SBT Soạn văn, Văn mẫu, Thi online, Bài giảng....miễn phí. Tải ngay ứng dụng trên Android và iOS.

Theo dõi chúng tôi miễn phí trên mạng xã hội facebook và youtube:

Các bạn có thể mua thêm khóa học JAVA CORE ONLINE VÀ ỨNG DỤNG cực hay, giúp các bạn vượt qua các dự án trên trường và đi thực tập Java. Khóa học có giá chỉ 300K, nhằm ưu đãi, tạo điều kiện cho sinh viên cho thể mua khóa học.

Nội dung khóa học gồm 16 chuơng và 100 video cực hay, học trực tiếp tại https://www.udemy.com/tu-tin-di-lam-voi-kien-thuc-ve-java-core-toan-tap/ Bạn nào có nhu cầu mua, inbox trực tiếp a Tuyền, cựu sinh viên Bách Khoa K53, fb: https://www.facebook.com/tuyen.vietjack

Follow facebook cá nhân Nguyễn Thanh Tuyền https://www.facebook.com/tuyen.vietjack để tiếp tục theo dõi các loạt bài mới nhất về Java,C,C++,Javascript,HTML,Python,Database,Mobile.... mới nhất của chúng tôi.


swing_control_trong_java_swing.jsp


Tài liệu giáo viên