開発者ドキュメント

Java Swing – JOptionPane showConfirmDialogの例

これは

JOptionPane`クラスの

showConfirmDialog() `メソッドのレビューです。この方法は、はい/いいえ/取り消しのような確認の質問をして、ユーザーの入力を得るためのすばやく簡単な方法です。 `showConfirmDialog()`は、以下のパラメータの組み合わせを使用して呼び出すことができます:

Component, Object
Component, Object, String, int
Component, Object, String, int, int
Component, Object, String, int, int, Icon
  1. コンポーネント – 最初のパラメータは、コンポーネントを決定するComponentです.

ダイアログが表示されるフレーム。 nullの場合、または「parentComponent」にFrameがない場合は、デフォルトのFrameが使用されます。

  1. Object – 2番目のパラメータは任意のObjectです. __(一部の年齢

Javaのバージョンでプリミティブを使用するとコンパイルエラーが発生することがあります
直接タイプ)__
。 String – 3番目のパラメータは、文字列のタイトルとして配置されます。

確認ダイアログウィンドウ。

  1. int – Stringの後に続くintは `OptionType`です. ザ


JOptionPane`のための異なる

OptionTypes`は以下の通りです:

`JOptionPane`の場合、次のようになります:

  • エラーメッセージ

  • INFORMATION__MESSAGE

  • WARNING__MESSAGE

  • QUESTION__MESSAGE

  • PLAIN__MESSAGE

    1. Icon – 最後のパラメータは、アイコンの中に表示される `Icon`です.

デフォルトの `MessageType`アイコンを上書きします。

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

ユーザーの入力を得るための最も簡単な方法。 `showConfirmDialog()`は、Yes、No、Cancelの各オプションとタイトルを持つダイアログを表示します。

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);

    }
}

出力:



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

確認ダイアログに情報を追加する。この例では、ダイアログのタイトルと `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);

    }
}

出力:



YES

NO

CANCEL__OPTIONを使用する別の簡単な例:

ConfirmDialog2b.java

package com.mkyong.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);

    }
}

出力:



3. Component、Object、String、int&int

エラーアイコン付きの確認ダイアログを表示:

ConfirmDialog3.java

package com.mkyong.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);

    }
}

出力:



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

あなたの確認ダイアログを “もっときれいにする”ディレクトリから取得した `Icon`を使った例:

ConfirmDialog4a.java

package com.mkyong.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);

    }
}

出力:




Component`が

frame`に設定された例:

ConfirmDialogInFrame.java

package com.mkyong.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);
    }
}

出力:



5.より高度な例

この例では、Objectパラメータとして

JPanel`を渡しています。 `JPanel`はカスタマイズされ、

JLabel`が追加されています。また、

UIManager`の呼び出しを使って

OptionPane`のサイズを操作しています。

ConfirmDialogPanel.java

package com.mkyong.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);

    }

}

出力:



モバイルバージョンを終了