📄 joptionpane.java
字号:
startModal(frame); if (pane.getValue() instanceof Integer) return ((Integer) pane.getValue()).intValue(); return -1; } /** * This method shows an internal confirmation dialog with the given message, * title, option type, message type, and icon. The internal frame dialog * will be placed in the first JDesktopPane ancestor that is found in the * given parentComponent. This method returns the selected value. * * @param parentComponent The parent to find a JDesktopPane in. * @param message The message to display. * @param title The title to display. * @param optionType The option type. * @param messageType The message type. * @param icon The icon to display. * * @return The selected value. */ public static int showInternalConfirmDialog(Component parentComponent, Object message, String title, int optionType, int messageType, Icon icon) { JOptionPane pane = new JOptionPane(message, messageType, optionType, icon); JInternalFrame frame = pane.createInternalFrame(parentComponent, title); startModal(frame); if (pane.getValue() instanceof Integer) return ((Integer) pane.getValue()).intValue(); return -1; } /** * This method shows an internal input dialog with the given message. The * internal frame dialog will be placed in the first JDesktopPane ancestor * of the given parent component. This method returns the value input by * the user. * * @param parentComponent The parent to find a JDesktopPane in. * @param message The message to display. * * @return The user selected value. */ public static String showInternalInputDialog(Component parentComponent, Object message) { JOptionPane pane = new JOptionPane(message); pane.setWantsInput(true); JInternalFrame frame = pane.createInternalFrame(parentComponent, null); startModal(frame); return (String) pane.getInputValue(); } /** * This method shows an internal input dialog with the given message, title * and message type. The internal input dialog will be placed in the first * JDesktopPane ancestor found in the given parent component. This method * will return the input value given by the user. * * @param parentComponent The component to find a JDesktopPane in. * @param message The message to display. * @param title The title to display. * @param messageType The message type. * * @return The user input value. */ public static String showInternalInputDialog(Component parentComponent, Object message, String title, int messageType) { JOptionPane pane = new JOptionPane(message, messageType); pane.setWantsInput(true); JInternalFrame frame = pane.createInternalFrame(parentComponent, title); startModal(frame); return (String) pane.getInputValue(); } /** * This method shows an internal input dialog with the given message, title * message type, icon, selection value list and initial selection value. * The internal frame dialog will be placed in the first JDesktopPane * ancestor found in the given parent component. This method returns the * input value from the user. * * @param parentComponent The parent to find a JDesktopPane in. * @param message The message to display. * @param title The title to display. * @param messageType The message type. * @param icon The icon to display. * @param selectionValues The selection value list. * @param initialSelectionValue The initial selection value. * * @return The user input value. */ public static Object showInternalInputDialog(Component parentComponent, Object message, String title, int messageType, Icon icon, Object[] selectionValues, Object initialSelectionValue) { JOptionPane pane = new JOptionPane(message, messageType); pane.setWantsInput(true); pane.setIcon(icon); pane.setSelectionValues(selectionValues); pane.setInitialSelectionValue(initialSelectionValue); JInternalFrame frame = pane.createInternalFrame(parentComponent, title); startModal(frame); return pane.getInputValue(); } /** * This method shows an internal message dialog with the given message. The * internal frame dialog will be placed in the first JDesktopPane ancestor * found in the given parent component. * * @param parentComponent The component to find a JDesktopPane in. * @param message The message to display. */ public static void showInternalMessageDialog(Component parentComponent, Object message) { JOptionPane pane = new JOptionPane(message); JInternalFrame frame = pane.createInternalFrame(parentComponent, null); startModal(frame); } /** * This method shows an internal message dialog with the given message, * title and message type. The internal message dialog is placed in the * first JDesktopPane ancestor found in the given parent component. * * @param parentComponent The parent component to find a JDesktopPane in. * @param message The message to display. * @param title The title to display. * @param messageType The message type. */ public static void showInternalMessageDialog(Component parentComponent, Object message, String title, int messageType) { JOptionPane pane = new JOptionPane(message, messageType); JInternalFrame frame = pane.createInternalFrame(parentComponent, title); startModal(frame); } /** * This method shows an internal message dialog with the given message, * title, message type and icon. The internal message dialog is placed in * the first JDesktopPane ancestor found in the given parent component. * * @param parentComponent The component to find a JDesktopPane in. * @param message The message to display. * @param title The title to display. * @param messageType The message type. * @param icon The icon to display. */ public static void showInternalMessageDialog(Component parentComponent, Object message, String title, int messageType, Icon icon) { JOptionPane pane = new JOptionPane(message, messageType); pane.setIcon(icon); JInternalFrame frame = pane.createInternalFrame(parentComponent, title); startModal(frame); } /** * This method displays an internal option dialog with the given message, * title, option type, message type, icon, option list, and initial option * value. The internal option dialog is placed in the first JDesktopPane * ancestor found in the parent component. This method returns the option * selected. * * @param parentComponent The parent to find a JDesktopPane in. * @param message The message displayed. * @param title The title displayed. * @param optionType The option type. * @param messageType The message type. * @param icon The icon to display. * @param options The array of options. * @param initialValue The initial value selected. * * @return The option that was selected. */ public static int showInternalOptionDialog(Component parentComponent, Object message, String title, int optionType, int messageType, Icon icon, Object[] options, Object initialValue) { JOptionPane pane = new JOptionPane(message, messageType, optionType, icon, options, initialValue); JInternalFrame frame = pane.createInternalFrame(parentComponent, title); startModal(frame); if (pane.getValue() instanceof Integer) return ((Integer) pane.getValue()).intValue(); return -1; } /** * This method shows an INFORMATION_MESSAGE type message dialog. * * @param parentComponent The component to find a frame in. * @param message The message displayed. */ public static void showMessageDialog(Component parentComponent, Object message) { JOptionPane pane = new JOptionPane(message, INFORMATION_MESSAGE); JDialog dialog = pane.createDialog(parentComponent, null); dialog.show(); } /** * This method shows a message dialog with the given message, title and * messageType. * * @param parentComponent The component to find a frame in. * @param message The message displayed. * @param title The title of the dialog. * @param messageType The messageType. */ public static void showMessageDialog(Component parentComponent, Object message, String title, int messageType) { JOptionPane pane = new JOptionPane(message, messageType); JDialog dialog = pane.createDialog(parentComponent, title); dialog.show(); } /** * This method shows a message dialog with the given message, title, * messageType and icon. * * @param parentComponent The component to find a frame in. * @param message The message displayed. * @param title The title of the dialog. * @param messageType The messageType. * @param icon The icon displayed. */ public static void showMessageDialog(Component parentComponent, Object message, String title, int messageType, Icon icon) { JOptionPane pane = new JOptionPane(message, messageType); pane.setIcon(icon); JDialog dialog = pane.createDialog(parentComponent, title); dialog.show(); } /** * This method shows an option dialog with the given message, title, * optionType, messageType, icon, options and initialValue. This method * returns the option that was selected. * * @param parentComponent The component to find a frame in. * @param message The message displayed. * @param title The title of the dialog. * @param optionType The optionType. * @param messageType The messageType. * @param icon The icon displayed. * @param options The options to choose from. * @param initialValue The initial value. * * @return The selected option. */ public static int showOptionDialog(Component parentComponent, Object message, String title, int optionType, int messageType, Icon icon, Object[] options, Object initialValue) { JOptionPane pane = new JOptionPane(message, messageType, optionType, icon, options, initialValue); JDialog dialog = pane.createDialog(parentComponent, title); dialog.show(); if (pane.getValue() instanceof Integer) return ((Integer) pane.getValue()).intValue(); return -1; } /** * This method resets the UI to the Look and Feel default. */ public void updateUI() { setUI((OptionPaneUI) UIManager.getUI(this)); invalidate(); } /** * This method returns true if the key is a valid messageType. * * @param key The key to check. * * @return True if key is valid. */ private boolean validMessageType(int key) { switch (key) { case ERROR_MESSAGE: case INFORMATION_MESSAGE: case PLAIN_MESSAGE: case QUESTION_MESSAGE: case WARNING_MESSAGE: return true; } return false; } /** * This method returns true if the key is a valid optionType. * * @param key The key to check. * * @return True if key is valid. */ private boolean validOptionType(int key) { switch (key) { case DEFAULT_OPTION: case OK_CANCEL_OPTION: case YES_NO_CANCEL_OPTION: case YES_NO_OPTION: return true; } return false; } /** * This helper method makes the JInternalFrame wait until it is notified by * an InternalFrameClosing event. This method also adds the given * JOptionPane to the JInternalFrame and sizes it according to the * JInternalFrame's preferred size. * * @param f The JInternalFrame to make modal. */ private static void startModal(JInternalFrame f) { synchronized (f) { final JInternalFrame tmp = f; tmp.toFront(); f.addInternalFrameListener(new InternalFrameAdapter() { public void internalFrameClosed(InternalFrameEvent e) { synchronized (tmp) { tmp.removeInternalFrameListener(this); tmp.notifyAll(); } } }); try { while (! f.isClosed()) f.wait(); } catch (InterruptedException ignored) { // Ignore this Exception. } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -