Java Swing – JOptionPane showMessageDialogの例
これは、JOptionPaneクラスのshowMessageDialog()メソッドのレビューです。 このメソッドは、発生したことをユーザーにすばやく簡単に伝える方法です。 showMessageDialog()は、次のパラメータの組み合わせを使用して呼び出すことができます。
Component, Object Component, Object, String, int Component, Object, String, int, Icon
-
コンポーネント–最初のパラメーターは、ダイアログが表示されるフレームを決定するコンポーネントです。 nullの場合、または
parentComponentにフレームがない場合は、デフォルトのフレームが使用されます。 -
オブジェクト-2番目のパラメーターは任意のオブジェクトにすることができます。 (In some older versions of Java you might get a compiler error when using primitive types directly)。
-
文字列– 3番目のパラメータは、メッセージダイアログウィンドウのタイトルとして配置される文字列です。
-
int –文字列に続くintは
MessageTypeです。JOptionPaneの異なるMessageTypesは、次のとおりです。-
エラーメッセージ
-
INFORMATION_MESSAGE
-
WARNING_MESSAGE
-
QUESTION_MESSAGE
-
PLAIN_MESSAGE
-
-
アイコン–最後のパラメータは
Iconで、ダイアログ内に表示され、デフォルトのMessageTypeアイコンを上書きします。
1. コンポーネントとオブジェクト
メッセージダイアログを使用する最も簡単な方法。 Componentをnullに設定し、2番目の引数としてStringを使用した例:
SimpleDialog1.java
package com.techfou.messageDialog;
import javax.swing.JOptionPane;
public class SimpleDialog1 {
public static void main(String[] args){
JOptionPane.showMessageDialog(null, "Simple Information Message");
}
}
出力:

2. コンポーネント、オブジェクト、文字列、int
メッセージダイアログにさらに情報を追加します。 Componentをnullに設定し、2番目のパラメーターとしてdoubleを使用した例:
SimpleDialog2a.java
package com.techfou.messageDialog;
import javax.swing.JOptionPane;
public class SimpleDialog2a {
public static void main(String[] args){
JOptionPane.showMessageDialog(null, 8.9, "This is not an integer.", JOptionPane.PLAIN_MESSAGE);
}
}
出力:

エラーメッセージの例(Componentをnullに設定、String Object):
SimpleDialog2b.java
package com.example.messageDialog;
import javax.swing.JOptionPane;
public class SimpleDialog2b {
public static void main(String[] args){
JOptionPane.showMessageDialog(null, "Uh-oh!", "Error", JOptionPane.ERROR_MESSAGE);
}
}
出力:

3. コンポーネント、オブジェクト、文字列、intおよびアイコン
メッセージダイアログを「きれいに」します。 ディレクトリから取得したIconの例:
SimpleDialog3a.java
package messageDialog;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
public class SimpleDialog3a {
public static void main(String[] args){
ImageIcon icon = new ImageIcon("src/images/turtle64.png");
JOptionPane.showMessageDialog(null, "I like turtles.",
"Customized Dialog", JOptionPane.INFORMATION_MESSAGE, icon);
}
}
出力:

Componentをframeに設定した例:
MessageDialogInFrame.java
package com.example.messageDialog;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.awt.Color;
public class MessageDialogInFrame extends JFrame{
public MessageDialogInFrame() {
getContentPane().setBackground(Color.DARK_GRAY);
setTitle("Message 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");
JOptionPane.showMessageDialog(new MessageDialogInFrame(),
"I appear as part of the frame!!", "Customized Dialog",
JOptionPane.INFORMATION_MESSAGE, icon);
}
}
出力:

4. より高度な例
この例では、JPanelをObjectパラメーターとして渡します。 JPanelはカスタマイズされており、JLabelが追加されています。 また、UIManagerの呼び出しを使用して、OptionPaneのサイズを操作しています。
MessageDialogPanel.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 MessageDialogPanel {
public static void main(String[] args){
ImageIcon icon = new ImageIcon("src/images/turtle64.png");
JPanel panel = new JPanel();
panel.setBackground(new Color(102, 205, 170));
panel.setSize(new Dimension(200, 64));
panel.setLayout(null);
JLabel label = new JLabel("Turtles are awesome!!! :D");
label.setBounds(0, 0, 200, 64);
label.setFont(new Font("Arial", Font.BOLD, 11));
label.setHorizontalAlignment(SwingConstants.CENTER);
panel.add(label);
UIManager.put("OptionPane.minimumSize",new Dimension(300, 120));
JOptionPane.showMessageDialog(null, panel, "Customized Message Dialog", JOptionPane.PLAIN_MESSAGE, icon);
}
}
出力:
