JOptionPane
类是一个组件,它提供标准方法来弹出值的标准对话框或通知用户某些内容。
类声明
以下是javax.swing.JOptionPane
类的声明 -
public class JOptionPane
extends JComponent
implements Accessible
字段
以下是javax.swing.JOptionPane
类的字段 -
类构造函数
编号 | 构造函数 | 描述 |
---|---|---|
1 | JOptionPane() |
使用测试消息创建JOptionPane 。 |
2 | JOptionPane(Object message) |
以使用纯文本消息类型和UI提供的默认选项显示消息创建JOptionPane 实例。 |
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实例。 |
类方法
以下是javax.swing.JOptionPane
类的方法 -
方法继承
该类继承以下类中的方法 -
javax.swing.JComponent
java.awt.Container
java.awt.Component
java.lang.Object
JOptionPane示例
使用编辑器创建以下Java程序:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JOptionPaneExample {
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
public JOptionPaneExample() {
prepareGUI();
}
public static void main(String[] args) {
JOptionPaneExample swingControlDemo = new JOptionPaneExample();
swingControlDemo.showDialogDemo();
}
private void prepareGUI() {
mainFrame = new JFrame("Java/Swing JOptionPane示例(yiibai.com)");
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("是");
JButton javaButton = new JButton("是/否");
JButton cancelButton = new JButton("是/否/取消");
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(mainFrame, "Welcome to yiibai.com");
}
});
javaButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int output = JOptionPane.showConfirmDialog(mainFrame, "点击一个按钮", "kaops.com",
JOptionPane.YES_NO_OPTION);
if (output == JOptionPane.YES_OPTION) {
statusLabel.setText("选择:'是'");
} else if (output == JOptionPane.NO_OPTION) {
statusLabel.setText("选择:'否'");
}
}
});
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int output = JOptionPane.showConfirmDialog(mainFrame, "点击一个按钮", "Kaops.com",
JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);
if (output == JOptionPane.YES_OPTION) {
statusLabel.setText("选择:'是'");
} else if (output == JOptionPane.NO_OPTION) {
statusLabel.setText("选择:'否'");
} else if (output == JOptionPane.CANCEL_OPTION) {
statusLabel.setText("选择:'取消'");
}
}
});
controlPanel.add(okButton);
controlPanel.add(javaButton);
controlPanel.add(cancelButton);
mainFrame.setVisible(true);
}
}
执行上面示例代码,得到以下结果: