Modal Window 在 java swing 中的 Modal Window 之上?
Modal Window on top of Modal Window in java swing?
我有以下问题:
我有一个填满整个屏幕的主应用程序 window (JFrame)。
单击按钮时,会出现一个较小的 window,让用户输入一些数据。当用户这样做时,主 window 不应跳到它前面,也不允许交互。
解决方案:打开模式 JDialog。让我们称之为对话 1。
但是当单击此对话框中的按钮时,应该会弹出一个新的 window (yes/no-dialog) .. 并且再次需要在已经模态的 JDialog 对话框 1 上进行模态操作。尝试做也就是说,第二个对话框一直在第一个对话框后面消失。
我尝试将 Dialog 1 设为 JFrame,但当然,我松开了 "modal" 位。强制 Dialog 1 停留在 from 仍然保持主要 window 的按钮可点击。
我错过了什么?我怎样才能把模态摆动 window 放在另一个模态摆动 window 上?
编辑:
一个非实际工作版本的最小示例:
主要class开场:
public class MainWin extends JFrame {
public MainWin(){
this.setSize(800,800);
JButton b = new JButton("click hehe");
this.getContentPane().add(b);
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Dia1(MainWin.this);
}
});
this.setVisible(true);
}
}
主要window:
public class MainWin extends JFrame {
public MainWin(){
this.setSize(800,800);
JButton b = new JButton("click hehe");
this.getContentPane().add(b);
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Dia1(MainWin.this);
}
});
this.setVisible(true);
}
}
第一个对话:
public class Dia1 extends JDialog {
public Dia1(final JFrame parent){
super(parent, true);
this.setSize(400, 400);
JButton b = new JButton("click hehe");
this.getContentPane().add(b);
this.setVisible(true);
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Dia2(parent);
}
});
}
}
第二个对话:
public class Dia2 extends JDialog {
public Dia2(JFrame parent){
super(parent, true);
this.setSize(200, 200);
JButton b = new JButton("click hehe");
this.getContentPane().add(b);
this.setVisible(true);
}
}
PS:我刚刚意识到:Dialog 2 并没有像我怀疑的那样被隐藏。它根本不存在。很可能是因为父 window 被模态对话框屏蔽了?
这是一个 MCVE 模态,如所宣传的那样工作。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class ModalOverModal {
private JComponent ui = null;
ModalOverModal() {
initUI();
}
public void initUI() {
if (ui!=null) return;
ui = new JPanel(new GridLayout());
ui.setBorder(new EmptyBorder(40,40,40,40));
final JButton b1 = new JButton("Open Modal Dialog");
b1.setMargin(new Insets(40, 200, 40, 200));
ui.add(b1);
final JButton b2 = new JButton("Open 2nd Modal Dialog");
ActionListener al1 = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(b1, b2);
}
};
b1.addActionListener(al1);
ActionListener al2 = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(b2, "Close Me!");
}
};
b2.addActionListener(al2);
}
public JComponent getUI() {
return ui;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
ModalOverModal o = new ModalOverModal();
JFrame f = new JFrame("Modal over Modal");
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());
f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}
我有以下问题:
我有一个填满整个屏幕的主应用程序 window (JFrame)。
单击按钮时,会出现一个较小的 window,让用户输入一些数据。当用户这样做时,主 window 不应跳到它前面,也不允许交互。
解决方案:打开模式 JDialog。让我们称之为对话 1。
但是当单击此对话框中的按钮时,应该会弹出一个新的 window (yes/no-dialog) .. 并且再次需要在已经模态的 JDialog 对话框 1 上进行模态操作。尝试做也就是说,第二个对话框一直在第一个对话框后面消失。
我尝试将 Dialog 1 设为 JFrame,但当然,我松开了 "modal" 位。强制 Dialog 1 停留在 from 仍然保持主要 window 的按钮可点击。
我错过了什么?我怎样才能把模态摆动 window 放在另一个模态摆动 window 上?
编辑: 一个非实际工作版本的最小示例:
主要class开场:
public class MainWin extends JFrame {
public MainWin(){
this.setSize(800,800);
JButton b = new JButton("click hehe");
this.getContentPane().add(b);
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Dia1(MainWin.this);
}
});
this.setVisible(true);
}
}
主要window:
public class MainWin extends JFrame {
public MainWin(){
this.setSize(800,800);
JButton b = new JButton("click hehe");
this.getContentPane().add(b);
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Dia1(MainWin.this);
}
});
this.setVisible(true);
}
}
第一个对话:
public class Dia1 extends JDialog {
public Dia1(final JFrame parent){
super(parent, true);
this.setSize(400, 400);
JButton b = new JButton("click hehe");
this.getContentPane().add(b);
this.setVisible(true);
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Dia2(parent);
}
});
}
}
第二个对话:
public class Dia2 extends JDialog {
public Dia2(JFrame parent){
super(parent, true);
this.setSize(200, 200);
JButton b = new JButton("click hehe");
this.getContentPane().add(b);
this.setVisible(true);
}
}
PS:我刚刚意识到:Dialog 2 并没有像我怀疑的那样被隐藏。它根本不存在。很可能是因为父 window 被模态对话框屏蔽了?
这是一个 MCVE 模态,如所宣传的那样工作。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class ModalOverModal {
private JComponent ui = null;
ModalOverModal() {
initUI();
}
public void initUI() {
if (ui!=null) return;
ui = new JPanel(new GridLayout());
ui.setBorder(new EmptyBorder(40,40,40,40));
final JButton b1 = new JButton("Open Modal Dialog");
b1.setMargin(new Insets(40, 200, 40, 200));
ui.add(b1);
final JButton b2 = new JButton("Open 2nd Modal Dialog");
ActionListener al1 = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(b1, b2);
}
};
b1.addActionListener(al1);
ActionListener al2 = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(b2, "Close Me!");
}
};
b2.addActionListener(al2);
}
public JComponent getUI() {
return ui;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
ModalOverModal o = new ModalOverModal();
JFrame f = new JFrame("Modal over Modal");
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());
f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}