英文:
two dialogs setAlwaysOnTop(true): the second generated one is behind the first one
问题
我有一个可以生成另一个对话框(JDialog)的对话框。这两个JDialog都具有setAlwaysOnTop(true)属性,并且不是模态的。从第一个对话框生成的第二个JDialog始终显示在后面。我希望它显示在前面。
我尝试了几种方法:toFront(),requestFocus()等。
以下是重现问题的简短示例:
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
public class SwingTester {
public static void main(String[] args) {
createWindow();
}
private static void createWindow() {
final JDialog modelDialog = createDialog();
modelDialog.setVisible(true);
}
private static JDialog createDialog(){
final JDialog modelDialog = new JDialog();
modelDialog.setBounds(132, 132, 300, 200);
Container dialogContainer = modelDialog.getContentPane();
dialogContainer.setLayout(new BorderLayout());
JPanel panel1 = new JPanel();
JButton okButton = new JButton("确定");
okButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
final JDialog modelDialog = createDialog();
modelDialog.setVisible(true);
}
});
panel1.add(okButton);
dialogContainer.add(panel1, BorderLayout.SOUTH);
modelDialog.setAlwaysOnTop(true);
return modelDialog;
}
}
在点击“确定”按钮后,我们看到另一个对话框出现在当前对话框后面。新的对话框拥有焦点,但仍然在后面。
英文:
I have a JDialog which can generate another one. The two JDialogs have the property setAlwaysOnTop(true) and aren't modal. The second Jdialog generated from the first one appears always behind. I would like it to appear in front.
I tried several things : toFront(), requestFocus(), etc..
Here a short example to reproduce the problem:
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
public class SwingTester {
public static void main(String[] args) {
createWindow();
}
private static void createWindow() {
final JDialog modelDialog = createDialog();
modelDialog.setVisible(true);
}
private static JDialog createDialog(){
final JDialog modelDialog = new JDialog();
modelDialog.setBounds(132, 132, 300, 200);
Container dialogContainer = modelDialog.getContentPane();
dialogContainer.setLayout(new BorderLayout());
JPanel panel1 = new JPanel();
JButton okButton = new JButton("Ok");
okButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
final JDialog modelDialog = createDialog();
modelDialog.setVisible(true);
}
});
panel1.add(okButton);
dialogContainer.add(panel1, BorderLayout.SOUTH);
modelDialog.setAlwaysOnTop(true);
return modelDialog;
}
After a click on the OK button, we see another dialog appearing behind the current one. The new dialog has the focus but is still behind
答案1
得分: 1
实际上,我尝试了你的代码,没有出现任何问题。如果你想要更清楚地看到它,可以创建一个带有随机位置的对话框。每次点击时,新的对话框会随机地出现在顶部。
modelDialog.setBounds(new Random().nextInt(400), new Random().nextInt(400), 300, 200);
但是我的建议是将对话框创建为模态对话框,此外,将旧对话框设置为父对话框。
英文:
Actually, I tried your code, and it is no problem appears. If you want to see it too clearly, create dialog with random location. In every click, new dialog is random place on the top.
modelDialog.setBounds(new Random().nextInt(400), new Random().nextInt(400), 300, 200);
But my suggestion is create dialog as modal, in addition to set old one as parent.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论