這是場景, 我的JFrame
有一個按鈕,它會在點擊它時打開JDialog
,它是一個模型對話框。 JDialog
有另一個按鈕,我想打開另一個JFrmae
點擊它時打開。從JDialog打開JFrame,它顯示在JDialog頂部
結果:另一個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,而只是使用框架?你有更多的控制框架(在我看來)。 – TheOneWhoPrograms
請參閱[使用多個JFrames,好/壞實踐?](http://stackoverflow.com/q/9554636/2587435)。查看一些更好解決方案的接受答案。 –
你知道'dialog.setModal(true);'是什麼意思嗎? –