Java Swing - JOptionPane showConfirmDialogの例

Java Swing – JOptionPane showConfirmDialogの例

これは、JOptionPaneクラスのshowConfirmDialog()メソッドのレビューです。 このメソッドは、yes / no / cancelなどの確認の質問をすることで、ユーザー入力をすばやく簡単に取得する方法です。 showConfirmDialog()は、次のパラメータの組み合わせを使用して呼び出すことができます。

Component, Object
Component, Object, String, int
Component, Object, String, int, int
Component, Object, String, int, int, Icon
  1. コンポーネント–最初のパラメーターは、ダイアログが表示されるフレームを決定するコンポーネントです。 nullの場合、またはparentComponentにフレームがない場合は、デフォルトのフレームが使用されます。

  2. オブジェクト-2番目のパラメーターは任意のオブジェクトにすることができます。 (In some older versions of Java you might get a compiler error when using primitive types directly)

  3. 文字列– 3番目のパラメーターは、confirmDialogウィンドウのタイトルとして配置される文字列です。

  4. int –文字列に続くintはOptionTypeです。 JOptionPaneの異なるOptionTypesは、次のとおりです。

    • DEFAULT_OPTION

    • YES_NO_OPTION

    • YES_NO_CANCEL_OPTION

    • OK_CANCEL_OPTION

  5. int –次のintはMessageTypeです。 JOptionPaneの異なるMessageTypesは、次のとおりです。

    • エラーメッセージ

    • INFORMATION_MESSAGE

    • WARNING_MESSAGE

    • QUESTION_MESSAGE

    • PLAIN_MESSAGE

  6. アイコン–最後のパラメータはIconで、ダイアログ内に表示され、デフォルトのMessageTypeアイコンを上書きします。

1. コンポーネントとオブジェクト

ユーザー入力を取得する最も簡単な方法。 showConfirmDialog()は、[はい]、[いいえ]、[キャンセル]オプションと、「オプションの選択」というタイトルのダイアログを表示します。

ConfirmDialog1.java

package com.techfou.confirmDialog;

import javax.swing.JOptionPane;

public class ConfirmDialog1 {

    public static void main(String[] args) {

        int input = JOptionPane.showConfirmDialog(null, "Do you like bacon?");
        // 0=yes, 1=no, 2=cancel
        System.out.println(input);

    }
}

出力:

swing-comfirm-dialog-2a

2. コンポーネント、オブジェクト、文字列、int

確認ダイアログにさらに情報を追加します。 この例では、ダイアログのタイトルとoptionTypeを選択できます。 DEFAULT_OPTIONには「OK」ボタンしかありません。 この形式の確認ダイアログは、単純なshowMessageDialog()と同等ですが、ユーザー入力を取得する機能を提供します。

ConfirmDialog2a.java

package com.techfou.confirmDialog;

import javax.swing.JOptionPane;

public class ConfirmDialog2a {

    public static void main(String[] args) {

        int input = JOptionPane.showConfirmDialog(null,
                "Click ok if you are ok", "Be ok!", JOptionPane.DEFAULT_OPTION);
        // 0=ok
        System.out.println(input);

    }
}

出力:

swing-comfirm-dialog-2b

YES_NO_CANCEL_OPTIONを使用した別の簡単な例:

ConfirmDialog2b.java

package com.example.confirmDialog;

import javax.swing.JOptionPane;

public class ConfirmDialog2b {

    public static void main(String[] args) {

        int input = JOptionPane.showConfirmDialog(null,
                "Do you want to proceed?", "Select an Option...",JOptionPane.YES_NO_CANCEL_OPTION);

    // 0=yes, 1=no, 2=cancel
    System.out.println(input);

    }
}

出力:

swing-comfirm-dialog-2c

3. コンポーネント、オブジェクト、文字列、int&int

エラーアイコンのある確認ダイアログを表示します。

ConfirmDialog3.java

package com.example.confirmDialog;

import javax.swing.JOptionPane;

public class ConfirmDialog3 {

    public static void main(String[] args) {

        int input = JOptionPane.showConfirmDialog(null, "Do you want to proceed?", "Select an Option...",
                JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.ERROR_MESSAGE);

    // 0=yes, 1=no, 2=cancel
    System.out.println(input);

    }
}

出力:

swing-comfirm-dialog-2d

4. コンポーネント、オブジェクト、文字列、int、int&アイコン

ディレクトリから取得したIconを使用して、確認ダイアログを「よりきれいな」例にします。

ConfirmDialog4a.java

package com.example.confirmDialog;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

public class ConfirmDialog4a {

    public static void main(String[] args) {

        ImageIcon icon = new ImageIcon("src/images/turtle64.png");
    int input = JOptionPane.showConfirmDialog(null, "Do you like turtles?", "Be honest...",
            JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, icon);

        // 0=yes, 1=no, 2=cancel
    System.out.println(input);

    }
}

出力:

swing-comfirm-dialog-2e

Componentframeに設定した例:

ConfirmDialogInFrame.java

package com.example.confirmDialog;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.awt.Color;

public class ConfirmDialogInFrame extends JFrame{

    public ConfirmDialogInFrame() {
        getContentPane().setBackground(Color.DARK_GRAY);
        setTitle("Confirm Dialog in Frame");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        setResizable(false);
        setSize(400, 300);
        getContentPane().setLayout(null);
    }

    public static void main(String[] args){
        ImageIcon icon = new ImageIcon("src/images/turtle64.png");
        int input = JOptionPane.showConfirmDialog(new ConfirmDialogInFrame(),
                "I appear as part of the frame!!", "Customized Dialog",
                JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE, icon);

        // 0=ok, 2=cancel
        System.out.println(input);
    }
}

出力:

swing-comfirm-dialog-2f

5. より高度な例

この例では、JPanelをObjectパラメーターとして渡します。 JPanelはカスタマイズされており、JLabelが追加されています。 また、UIManagerの呼び出しを使用して、OptionPaneのサイズを操作しています。

ConfirmDialogPanel.java

package com.example.messageDialog;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;

public class ConfirmDialogPanel {

    public static void main(String[] args) {

        ImageIcon icon = new ImageIcon("src/images/lock64.png");

        JPanel panel = new JPanel();
        panel.setBackground(new Color(102, 205, 170));
        panel.setSize(new Dimension(200, 64));
        panel.setLayout(null);

        JLabel label1 = new JLabel("This file requires administrator rights.");
        label1.setVerticalAlignment(SwingConstants.BOTTOM);
        label1.setBounds(0, 0, 200, 32);
        label1.setFont(new Font("Arial", Font.BOLD, 10));
        label1.setHorizontalAlignment(SwingConstants.CENTER);
        panel.add(label1);

        JLabel label2 = new JLabel("Are you sure you want to continue?");
        label2.setVerticalAlignment(SwingConstants.TOP);
        label2.setHorizontalAlignment(SwingConstants.CENTER);
        label2.setFont(new Font("Arial", Font.BOLD, 10));
        label2.setBounds(0, 32, 200, 32);
        panel.add(label2);

        UIManager.put("OptionPane.minimumSize", new Dimension(300, 120));
        int input = JOptionPane.showConfirmDialog(null, panel, "Admin Rights Confirmation",
                JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, icon);

        // 0=yes, 1=no, 2=cancel
        System.out.println(input);

    }

}

出力:

swing-comfirm-dialog-2g