Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Java

Các loại hộp thoại JOptionPane trong Java là gì?

JOptionPane là một lớp con của JComponent cung cấp các phương thức tĩnh để tạo và tùy chỉnh các hộp thoại modal với mã nguồn đơn giản. Thay vì sử dụng JDialog, JOptionPane giúp giảm thiểu độ phức tạp của code. Lớp này hiển thị hộp thoại với bốn biểu tượng chuẩn (câu hỏi, thông tin, cảnh báo, lỗi) hoặc biểu tượng tùy chỉnh do người dùng chỉ định.

Bốn loại hộp thoại chính

  • MessageDialog – Hộp thoại hiển thị thông báo, cho phép thêm biểu tượng để cảnh báo người dùng.
  • ConfirmDialog – Hộp thoại hiển thị thông báo và cho phép người dùng trả lời câu hỏi (Yes/No/Cancel).
  • InputDialog – Hộp thoại hiển thị thông báo và cho phép người dùng nhập văn bản.
  • OptionDialog – Hộp thoại tổng hợp bao gồm ba loại trên, cho phép tùy chọn hoàn toàn các nút bấm.

Ví dụ minh họa

import javax.swing.*; 
public class JoptionPaneTest { 
    public static void main(String[] args) { 
        JFrame frame = new JFrame("JoptionPane Test"); 
        frame.setSize(200, 200); 
        frame.setLocationRelativeTo(null); 
        frame.setVisible(true); 
        
        // MessageDialog đơn giản
        JOptionPane.showMessageDialog(frame, "Hello Java"); 
        
        // MessageDialog có tiêu đề và biểu tượng cảnh báo
        JOptionPane.showMessageDialog(frame, 
            "You have less amount, please recharge", 
            "Apocalyptic message", 
            JOptionPane.WARNING_MESSAGE); 
        
        // ConfirmDialog
        int result = JOptionPane.showConfirmDialog(null, "Do you want to remove item now?"); 
        switch (result) { 
            case JOptionPane.YES_OPTION: 
                System.out.println("Yes"); 
                break; 
            case JOptionPane.NO_OPTION: 
                System.out.println("No"); 
                break; 
            case JOptionPane.CANCEL_OPTION: 
                System.out.println("Cancel"); 
                break; 
            case JOptionPane.CLOSED_OPTION: 
                System.out.println("Closed"); 
                break; 
        } 
        
        // InputDialog
        String name = JOptionPane.showInputDialog(null, "Please enter your name."); 
        System.out.println(name); 
        
        // OptionDialog tùy chỉnh với nhiều thành phần
        JTextField userField = new JTextField(); 
        JPasswordField passField = new JPasswordField(); 
        String message = "Please enter your user name and password."; 
        result = JOptionPane.showOptionDialog(frame, 
            new Object[] {message, userField, passField}, 
            "Login", 
            JOptionPane.OK_CANCEL_OPTION, 
            JOptionPane.QUESTION_MESSAGE, 
            null, null, null); 
        
        if (result == JOptionPane.OK_OPTION) 
            System.out.println(userField.getText() + " " + new String(passField.getPassword())); 
        
        System.exit(0); 
    } 
}

Kết quả đầu ra

Chương trình sẽ hiển thị lần lượt: hộp thoại chào mừng, hộp thoại cảnh báo, hộp thoại xác nhận xóa, hộp thoại nhập tên, và hộp thoại đăng nhập tùy chỉnh.