簡體   English   中英

從JDialog打開JFrame,它顯示在JDialog的頂部

[英]Open JFrame from JDialog and it shows on top of JDialog

在這種情況下,“我的JFrame有一個按鈕,單擊它會打開一個JDialog ,這是一個模型對話框。 JDialog有另一個按鈕,單擊它時我想打開另一個JFrmae

結果:另一個Jframe打開,但它不會到達頂部,它顯示在對話框下方。我想在該對話框頂部打開第二個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);
    }
}

JDialog有另一個按鈕,單擊它時我想打開另一個JFrmae。

不要那樣做 一個典型的Swing應用程序具有一個主JFrame和幾個JDialog 請參閱本主題使用多個JFrame,良好/不良做法?

結果:另一個Jframe打開,但它不會到達頂部,它顯示在對話框下方。我想在該對話框頂部打開第二個JFrame。

當然可以,因為對話框是模式對話框。

可以使用secondFrame.setAlwaysOnTop(true); 但我沒有控制權將其關閉或移動。

它不會解決任何問題,因為問題與對話框中的模態有關。 請參閱本文: 如何在對話框中使用模態以了解模態如何工作。 這個答案中也有一個解釋。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM