默认情况下,当我选择任何按钮时,JOptionPane会关闭框架但在我的情况下,我希望它在我选择其中一个按钮时保持打开状态,这样我就可以显示一个弹出屏幕(背景中的原始框架) )。
我该怎么做?
这是我的代码: (它非常混乱,我只是在收集我的想法。我有一个更大的官方程序,我将在稍后实现)
当我选择A-E时,我会弹出一个“Pick”或“Cancel”对话框。我不希望原始帧关闭。
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JLabel;
import java.awt.*;
import javax.swing.*;
public class Quiz
{
public static void main(String[] args)
{
String message = "";
for(int j = 0; j < 40; j++)
message += "hello world ";
JScrollPane scrollPane = new JScrollPane(new JLabel(message));
scrollPane.setPreferredSize(new Dimension(500,400));
Object test = scrollPane;
String[] possibleAns = { "E", "D", "C", "B", "A" };
JTextArea textArea = new JTextArea(message);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setMargin(new Insets(5,5,5,5));
scrollPane.getViewport().setView(textArea);
test = scrollPane;
int rc = JOptionPane.showOptionDialog(null, test, "Advanced Quiz Program",
JOptionPane.WARNING_MESSAGE, 0, null, possibleAns, possibleAns[0]);
System.out.println(rc);
String[] pick = {"Pick", "Close" };
switch(rc)
{
case 4:
System.out.println("You picked A");
JScrollPane scrollPane2 = new JScrollPane(new JLabel(message));
scrollPane.setPreferredSize(new Dimension(500,400));
Object messaseA = scrollPane;
JTextArea textArea2 = new JTextArea("blah blah");
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setMargin(new Insets(5,5,5,5));
scrollPane.getViewport().setView(textArea);
test = scrollPane;
int retVal = JOptionPane.showOptionDialog(null, test, "Advanced Quiz Program",
JOptionPane.WARNING_MESSAGE, 0, null, pick, pick[0]);
break;
case 3:
System.out.println("You picked B");
break;
case 2:
System.out.println("You picked C");
break;
case 1:
System.out.println("You picked D");
break;
case 0:
System.out.println("You picked E");
break;
default:
System.out.println("error");
}
}
}
答案 0 :(得分:3)
最好的选择:不要使用JOptionPane。如果你想要一个顶级主窗口,使用JFrame,如果你想要一个保持打开但是非模态的对话窗口,那么使用非模态JDialog。如果你想要一个模态对话窗口,那么使用JOptionPane或模态JDialog(实际创建一个JOptionPane)。
如果必须使用JOptionPane,可以使用JOptionPane构造函数创建一个从中提取对话框,并使其成为非模态。
根据您的编辑和代码,您似乎尝试使用JOptionPane作为主程序窗口,这是您真正不想做的事情。我建议: