Home > Java Swing > Confirm Dialog trong Java Swing

Confirm Dialog trong Java Swing

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

– Hiển thị hộp thoại xác nhận

Code minh họa sử dụng Confirm 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 ConfirmDialogDemo extends JFrame {
    public ConfirmDialogDemo() {
        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) {
                 int click = JOptionPane.showConfirmDialog(null, "This is a confirm dialog");
                 if (click==JOptionPane.YES_OPTION) {
                      JOptionPane.showMessageDialog(null, "Click Yes");
                 }
                 if (click==JOptionPane.NO_OPTION) {
                      JOptionPane.showMessageDialog(null, "Click No");
                 }
                 if (click==JOptionPane.CANCEL_OPTION) {
                      JOptionPane.showMessageDialog(null, "Click Cancel");
                 }
                 if (click==JOptionPane.CLOSED_OPTION) {
                      JOptionPane.showMessageDialog(null, "Click Close");
                 }
             }
        });
    }
    public static void main(String[] args) {
        ConfirmDialogDemo confirmDialog = new ConfirmDialogDemo();
    }
}

Kết quả : Khi click vào nút Button sẽ hiển thị hộp thoại Confirm như hình sau

ConfirmDialogDemo

Advertisements
Categories: Java Swing
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: