gpt4 book ai didi

java - Jframe 中有 2 个 Jdialogs 的 setModal 问题

转载 作者:行者123 更新时间:2023-11-29 05:28:02 26 4
gpt4 key购买 nike

当我设置第一个 JDialog 模态和第二个非模态时,我遇到了问题。

这是我要实现的功能:

  1. 点击“测试对话框!”按钮,一个名为 Custom Dialog 的 JDialogMain 将打开。
  2. 如果在 Custom Dialog Main 中单击“yes”选项,另一个名为 Custom Dialog SearchJDialog 将打开。
  3. 如果在自定义对话框搜索中单击"is"选项,则Custom Dialog Main 应该放在前面。
  4. 而且我应该能够选择任何 JDialog。例如,如果我选择Custom Dialog Search,另一个对话框应该去返回,反之亦然。

我面临的问题是,当我在Custom Dialog Main 中单击“yes”时,Custom Dialog Search 会显示在主对话框后面。

发生这种情况是因为我将自定义对话框搜索 设置为非模态。如果我使用此对话框模式,它会正确显示,但在我单击"is"后,Custom Dialog Main 不会出现在前面。

我什至尝试将 CustomDialogSearch 的父级设置为 CustomDialog,但行为仍然不正确。

下面是我正在测试的示例代码。

import java.awt.event.ActionListener;  
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.ActionEvent;
import java.awt.Dimension;

public class TestTheDialog implements ActionListener {
JFrame mainFrame = null;
JButton myButton = null;

public TestTheDialog() {
mainFrame = new JFrame("TestTheDialog Tester");
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
myButton = new JButton("Test the dialog!");
myButton.addActionListener(this);
mainFrame.setLocationRelativeTo(null);
mainFrame.getContentPane().add(myButton);
mainFrame.pack();
mainFrame.setVisible(true);
}

public void actionPerformed(ActionEvent e) {
if(myButton == e.getSource()) {
System.err.println("Opening dialog.");
CustomDialog myDialog = new CustomDialog(mainFrame, true, "Custom Dialog Main?");
System.err.println("After opening dialog.");
if(myDialog.getAnswer()) {
System.err.println("The answer stored in CustomDialog is 'true' (i.e. user clicked yes button.)");
}
else {
System.err.println("The answer stored in CustomDialog is 'false' (i.e. user clicked no button.)");
}
}
}

public static void main(String argv[]) {

TestTheDialog tester = new TestTheDialog();
}
}

import javax.swing.JDialog;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionEvent;

public class CustomDialog extends JDialog implements ActionListener {
private JPanel myPanel = null;
private JButton yesButton = null;
private JButton noButton = null;
private boolean answer = false;
private JFrame parentFrame;
public boolean getAnswer() { return answer; }

public CustomDialog(JFrame frame, boolean modal, String myMessage) {
super(frame, modal);
parentFrame = frame;
myPanel = new JPanel();
getContentPane().add(myPanel);
myPanel.add(new JLabel(myMessage));
yesButton = new JButton("Yes");
yesButton.addActionListener(this);
myPanel.add(yesButton);
noButton = new JButton("No");
noButton.addActionListener(this);
myPanel.add(noButton);
pack();
setLocationRelativeTo(frame);
setVisible(true);
}

public void actionPerformed(ActionEvent e) {
if(yesButton == e.getSource()) {
CustomDialogSearch myDialog = new CustomDialogSearch(parentFrame, false, "CustomDialog Search?");
System.err.println("User chose yes.");
answer = true;
myDialog.getAnswer();
System.out.println("myDialog.getAnswer()="+myDialog.getAnswer());
myDialog.show();

if(myDialog.getAnswer()==true)
{
System.out.println("tofront");
this.toFront();
}
//setVisible(false);
}
else if(noButton == e.getSource()) {
System.err.println("User chose no.");
answer = false;
setVisible(false);
}
}

}

import javax.swing.JDialog;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionEvent;

public class CustomDialogSearch extends JDialog implements ActionListener {
private JPanel myPanel = null;
private JButton yesButton = null;
private JButton noButton = null;
private boolean answer = false;
public boolean getAnswer() { return answer; }

public CustomDialogSearch(JFrame frame, boolean modal, String myMessage) {
super(frame, modal);
myPanel = new JPanel();
getContentPane().add(myPanel);
myPanel.add(new JLabel(myMessage));
yesButton = new JButton("Yes");
yesButton.addActionListener(this);
myPanel.add(yesButton);
noButton = new JButton("No");
noButton.addActionListener(this);
myPanel.add(noButton);
pack();
setLocationRelativeTo(frame);
setVisible(true);
}

public void actionPerformed(ActionEvent e) {
if(yesButton == e.getSource()) {
System.err.println("Search User chose yes.");
answer = true;
//setVisible(false);
}
else if(noButton == e.getSource()) {
System.err.println("Search User chose no.");
answer = false;
setVisible(false);
}
}

}

最佳答案

I even tried to set CustomDialogSearch's parent to be CustomDialog the behaviour still not correct.

我认为您在这里的方向是正确的,但您需要使用对话模态类型。例如:

  • Custom Dialog Main(“父”对话框)的模态类型设置为Dialog.ModalityType.APPLICATION_MODAL .通过在该对话框可见时执行此操作,它将阻止除其子项之外的所有窗口。
  • 自定义对话搜索(“子”对话)的模态类型设置为Dialog.ModalityType.MODELESS .这样它就不会阻塞任何其他窗口,您可以从子窗口转到父窗口,反之亦然。

为了更好地理解,请查看 How to Use Modality in Dialogs文章。

例子

这是我上面建议的关于使用模态的代码示例:

import java.awt.Dialog;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class Demo {

private void createAndShowGUI() {
JButton button = new JButton("Create Parent modal dialog");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JButton button = (JButton)e.getSource();
JFrame owner = (JFrame)SwingUtilities.windowForComponent(button);
Demo.this.createAndShowParentDialog(owner);
}
});

JFrame frame = new JFrame("Demo");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(button);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

private void createAndShowParentDialog(JFrame owner) {
JButton button = new JButton("Create Child non-modal dialog");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JButton button = (JButton)e.getSource();
JDialog parent = (JDialog)SwingUtilities.windowForComponent(button);
Demo.this.createAndShowChildrenDialog(parent);
}
});

JDialog parentDialog = new JDialog(owner, "Parent dialog");
parentDialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
parentDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
parentDialog.getContentPane().add(button);
parentDialog.pack();
parentDialog.setLocationRelativeTo(null);
parentDialog.setVisible(true);
}

private void createAndShowChildrenDialog(JDialog parent) {
JButton backButton = new JButton("Back to parent dialog");
backButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JButton button = (JButton)e.getSource();
Window dialog = SwingUtilities.windowForComponent(button);
dialog.getOwner().toFront();
}
});

JDialog childDialog = new JDialog(parent, "Child dialog");
childDialog.setModalityType(Dialog.ModalityType.MODELESS);
childDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
childDialog.getContentPane().add(backButton);
childDialog.pack();
childDialog.setLocationRelativeTo(null);
childDialog.setVisible(true);
}


public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Demo().createAndShowGUI();
}
});
}
}

编辑

I can select the windows of parent and child JDialogs but when I select the parent JDialog window, the child JDialog is still in front of parent JDialog.

好吧,我现在对问题所在有了更好的理解。此行为取决于 native 窗口系统如何处理焦点窗口和 Activity 窗口。话虽如此,如果您调用 toFront()它会尝试将窗口放在堆栈的顶部,但某些平台不允许拥有其他窗口的窗口出现在其子窗口的顶部。当您调用 toBack() 时也会发生同样的情况方法。有关详细信息,请参阅 javadoc。

我已经在 Windows 7 上测试了我的代码(32 位,如果它有任何区别的话)并且父对话框变得聚焦但它的子对话框仍然显示(未聚焦)在顶部。如上所述,由窗口系统决定如何处理这个问题。

关于java - Jframe 中有 2 个 Jdialogs 的 setModal 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22037658/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com