这就是场景,我的JFrame有一个按钮,当点击它时会打开一个JDialog,这是一个模型对话框。JDialog有另一个按钮,当单击它时,我想打开另一个JFrmae。
结果:另一个Jframe打开,但它不会出现在对话框下面的top.It显示中,我想在对话框的顶部打开第二个JFrame。
可以使用secondFrame.setAlwaysOnTop(true);,但我无法控制它的关闭或移动。
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
public class FrameTest
{
public static void main(String args[])
{
JFrame firstFrame = new JFrame("My 1st Frame");
JButton button = new JButton("Frame Click");
button.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
JDialog dialog = new JDialog();
dialog.setSize(100, 100);
dialog.setModal(true);
JButton button1 = new JButton("Dialog Click");
button1.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
JFrame secondFrame = new JFrame("My 2nd Frame");
secondFrame.setVisible(true);
secondFrame.setSize(400, 200);
secondFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
secondFrame.setAlwaysOnTop(true);
}
});
dialog.add(button1);
dialog.setVisible(true);
}
});
firstFrame.add(button);
firstFrame.setVisible(true);
firstFrame.setSize(400, 200);
firstFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}发布于 2014-03-07 13:24:50
JDialog有另一个按钮,当单击它时,我想打开另一个JFrmae。
别干那事。一个提示性的Swing应用程序有一个主JFrame和几个JDialog。
结果:另一个Jframe打开了,但它不会出现在对话框下面的top.It上,我想在对话框的顶部打开第二个JFrame。
当然,这是因为对话是有模态的。
可以使用secondFrame.setAlwaysOnTop(真);但我无法控制它的关闭或移动。
它不会解决任何问题,因为问题与对话中的情态有关。请参阅本文:对话中如何使用情态以了解情态是如何工作的。在这个答案中也有一个解释。
发布于 2022-04-20 15:10:41
试一试
secondFrame.setModalExclusionType(ModalExclusionType.APPLICATION_EXCLUDE);在同样的情况下,它对我起了作用。
https://stackoverflow.com/questions/22249467
复制相似问题