對話框模態是關鍵
作爲模態可以防止用戶在顯示對話框,其中GUI的其他組分相互作用不能用正常的JOptionPane或與任何模態對話框做到這一點。如果你創建一個非模態對話框,你只能得到這個工作,這意味着JOptionPane必須不是用JOptionPane靜態工廠方法創建的,而是以非傳統的方式使用JOptionPane構造函數創建的 - 請檢查JOptionPane API這個怎麼做。
例如:
import java.awt.Dialog.ModalityType;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import javax.swing.*;
public class NonModalJOptionPane {
private static void createAndShowGui() {
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(400, 300));
final JFrame frame = new JFrame("NonModalJOptionPane");
panel.add(new JButton(new AbstractAction("Push Me") {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane optionPane = new JOptionPane("My Message", JOptionPane.PLAIN_MESSAGE);
JDialog dialog = optionPane.createDialog(frame, "My Option");
dialog.setModalityType(ModalityType.MODELESS); // **** key ***
dialog.setVisible(true);
}
}));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
這個代碼的關鍵是在這裏:
// create the JOptionPane using one of its constructors
JOptionPane optionPane = new JOptionPane("My Message", JOptionPane.PLAIN_MESSAGE);
// create a JDialog from it, tying it to the parent JFrame, here called "frame"
JDialog dialog = optionPane.createDialog(frame, "My Option");
// setting the modality type so that it is **not** modal
dialog.setModalityType(ModalityType.MODELESS); // **** key ***
// and then displaying it
dialog.setVisible(true);
當我通過它的構造函數創建的JOptionPane,而不是通過一個靜態方法,我創建一個JDialog並設置它是MODELESS,然後顯示它。
另一個可行的選擇是創建您自己的JDialog,確保您將它設置爲非模態。
例如,您可以將以下代碼添加到上面的代碼:
panel.add(new JButton(new AbstractAction("Push Me 2 -- Using Dialog") {
@Override
public void actionPerformed(ActionEvent e) {
// button that when pressed, closes the JDialog that holds it
// similar to a JOptionPane's OK button
JButton disposeWinBtn = new JButton(new DisposeWindowAction("OK", KeyEvent.VK_O));
// create a bunch of JPanels, add components to them, ...
JPanel bottomPanel = new JPanel();
bottomPanel.add(disposeWinBtn);
JLabel msgLabel = new JLabel("My Message");
JPanel msgPanel = new JPanel();
msgPanel.add(msgLabel);
JPanel panel = new JPanel(new BorderLayout());
panel.add(msgPanel, BorderLayout.CENTER);
panel.add(bottomPanel, BorderLayout.PAGE_END);
// create a JDialog whose parent component is the main JFrame
// and make sure that it is *****non-modal ***** <===== this is KEY *****
JDialog dialog = new JDialog(frame, "Dialog", ModalityType.MODELESS);
dialog.add(panel); // add the JPanel, panel, created above, with components
dialog.pack(); // have layout managers do their thing
dialog.setLocationRelativeTo(frame); // center it over the main JFrame
dialog.setVisible(true); // and display it
}
}));
不到其中的第一個按鈕添加。您還需要在DisposeWindowAction類,允許按鈕關閉並正在顯示它的窗口(這裏一個JDialog窗)的配置:
import java.awt.Component;
import java.awt.Window;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.SwingUtilities;
@SuppressWarnings("serial")
public class DisposeWindowAction extends AbstractAction {
public DisposeWindowAction(String name, int mnemonic) {
super(name);
putValue(MNEMONIC_KEY, mnemonic);
}
@Override
public void actionPerformed(ActionEvent e) {
Component component = (Component) e.getSource();
if (component == null) {
return;
}
Window win = SwingUtilities.getWindowAncestor(component);
if (win == null) {
return;
}
win.dispose();
}
}
請參閱編輯回答,並隨時問,如果你有任何問題。 –