Message Dialog trong Java Swing

Đặc điểm cơ bản của Message Dialog :

– Hiển thị thông điệp dạng dialog

Code minh họa sử dụng Message Dialog :

package quyetdv.javaswing.components;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
public class MessageDialogDemo extends JFrame {
    public MessageDialogDemo() {
        setSize(250, 100);
        setVisible(true);
        setLocation(500, 300);
        setResizable(false);
        setTitle("JButton Demo");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // Create label
        JLabel label = new JLabel("A JLabel");
        add(label);
        // Create button
        JButton button = new JButton("Click me");
        add(button, "North", 1);
        // add ActionListener
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                JOptionPane.showMessageDialog(null, "This is a messag dialog",
                  "Title", JOptionPane.WARNING_MESSAGE);
            }
       });
    }
    public static void main(String[] args) {
        MessageDialogDemo messageDialog = new MessageDialogDemo();
    }
}

Kết quả : Khi click vào nút Button sẽ hiển thị hộp thoại thông báo sau

MessageDialogDemo

Share this:

Like this:

Like

Loading…