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示例(xuhuhu.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 xuhuhu.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);
}
}
執行上面示例代碼,得到以下結果: