我有一个应用程序,我需要在一个框架上工作,同时我想打开一个对话框。
所以我将模态设置为Dialog.ModalityType.MODELESS
. 虽然这使我能够与父 JFrame 交互,但我不能getValue()
再在对话框中使用。
这是一个正在运行的最小示例:
package Test;
import java.awt.Dimension;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
public class TestModalityDialog {
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame = new JFrame();
frame.setPreferredSize(new Dimension(800,600));
frame.setVisible(true);
frame.pack();
JOptionPane optionPane = new JOptionPane();
String[] options = new String[]{"Hello"};
JLabel label1 = new JLabel(
"Click on a cluster to delete it (needs to be confirmed by pressing the 'Confirm' button.");
JLabel label2 = new JLabel(
"Press 'p' to undelete an unconfirmed deletion.");
Object complexMsg[] = { label1, label2 };
optionPane.setMessage(complexMsg);
optionPane.setOptions(options);
optionPane.setMessageType(JOptionPane.PLAIN_MESSAGE);
JDialog dialog = optionPane.createDialog(frame,
"Select undesired clusters");
//dialog.setModalityType(Dialog.ModalityType.MODELESS); //uncomment this line out
dialog.setVisible(true);
dialog.setVisible(false);//must be set to false for Modality to work
dialog.setVisible(true);
Object obj = optionPane.getValue();
int result = -1;
for (int k = 0; k < options.length; k++) {
if (options[k].equals(obj)) {
result = k;
}
}
if (result == 0) {
System.out.println("Succesful");
}
}
}
当你按下标有“你好”的按钮时,系统在这里工作。把你不能与后面的框架交互。如果你取消注释
//dialog.setModalityType(Dialog.ModalityType.MODELESS);
这将允许“交互”(不在这个最小的例子 ofc 中),但我不再得到系统了。
我没想到的第二件事是您必须在未注释的版本中按两次按钮才能工作。
寻求帮助我会很高兴,已经尝试了其他 3 个模态值,但是没有用。
干杯,菩萨