gpt4 book ai didi

java - 如何只为他的 parent 制作 JDialog onTop?

转载 作者:搜寻专家 更新时间:2023-11-01 02:12:38 25 4
gpt4 key购买 nike

比方说,我们有几个同时可见的 JFrame 窗口,并且每个窗口都会出现 JDialog。当我们的窗口处于级联模式并且对话框 setAlwaysOnToptrue 时,所有对话框都将在最后一个窗口上可见。

我只想将一个 Dialog 组件与其所有者相关联,这样当您在 Frames 之间切换时,您只会在顶部看到一个对话框,并且在单击一个框架时不会丢失该对话框。

对话框有这样的构造器:

setAlwaysOnTop(true);
setModal(false);

提前致谢!

最佳答案

How to make JDialog onTop only for his parent?
  • setParent in the constructor properly

  • 必须使用 setModalityType f.e. ModalityType.DOCUMENT_MODAL ModalityType.APPLICATION_MODAL 代替 setModal

  • setModalinitialized 的容器有效/是父级 这个 JDialog

  • 不要使用多个JFrame,而是使用JDialog,将此容器重新用于其他操作

例如

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SuperConstructor extends JFrame {

private static final long serialVersionUID = 1L;

public SuperConstructor() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setPreferredSize(new Dimension(300, 300));
setTitle("Super constructor");
Container cp = getContentPane();
JButton b = new JButton("Show dialog");
b.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent evt) {
FirstDialog firstDialog = new FirstDialog(SuperConstructor.this);
}
});
cp.add(b, BorderLayout.SOUTH);
JButton bClose = new JButton("Close");
bClose.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent evt) {
System.exit(0);
}
});
add(bClose, BorderLayout.NORTH);
pack();
setVisible(true);
}

public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
SuperConstructor superConstructor = new SuperConstructor();
}
});
}

private class FirstDialog extends JDialog {

private static final long serialVersionUID = 1L;

FirstDialog(final Frame parent) {
super(parent, "FirstDialog");
setPreferredSize(new Dimension(200, 200));
setLocationRelativeTo(parent);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
JButton bNext = new JButton("Show next dialog");
bNext.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent evt) {
SecondDialog secondDialog = new SecondDialog(parent, false);
}
});
add(bNext, BorderLayout.NORTH);
JButton bClose = new JButton("Close");
bClose.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent evt) {
setVisible(false);
}
});
add(bClose, BorderLayout.SOUTH);
pack();
setVisible(true);
}
}
private int i;

private class SecondDialog extends JDialog {

private static final long serialVersionUID = 1L;

SecondDialog(final Frame parent, boolean modal) {
//super(parent); // Makes this dialog unfocusable as long as FirstDialog is visible
setPreferredSize(new Dimension(200, 200));
setLocation(300, 50);
setModal(modal);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setTitle("SecondDialog " + (i++));
setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
JButton bClose = new JButton("Close");
bClose.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent evt) {
setVisible(false);
}
});
add(bClose, BorderLayout.SOUTH);
pack();
setVisible(true);
}
}
}

关于java - 如何只为他的 parent 制作 JDialog onTop?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15134361/

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