两个对话框设置了setAlwaysOnTop(true):第二个生成的在第一个对话框后面。

huangapple go评论59阅读模式
英文:

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;
}

两个对话框设置了setAlwaysOnTop(true):第二个生成的在第一个对话框后面。

两个对话框设置了setAlwaysOnTop(true):第二个生成的在第一个对话框后面。

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.

huangapple
  • 本文由 发表于 2020年10月1日 18:10:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/64153239.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定