我有一个应用程序,在这个应用程序中,我需要处理一个框架,而我希望打开一个对话框。
因此,我将模式设置为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");
}
}
}当您按下标记为"Hello“的按钮时,系统在这里工作。放,你不能与后面的框架交互。如果你取消评论
//dialog.setModalityType(Dialog.ModalityType.MODELESS);这将允许“交互”(在这个最小的例子ofc),但我不再得到一个系统。
我没想到的第二件事是,在未注释的版本中,您必须按两次按钮才能工作。
为了得到帮助,我会很高兴,已经尝试了其他3种modalitie值,但没有起作用。
干杯,佛陀
https://stackoverflow.com/questions/22540217
复制相似问题