📜  Java Swing-JOptionPane

📅  最后修改于: 2020-09-30 05:59:34             🧑  作者: Mango

Java JOptionPane

JOptionPane类用于提供标准对话框,例如消息对话框,确认对话框和输入对话框。这些对话框用于显示信息或从用户那里获取输入。 JOptionPane类继承了JComponent类。

JOptionPane类声明

public class JOptionPane extends JComponent implements Accessible

JOptionPane类的常见构造函数

Constructor Description
JOptionPane() It is used to create a JOptionPane with a test message.
JOptionPane(Object message) It is used to create an instance of JOptionPane to display a message.
JOptionPane(Object message, int messageType It is used to create an instance of JOptionPane to display a message with specified message type and default options.

JOptionPane类的常用方法

Methods Description
JDialog createDialog(String title) It is used to create and return a new parentless JDialog with the specified title.
static void showMessageDialog(Component parentComponent, Object message) It is used to create an information-message dialog titled “Message”.
static void showMessageDialog(Component parentComponent, Object message, String title, int messageType) It is used to create a message dialog with given title and messageType.
static int showConfirmDialog(Component parentComponent, Object message) It is used to create a dialog with the options Yes, No and Cancel; with the title, Select an Option.
static String showInputDialog(Component parentComponent, Object message) It is used to show a question-message dialog requesting input from the user parented to parentComponent.
void setInputValue(Object newValue) It is used to set the input value that was selected or input by the user.

Java JOptionPane示例:showMessageDialog()

import javax.swing.*;
public class OptionPaneExample {
JFrame f;
OptionPaneExample(){
f=new JFrame();
JOptionPane.showMessageDialog(f,"Hello, Welcome to Javatpoint.");
}
public static void main(String[] args) {
new OptionPaneExample();
}
}

输出:

Java JOptionPane示例:showMessageDialog()

import javax.swing.*;
public class OptionPaneExample {
JFrame f;
OptionPaneExample(){
f=new JFrame();
JOptionPane.showMessageDialog(f,"Successfully Updated.","Alert",JOptionPane.WARNING_MESSAGE);
}
public static void main(String[] args) {
new OptionPaneExample();
}
}

输出:

Java JOptionPane示例:showInputDialog()

import javax.swing.*;
public class OptionPaneExample {
JFrame f;
OptionPaneExample(){
f=new JFrame();
String name=JOptionPane.showInputDialog(f,"Enter Name");
}
public static void main(String[] args) {
new OptionPaneExample();
}
}

输出:

Java JOptionPane示例:showConfirmDialog()

import javax.swing.*;
import java.awt.event.*;
public class OptionPaneExample extends WindowAdapter{
JFrame f;
OptionPaneExample(){
f=new JFrame();
f.addWindowListener(this);
f.setSize(300, 300);
f.setLayout(null);
f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
f.setVisible(true);
}
public void windowClosing(WindowEvent e) {
int a=JOptionPane.showConfirmDialog(f,"Are you sure?");
if(a==JOptionPane.YES_OPTION){
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public static void main(String[] args) {
new  OptionPaneExample();
}
}

输出: