Java Swing – JOptionPane showInputDialogの例
これは、JOptionPaneクラスのshowInputDialog()メソッドのレビューです。 このメソッドを使用すると、ダイアログウィンドウのカスタマイズ中にユーザーに入力を求めることができます。 showConfirmDialogは、StringまたはObjectのいずれかを返し、次のパラメーターの組み合わせを使用して呼び出すことができます。
Object (returns String) –ユーザーからの入力を要求する質問メッセージダイアログを表示します。
Object, Object (returns String) –入力値が初期化された状態でユーザーからの入力を要求する質問メッセージダイアログを表示します。
Component, Object (returns String) –ユーザーからの入力を要求する質問メッセージダイアログを表示します。 入力を文字列として返します。 コンポーネントは、ダイアログが表示されるフレームを決定します。 nullの場合、またはparentComponentにフレームがない場合、デフォルトのフレームが使用されます。
Component, Object, Object (returns String) –上記と同じ。 唯一の違いは、入力フィールドに最後のObjectパラメーターを介して設定された初期値があることです。
Component, Object, String, int (returns String) –ユーザーからの入力を要求するダイアログを表示します。 ダイアログには、Stringパラメーターで設定されたタイトルと、intパラメーターで設定されたMessageTypeがあります。 JOptionPaneのさまざまなMessageTypeは次のとおりです。
-
エラーメッセージ
-
INFORMATION_MESSAGE
-
WARNING_MESSAGE
-
QUESTION_MESSAGE
-
PLAIN_MESSAGE
Component, Object, String, int, Icon, Object[], Object (returns Object) –初期選択、可能な選択、および他のすべてのオプションを指定できるブロッキングダイアログへの入力をユーザーに求めます。 アイコン(nullでない場合)はダイアログ内に表示され、デフォルトのMessageTypeアイコンをオーバーライドします。
1. オブジェクト–The simplest way to get user input
ユーザー入力をStringとして取得する質問メッセージダイアログの例:
SimpleInputDialog1.java
package com.example.inputDialog;
import javax.swing.JOptionPane;
public class SimpleInputDialog1 {
public static void main(String[] args){
String m = JOptionPane.showInputDialog("Anyone there?");
System.out.println(m);
}
}
出力:

入力フィールドに「Hellooooo !!!!」と入力して「OK」をクリックすると
Hellooooo!!!!
2. オブジェクトとオブジェクト–Setting an initial value over the input
ユーザー入力をStringとして取得する初期値を持つ質問メッセージダイアログの例:
SimpleInputDialog2.java
package com.example.inputDialog;
import javax.swing.JOptionPane;
public class SimpleInputDialog2 {
public static void main(String[] args){
String m = JOptionPane.showInputDialog("Anyone there?", 42);
System.out.println(m);
}
}
出力:

3. コンポーネントとオブジェクト–Setting the dialog in a parent Component
Componentをnullに設定すると、結果は番号1と同じになります。 この例では、ダイアログを配置するためのJFrameを作成します。 ユーザーがフィールドに何かを入力しない限り、フレームは閉じます。
InputDialogInFrame.java
package com.example.inputDialog;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class InputDialogInFrame extends JFrame{
public InputDialogInFrame() {
getContentPane().setBackground(Color.DARK_GRAY);
setTitle("Input Dialog in Frame");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setResizable(false);
setSize(400, 300);
getContentPane().setLayout(null);
}
private void closeIt(){
this.getContentPane().setVisible(false);
this.dispose();
}
public static void main(String[] args){
InputDialogInFrame frame = new InputDialogInFrame();
String m = JOptionPane.showInputDialog(frame, "Anyone there?");
if(m.isEmpty()){
frame.closeIt();
}
}
}
出力:

4. コンポーネント、オブジェクト、オブジェクト–Setting the dialog in a parent Component with an initial value
Componentをnullに設定すると、結果は番号2と同じになります。 この例では、コードを番号3からわずかに変更します。
InputDialogInFrame.java
package com.example.inputDialog;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class InputDialogInFrame extends JFrame{
public InputDialogInFrame() {
getContentPane().setBackground(Color.DARK_GRAY);
setTitle("Input Dialog in Frame");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setResizable(false);
setSize(400, 300);
getContentPane().setLayout(null);
}
private void closeIt(){
this.getContentPane().setVisible(false);
this.dispose();
}
public static void main(String[] args){
InputDialogInFrame frame = new InputDialogInFrame();
String m = JOptionPane.showInputDialog(frame, "Anyone there?", 42);
if(m.isEmpty() || m.equals("42")){
frame.closeIt();
}
}
}
出力:

5. コンポーネント、オブジェクト、文字列、整数–Let us give our input dialog a title and choose the MessageType
JOptionPane.INFORMATION_MESSAGEまでのデフォルトアイコンを使用した情報メッセージの例:
SimpleInputDialog5.java
package com.example.inputDialog;
import javax.swing.JOptionPane;
public class SimpleInputDialog5 {
public static void main(String[] args){
String m = JOptionPane.showInputDialog(null, "Broccoli is tasty!",
"Green dinner", JOptionPane.INFORMATION_MESSAGE);
System.out.println(m);
}
}
出力:

6. コンポーネント、オブジェクト、文字列、int、アイコン、オブジェクト[]およびオブジェクト–Input dialog with predefined options
6.1 In this example we provide the user with a set of options to choose from. さまざまなオプションが、選択した初期値とともにドロップダウンメニューの形式で表示されます。
InputDialog6a.java
package com.example.inputDialog;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
public class InputDialog6a {
public static void main(String[] args) {
String[] options = {"I adore turtles", "Yes", "Maybe", "Urm...", "No", "Hate them"};
ImageIcon icon = new ImageIcon("src/images/turtle32.png");
String n = (String)JOptionPane.showInputDialog(null, "Do you like turtles??",
"I like turtles", JOptionPane.QUESTION_MESSAGE, icon, options, options[2]);
System.out.println(n);
}
}
出力:

「カメが大好き」というオプションを選択し、「OK」をクリックすると
I adore turtles
6.2 An example using array of Integers:
InputDialog6b.java
package com.example.inputDialog;
import javax.swing.JOptionPane;
public class InputDialog6b {
public static void main(String[] args) {
Integer[] options = {2, 3, 5, 7, 9, 11};
int n = (Integer)JOptionPane.showInputDialog(null, "Pick a number that is not prime:",
"Prime numbers", JOptionPane.QUESTION_MESSAGE, null, options, options[2]);
System.out.println(n);
}
}
出力:

6.3 An example where we take advantage of the dynamics the Object array and the Object return type of the method:
InputDialog6c.java
package com.example.inputDialog;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
public class InputDialog6c {
public static void main(String[] args) {
Object[] options = {2, "No", 5.6, true};
Object l = JOptionPane.showInputDialog(null, "Just pick something already!",
"Mix & Match", JOptionPane.ERROR_MESSAGE, null, options, options[0]);
if(l instanceof Integer){
System.out.println("You picked an Integer!");
}else if(l instanceof String){
System.out.println("You picked a String!");
}else if(l instanceof Double){
System.out.println("You picked a Double!");
}else if(l instanceof Boolean){
System.out.println("You picked a Boolean!");
}
}
}
出力:

「2」が選択され、ユーザーが「OK」をクリックすると:
You picked an Integer!
「いいえ」が選択され、ユーザーが「OK」をクリックした場合:
You picked a String!
「5.6」が選択され、ユーザーが「OK」をクリックした場合:
You picked a Double!
「true」が選択され、ユーザーが「OK」をクリックした場合:
You picked a Boolean!
7. より高度な例
これまでのすべての例では、Objectの代わりにStringが使用されていました。この例では、Objectの代わりにJPanelを使用します。 JPanelはカスタマイズされており、JLabelが追加されています。 また、UIManagerの呼び出しを使用して、OptionPaneのサイズを操作しています。
InputDialogPanel.java
package com.example.inputDialog;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.SwingConstants;
public class InputDialogPanel {
public static void main(String[] args) {
JPanel panel = new JPanel();
panel.setBackground(new Color(0, 0, 0));
panel.setSize(new Dimension(250, 32));
panel.setLayout(null);
JLabel label = new JLabel("It's your choice! :)");
label.setForeground(new Color(255, 255, 0));
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setFont(new Font("Arial", Font.BOLD, 11));
label.setBounds(0, 0, 250, 32);
panel.add(label);
UIManager.put("OptionPane.minimumSize",new Dimension(270, 120));
Object[] options = {2, "No", 5.6, true};
Object l = JOptionPane.showInputDialog(null, panel,
"Mix & Match", JOptionPane.PLAIN_MESSAGE, null, options, options[3]);
if(l instanceof Integer){
System.out.println("You picked an Integer!");
}else if(l instanceof String){
System.out.println("You picked a String!");
}else if(l instanceof Double){
System.out.println("You picked a Double!");
}else if(l instanceof Boolean){
System.out.println("You picked a Boolean!");
}
}
}
出力:
