Swing-joptionpane
SWING-JOptionPaneクラス
前書き
クラス JOptionPane は、値の標準ダイアログボックスをポップアップするための標準メソッドを提供したり、ユーザーに何かを通知したりするコンポーネントです。
クラス宣言
以下は javax.swing.JOptionPane クラスの宣言です-
public class JOptionPane
extends JComponent
implements Accessible
フィールド
以下は javax.swing.JOptionPane クラスのフィールドです-
- static int CANCEL_OPTION -CANCELが選択されている場合、クラスメソッドから値を返します。
- static int CLOSED_OPTION -ユーザーが何も選択せずにウィンドウを閉じた場合、クラスメソッドから値を返します。これはおそらくCANCEL_OPTIONまたはNO_OPTIONとして扱われるべきです。
- static int DEFAULT_OPTION -Look&FeelがJOptionPaneのオプションのみを使用するオプションを提供することを意味する型を意味します。
- static int ERROR_MESSAGE -エラーメッセージに使用されます。
- *保護されたアイコンicon *-ペインで使用されるアイコン。
- static string ICON_PROPERTY -アイコンのバインドされたプロパティ名。
- static int INFORMATION_MESSAGE -情報メッセージに使用されます。
- *静的文字列INITIAL_SELECTION_VALUE_PROPERTY *-initialSelectionValueのバインドされたプロパティ名。
- *静的文字列INITIAL_VALUE_PROPERTY *-initialValueのバインドされたプロパティ名。
- protected Object initialSelectionValue -selectionValuesで選択する初期値。
- protected Object initialValue -オプションで最初に選択する値。
- *静的文字列INPUT_VALUE_PROPERTY *-inputValueのバインドされたプロパティ名。
- protected Object inputValue -ユーザーが入力した値。
- protected Object message -表示するメッセージ。
- *静的文字列MESSAGE_PROPERTY *-メッセージのバインドされたプロパティ名。
- *静的文字列MESSAGE_TYPE_PROPERTY *-型のバインドされたプロパティ名。
- static int OK_CANCEL_OPTION -showConfirmDialogに使用されるタイプ。
- protected int messageType -メッセージタイプ。
- static int NO_OPTION -NOが選択された場合、クラスメソッドから値を返します。
- static int OK_OPTION -OKが選択された場合、クラスメソッドから値を返します。
- *静的文字列OPTION_TYPE_PROPERTY *-optionTypeのバインドされたプロパティ名。
- protected Object [] options -ユーザーに表示するオプション。
- *静的文字列OPTIONS_PROPERTY *-オプションのバインドされたプロパティ名。
- protected int optionType -オプションタイプ、DEFAULT_OPTION、YES_NO_OPTION、YES_NO_CANCEL_OPTION、またはOK_CANCEL_OPTIONのいずれか。
- static int PLAIN_MESSAGE -アイコンは使用されません。
- static int QUESTION_MESSAGE -質問に使用されます。
- *静的文字列SELECTION_VALUES_PROPERTY *-selectionValuesのバインドされたプロパティ名。
- protected Object [] selectionValues -ユーザーが選択できる値の配列。
- *静的オブジェクトUNINITIALIZED_VALUE *-ユーザーがまだ値を選択していないことを示します。
- 保護されたオブジェクト値-現在選択されている値は、有効なオプション、またはUNINITIALIZED_VALUEまたはnullになります。
- *静的文字列VALUE_PROPERTY *-値のバインドされたプロパティ名。
- *静的文字列WANTS_INPUT_PROPERTY *-wantsInputのバインドされたプロパティ名。
- protected boolean wantsInput -trueの場合、入力を取得するためのUIウィジェットがユーザーに提供されます。
- static int WARNING_MESSAGE -警告メッセージに使用されます。
- static int YES_NO_CANCEL_OPTION -showConfirmDialogに使用されるタイプ。
- static int YES_NO_OPTION -showConfirmDialogに使用されるタイプ。
- static int YES_OPTION -YESが選択されている場合、クラスメソッドから値を返します。
クラスコンストラクター
Sr.No. | Constructor & Description |
---|---|
1 |
JOptionPane() テストメッセージを使用してJOptionPaneを作成します。 |
2 |
JOptionPane(Object message) JOptionPaneのインスタンスを作成して、プレーンメッセージのメッセージタイプとUIによって提供されるデフォルトオプションを使用してメッセージを表示します。 |
3 |
JOptionPane(Object message, int messageType) JOptionPaneのインスタンスを作成して、指定されたメッセージタイプとデフォルトオプションのメッセージを表示します。 |
4 |
JOptionPane(Object message, int messageType, int optionType) JOptionPaneのインスタンスを作成して、指定されたメッセージタイプとオプションでメッセージを表示します。 |
5 |
JOptionPane(Object message, int messageType, int optionType, Icon icon) JOptionPaneのインスタンスを作成して、指定されたメッセージタイプ、オプション、およびアイコンでメッセージを表示します。 |
6 |
JOptionPane(Object message, int messageType, int optionType, Icon icon, Object[] options) JOptionPaneのインスタンスを作成して、指定されたメッセージタイプ、アイコン、およびオプションでメッセージを表示します。 |
7 |
JOptionPane(Object message, int messageType, int optionType, Icon icon, Object[] options, Object initialValue) JOptionPaneのインスタンスを作成し、指定されたメッセージタイプ、アイコン、オプション、および最初に選択されたオプションを指定したメッセージを表示します。 |
クラスメソッド
Swing JOptionPaneクラスのメソッドのリストを次に示します。
Sr.No. | Method & Description |
---|---|
1 |
JDialog createDialog(Component parentComponent, String title) parentComponentのフレーム内のparentComponentを中心としてこれをラップする新しいJDialogを作成して返します。 |
2 |
JDialog createDialog(String title) 指定されたタイトルで新しい親のないJDialogを作成して返します。 |
3 |
JInternalFrame createInternalFrame(Component parentComponent, String title) JInternalFrameのインスタンスを作成して返します。 |
4 |
AccessibleContext getAccessibleContext() このJOptionPaneに関連付けられているAccessibleContextを返します。 |
5 |
static JDesktopPane getDesktopPaneForComponent(Component parentComponent) 指定されたコンポーネントのデスクトップペインを返します。 |
6 |
static Frame getFrameForComponent(Component parentComponent) 指定されたコンポーネントのフレームを返します。 |
7 |
Icon getIcon() このペインに表示されるアイコンを返します。 |
8 |
Object getInitialSelectionValue() ユーザーに最初に選択されたときに表示される入力値を返します。 |
9 |
Object getInitialValue() 初期値を返します。 |
10 |
Object getInputValue() wantsInputがtrueの場合、ユーザーが入力した値を返します。 |
11 |
int getMaxCharactersPerLineCount() メッセージの行に配置する最大文字数を返します。 |
12 |
Object getMessage() このペインが表示するメッセージオブジェクトを返します。 |
13 |
int getMessageType() メッセージタイプを返します。 |
14 |
Object[] getOptions() ユーザーが選択できる選択肢を返します。 |
15 |
int getOptionType() 表示されるオプションのタイプを返します。 |
16 |
static Frame getRootFrame() フレームが提供されないクラスメソッドに使用するフレームを返します。 |
17 |
Object[] getSelectionValues() 入力選択値を返します。 |
18 |
OptionPaneUI getUI() このコンポーネントのL&Fを実装するUIオブジェクトを返します。 |
19 |
String getUIClassID() このコンポーネントのL&Fを実装するUIクラスの名前を返します。 |
20 |
Object getValue() ユーザーが選択した値を返します。 |
21 |
boolean getWantsInput() wantsInputプロパティの値を返します。 |
22 |
protected String paramString() このJOptionPaneの文字列表現を返します。 |
23 |
void selectInitialValue() 初期値を選択するように要求します。これにより、フォーカスが初期値に設定されます。 |
24 |
void setIcon(Icon newIcon) 表示するアイコンを設定します。 |
25 |
void setInitialSelectionValue(Object newValue) ユーザーが選択したとおりに最初に表示される入力値を設定します。 |
26 |
void setInitialValue(Object newInitialValue) 有効にする初期値、つまりペインが最初に表示されるときにフォーカスを持つコンポーネントを設定します。 |
27 |
void setInputValue(Object newValue) ユーザーが選択または入力した入力値を設定します。 |
28 |
void setMessage(Object newMessage) オプションペインのメッセージオブジェクトを設定します。 |
29 |
void setMessageType(int newType) オプションペインのメッセージタイプを設定します。 |
30 |
void setOptions(Object[] newOptions) このペインに表示されるオプションを設定します。 |
31 |
void setOptionType(int newType) 表示するオプションを設定します。 |
32 |
static voidsetRootFrame(Frame newRootFrame) フレームが提供されないクラスメソッドに使用するフレームを設定します。 |
33 |
void setSelectionValues(Object[] newValues) ユーザーに選択項目のリストを提供するペインの入力選択値を設定します。 |
34 |
void setUI(OptionPaneUI ui) このコンポーネントのL&Fを実装するUIオブジェクトを設定します。 |
35 |
void setValue(Object newValue) ユーザーが選択した値を設定します。 |
36 |
void setWantsInput(boolean newValue) wantsInputプロパティを設定します。 |
37 |
static int showConfirmDialog(Component parentComponent, Object message) [はい]、[いいえ]、[キャンセル]のオプションを含むダイアログを表示しますタイトルとともに、オプションを選択します。 |
38 |
static int showConfirmDialog(Component parentComponent, Object message, String title, int optionType) 選択肢の数がoptionTypeパラメーターによって決定されるダイアログを表示します。 |
39 |
static int showConfirmDialog(Component parentComponent, Object message, String title, int optionType, int messageType) 選択肢の数がoptionTypeパラメーターによって決定されるダイアログを表示します。messageTypeパラメーターは表示するアイコンを決定します。 |
40 |
static int showConfirmDialog(Component parentComponent, Object message, String title, int optionType, int messageType, Icon icon) 指定したアイコンのダイアログを表示します。選択の数は、optionTypeパラメーターによって決定されます。 |
41 |
static String showInputDialog(Component parentComponent, Object message) parentComponentの親であるユーザーからの入力を要求する質問メッセージダイアログを表示します。 |
42 |
static String showInputDialog(Component parentComponent, Object message, Object initialSelectionValue) ユーザーからの入力を要求し、parentComponentの親となる質問メッセージダイアログを表示します。 |
43 |
static String showInputDialog(Component parentComponent, Object message, String title, int essageType) タイトルタイトルとメッセージタイプmessageTypeを持つダイアログで、parentComponentを親とするユーザーからの入力を要求するダイアログを表示します。 |
44 |
static Object showInputDialog(Component parentComponent, Object message, String title, int messageType, Icon icon, Object[] selectionValues, Object initialSelectionValue) 初期選択、可能な選択、および他のすべてのオプションを指定できるブロックダイアログで、ユーザーに入力を求めます。 |
45 |
static String showInputDialog(Object message) ユーザーからの入力を要求する質問メッセージダイアログを表示します。 |
46 |
static String showInputDialog(Object message, Object initialSelectionValue) 入力値をinitialSelectionValueに初期化して、ユーザーからの入力を要求する質問メッセージダイアログを表示します。 |
47 |
static int showInternalConfirmDialog(Component parentComponent, Object message) [はい]、[いいえ]、[キャンセル]のオプションを備えた内部ダイアログパネルを表示します。タイトルとともに、オプションを選択します。 |
48 |
static int showInternalConfirmDialog(Component parentComponent, Object message, String title, int optionType) 選択肢の数がoptionTypeパラメータによって決定される内部ダイアログパネルを表示します。 |
49 |
static int showInternalConfirmDialog(Component parentComponent, Object message, String title, int optionType, int messageType) 選択肢の数がoptionTypeパラメーターによって決定される内部ダイアログパネルを表示します。messageTypeパラメーターは表示するアイコンを決定します。 |
50 |
static int showInternalConfirmDialog(Component parentComponent, Object message, String title, int optionType, int messageType, Icon icon) 指定されたアイコンを持つ内部ダイアログパネルを表示します。選択の数は、optionTypeパラメーターによって決定されます。 |
51 |
static String showInternalInputDialog(Component parentComponent, Object message) parentComponentの親であるユーザーからの入力を要求する内部質問メッセージダイアログを表示します。 |
52 |
static String showInternalInputDialog(Component parentComponent, Object message, String title, int messageType) タイトルタイトルとメッセージタイプmessageTypeを持つダイアログで、parentComponentを親とするユーザーからの入力を要求する内部ダイアログを表示します。 |
53 |
static Object showInternalInputDialog(Component parentComponent, Object message, String title, int messageType, Icon icon, Object[] selectionValues, Object initialSelectionValue) 初期選択、可能な選択、および他のすべてのオプションを指定できるブロッキング内部ダイアログでユーザーに入力を求めます。 |
54 |
static voidshowInternalMessageDialog(Component parentComponent, Object message) 内部確認ダイアログパネルを表示します。 |
55 |
static voidshowInternalMessageDialog(Component parentComponent, Object message, String title, int messageType) messageTypeパラメーターによって決定されるデフォルトアイコンを使用してメッセージを表示する内部ダイアログパネルを表示します。 |
56 |
static voidshowInternalMessageDialog(Component parentComponent, Object message, String title, int messageType, Icon icon) すべてのパラメーターを指定して、メッセージを表示する内部ダイアログパネルを表示します。 |
57 |
static voidshowMessageDialog(Component parentComponent, Object message) 「メッセージ」というタイトルの情報メッセージダイアログを表示します。 |
58 |
static voidshowMessageDialog(Component parentComponent, Object message, String title, int messageType) messageTypeパラメーターによって決定されるデフォルトのアイコンを使用してメッセージを表示するダイアログを表示します。 |
59 |
static voidshowMessageDialog(Component parentComponent, Object message, String title, int messageType, Icon icon) すべてのパラメーターを指定して、メッセージを表示するダイアログを表示します。 |
60 |
static int showOptionDialog(Component parentComponent, Object message, String title, int optionType, int messageType, Icon icon, Object[] options, Object initialValue) 指定されたアイコンのダイアログを表示します。最初の選択はinitialValueパラメーターによって決定され、選択の数はoptionTypeパラメーターによって決定されます。 |
61 |
void updateUI() L&Fが変更されたというUIManagerからの通知。 |
62 |
static int showInternalOptionDialog(Component parentComponent, Object message, String title, int optionType, int messageType, Icon icon, Object[] options, Object initialValue) 指定されたアイコンで内部ダイアログパネルを表示します。初期選択はinitialValueパラメーターによって決定され、選択の数はoptionTypeパラメーターによって決定されます。 |
継承されるメソッド
このクラスは、次のクラスからメソッドを継承します-
- javax.swing.JComponent
- java.awt.Container
- java.awt.Component
- java.lang.Object
JOptionPaneの例
たとえば、 D:/> SWING> com> finddevguides> gui> の任意のエディターを使用して、次のJavaプログラムを作成します。
SwingControlDemo.java
package com.finddevguides.gui;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingControlDemo {
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
public SwingControlDemo(){
prepareGUI();
}
public static void main(String[] args){
SwingControlDemo swingControlDemo = new SwingControlDemo();
swingControlDemo.showDialogDemo();
}
private void prepareGUI(){
mainFrame = new JFrame("Java Swing Examples");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1));
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
headerLabel = new JLabel("", JLabel.CENTER);
statusLabel = new JLabel("",JLabel.CENTER);
statusLabel.setSize(350,100);
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
private void showDialogDemo(){
headerLabel.setText("Control in action: JOptionPane");
JButton okButton = new JButton("OK");
JButton javaButton = new JButton("Yes/No");
JButton cancelButton = new JButton("Yes/No/Cancel");
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(mainFrame, "Welcome to finddevguides.com");
}
});
javaButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int output = JOptionPane.showConfirmDialog(mainFrame
, "Click any button"
,"finddevguides.com"
,JOptionPane.YES_NO_OPTION);
if(output == JOptionPane.YES_OPTION){
statusLabel.setText("Yes selected.");
} else if(output == JOptionPane.NO_OPTION){
statusLabel.setText("No selected.");
}
}
});
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int output = JOptionPane.showConfirmDialog(mainFrame
, "Click any button"
,"finddevguides.com"
,JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.INFORMATION_MESSAGE);
if(output == JOptionPane.YES_OPTION){
statusLabel.setText("Yes selected.");
} else if(output == JOptionPane.NO_OPTION){
statusLabel.setText("No selected.");
} else if(output == JOptionPane.CANCEL_OPTION){
statusLabel.setText("Cancel selected.");
}
}
});
controlPanel.add(okButton);
controlPanel.add(javaButton);
controlPanel.add(cancelButton);
mainFrame.setVisible(true);
}
}
コマンドプロンプトを使用してプログラムをコンパイルします。 D:/> SWING に移動して、次のコマンドを入力します。
D:\SWING>javac com\finddevguides\gui\SwingControlDemo.java
エラーが発生しない場合、コンパイルが成功したことを意味します。 次のコマンドを使用してプログラムを実行します。
D:\SWING>java com.finddevguides.gui.SwingControlDemo
次の出力を確認します。