Dialog trong Java AWT – VietTuts

Dialog (hộp thoại) đại diện cho một cửa sổ cấp cao nhất với một đường viền và một tiêu đề được sử dụng để tạo hộp thoại lấy thông tin từ người dùng. Nó kế thừa lớp Window.

Khai báo lớp AWT Dialog

public class Dialog extends Window

Ví dụ Dialog trong Java AWT

package vn.viettuts.awt;

import java.awt.Button;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class DialogExample {
    private static Dialog dialog;

    public DialogExample() {
        Frame frame = new Frame();
        dialog = new Dialog(frame, "Ví dụ Dialog trong Java AWT", true);
        dialog.setLayout(new FlowLayout());
        Button button = new Button("Close");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                DialogExample.dialog.setVisible(false);
            }
        });
        dialog.add(new Label("Click button to close dialog."));
        dialog.add(button);
        dialog.setSize(300, 300);
        dialog.setVisible(true);
    }

    public static void main(String args[]) {
        new DialogExample();
    }
}

Kết quả:

Ví dụ Dialog trong Java AWT